feat(mbuild): media-upload doit() methods

It's just a first step, and even though the generation works well,
I am still missing the right Rust code. Will have to simplify ...
This commit is contained in:
Sebastian Thiel
2015-03-08 15:49:28 +01:00
parent de0c7a4ae0
commit 5b2d8a77a3
7 changed files with 1046 additions and 370 deletions

View File

@@ -18,6 +18,8 @@ api:
terms:
# how to actually do something with the API
action: doit
# when a resource is supposed to be uploaded
upload_action: upload
templates:
# all output directories are relative to the one set for the respective API
- source: README.md

View File

@@ -1,6 +1,8 @@
// COPY OF 'src/rust/cmn.rs'
// DO NOT EDIT
use std::marker::MarkerTrait;
use std::io::{Read, Seek};
use std::borrow::BorrowMut;
/// Identifies the Hub. There is only one per library, this trait is supposed
/// to make intended use more explicit.
@@ -30,3 +32,7 @@ pub trait Part: MarkerTrait {}
/// Identifies types which are only used by other types internally.
/// They have no special meaning, this trait just marks them for completeness.
pub trait NestedType: MarkerTrait {}
/// A utility to specify reader types which provide seeking capabilities too
pub trait ReadSeek: Seek + Read {}
impl<T: Seek + Read> ReadSeek for T {}

File diff suppressed because it is too large Load Diff

View File

@@ -33,8 +33,9 @@ use std::borrow::BorrowMut;
use std::cell::RefCell;
use std::default::Default;
use std::io::{Read, Seek};
use std::fs::File;
pub use cmn::{Hub, ResourceMethodsBuilder, MethodBuilder, Resource, Part, ResponseResult, RequestValue, NestedType};
pub use cmn::{Hub, ReadSeek, ResourceMethodsBuilder, MethodBuilder, Resource, Part, ResponseResult, RequestValue, NestedType};
// ##############

View File

@@ -6,7 +6,7 @@
schema_to_required_property, rust_copy_value_s, is_required_property,
hide_rust_doc_test, build_all_params, REQUEST_VALUE_PROPERTY_NAME, organize_params,
indent_by, to_rust_type, rnd_arg_val_for_type, extract_parts, mb_type_params_s,
hub_type_params_s)
hub_type_params_s, method_media_params)
def get_parts(part_prop):
if not part_prop:
@@ -103,7 +103,7 @@ impl${mb_tparams} MethodBuilder for ${ThisType} {}
impl${mb_tparams} ${ThisType} {
${self._action_fn(resource, method, params, request_value, parts)}\
${self._action_fn(resource, method, m, params, request_value, parts)}\
## SETTERS ###############
% for p in params:
@@ -259,12 +259,49 @@ ${'.' + api.terms.action | indent_by(13)}();
## create an entire 'api.terms.action' method
###############################################################################################
###############################################################################################
<%def name="_action_fn(resource, method, params, request_value, parts)">\
<%def name="_action_fn(resource, method, m, params, request_value, parts)">\
<%
media_params = method_media_params(m)
type_params = ''
where = ''
qualifier = 'pub '
add_args = ''
rtype = '()'
if media_params:
stripped = lambda s: s.strip().strip(',')
qualifier = ''
for p in media_params:
type_params += p.type.param + (' = %s, ' % p.type.default)
where += p.type.param + ': BorrowMut<' + p.type.where + '>, '
add_args += p.type.arg_name + ': ' + ('Option<%s>' % p.type.param) + ', '
# end for each param
where = ' where ' + stripped(where)
type_params = '<' + stripped(type_params) + '>'
add_args = ', ' + stripped(add_args)
# end handle media params
action_fn = qualifier + 'fn ' + api.terms.action + type_params + ('(mut self%s)' % add_args) + ' -> ' + rtype + where
%>
/// Perform the operation you have build so far.
/// Can only be called once !
/// TODO: Build actual call
pub fn ${api.terms.action}(self) {
${action_fn} {
}
% for p in media_params:
pub fn ${api.terms.upload_action}${p.type.suffix}<${p.type.param}>(mut self, ${p.type.arg_name}: ${p.type.param}) -> ${rtype}
where ${p.type.param}: BorrowMut<${p.type.where}> {
self.${api.terms.action}(\
% for _ in range(0, loop.index):
None, \
% endfor
Some(${p.type.arg_name}), \
% for _ in range(loop.index+1, len(media_params)):
None, \
% endfor
)
}
% endfor
</%def>

View File

@@ -48,14 +48,20 @@ REQUEST_VALUE_PROPERTY_NAME = 'request'
PROTOCOL_TYPE_INFO = {
'simple' : {
'arg_name': 'stream',
'param': 'R',
'description': 'TODO: FOO',
'where': 'R: Read'
'default': 'File',
'where': 'Read',
'suffix': '',
},
'resumable' : {
'arg_name': 'resumeable_stream',
'param': 'RS',
'description': 'TODO: BAR',
'where': 'RS: Read+Seek'
'default': 'File',
'where': 'ReadSeek',
'suffix': '_resumable',
}
}
@@ -458,12 +464,10 @@ def method_media_params(m):
# the pi (proto-info) dict can be shown to the user
pi = {'multipart': proto.multipart, 'maxSize': mu.maxSize, 'mimeTypes': mu.accept}
try:
ti = PROTOCOL_TYPE_INFO[pn]
ti = type(m)(PROTOCOL_TYPE_INFO[pn])
except KeyError:
raise AssertionError("media upload protocol '%s' is not implemented" % pn)
p = type(m)({'name': 'media_%s',
'priority': 0,
TREF: ti.member_type,
'info': pi,
'path': proto.path,
'type': ti,

View File

@@ -1,4 +1,6 @@
use std::marker::MarkerTrait;
use std::io::{Read, Seek};
use std::borrow::BorrowMut;
/// Identifies the Hub. There is only one per library, this trait is supposed
/// to make intended use more explicit.
@@ -28,3 +30,7 @@ pub trait Part: MarkerTrait {}
/// Identifies types which are only used by other types internally.
/// They have no special meaning, this trait just marks them for completeness.
pub trait NestedType: MarkerTrait {}
/// A utility to specify reader types which provide seeking capabilities too
pub trait ReadSeek: Seek + Read {}
impl<T: Seek + Read> ReadSeek for T {}