From af73936b9536b3844d8bc0a05432d413de58f0aa Mon Sep 17 00:00:00 2001 From: Petros Angelatos Date: Fri, 18 Dec 2020 21:17:17 +0100 Subject: [PATCH] rename cmn to client Signed-off-by: Petros Angelatos --- Cargo.toml | 6 ++-- src/mako/api/api.rs.mako | 4 +-- src/mako/api/lib.rs.mako | 4 +-- src/mako/api/lib/lib.mako | 6 ++-- src/mako/api/lib/mbuild.mako | 56 +++++++++++++++--------------- src/mako/api/lib/rbuild.mako | 2 +- src/mako/cli/lib/engine.mako | 6 ++-- src/mako/cli/main.rs.mako | 2 +- src/mako/deps.mako | 4 +-- src/mako/lib/util.py | 20 +++++------ src/rust/api/{cmn.rs => client.rs} | 2 +- src/rust/api/mod.rs | 2 +- src/rust/cli/{cmn.rs => client.rs} | 0 src/rust/cli/mod.rs | 2 +- src/rust/lib.rs | 4 +-- 15 files changed, 60 insertions(+), 60 deletions(-) rename src/rust/api/{cmn.rs => client.rs} (99%) rename src/rust/cli/{cmn.rs => client.rs} (100%) diff --git a/Cargo.toml b/Cargo.toml index ca5fe9f687..87bcb205a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ # This library is just to try out the code that should ultimately go into the code generator ! [package] -name = "cmn" +name = "client" version = "0.0.1" authors = ["Sebastian Thiel "] description = "A library to facilitate interacting with your youtube account" @@ -11,8 +11,8 @@ license = "MIT/Apache-2.0" keywords = ["youtube", "google", "protocol", "not-for-use"] [lib] -# The common code, used by all generated implementations -name = "cmn" +# The common client code, used by all generated implementations +name = "client" path = "src/rust/lib.rs" [dependencies] diff --git a/src/mako/api/api.rs.mako b/src/mako/api/api.rs.mako index 24d5802ab1..5731400474 100644 --- a/src/mako/api/api.rs.mako +++ b/src/mako/api/api.rs.mako @@ -27,7 +27,7 @@ use std::mem; use std::thread::sleep; use std::time::Duration; -use crate::cmn; +use crate::client; // ############## // UTILITIES ### @@ -57,7 +57,7 @@ pub struct ${hub_type}${ht_params} { _root_url: String, } -impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> cmn::Hub for ${hub_type}${ht_params} {} +impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> client::Hub for ${hub_type}${ht_params} {} impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> ${hub_type}${ht_params} where ${', '.join(hub_type_bounds())} { diff --git a/src/mako/api/lib.rs.mako b/src/mako/api/lib.rs.mako index 7827f6f8c9..e558e6fc6c 100644 --- a/src/mako/api/lib.rs.mako +++ b/src/mako/api/lib.rs.mako @@ -51,8 +51,8 @@ extern crate mime; extern crate url; pub mod api; -pub mod cmn; +pub mod client; // Re-export the hub type and some basic client structs pub use api::${hub_type}; -pub use cmn::{Result, Error, Delegate}; +pub use client::{Result, Error, Delegate}; diff --git a/src/mako/api/lib/lib.mako b/src/mako/api/lib/lib.mako index 1a36e60717..96d6a01719 100644 --- a/src/mako/api/lib/lib.mako +++ b/src/mako/api/lib/lib.mako @@ -192,7 +192,7 @@ ${self.hub_usage_example(c, rust_doc, fr=fr)}\ ${'##'} Handling Errors -All errors produced by the system are provided either as ${link('Result', 'cmn::Result')} enumeration as return value of +All errors produced by the system are provided either as ${link('Result', 'client::Result')} enumeration as return value of the ${api.terms.action}() methods, or handed as possibly intermediate results to either the ${link('Hub Delegate', delegate_url)}, or the ${link('Authenticator Delegate', urls.authenticator_delegate)}. @@ -200,9 +200,9 @@ When delegates handle errors or intermediate values, they may have a chance to i makes the system potentially resilient to all kinds of errors. ${'##'} Uploads and Downloads -If a method supports downloads, the response body, which is part of the ${link('Result', 'cmn::Result')}, should be +If a method supports downloads, the response body, which is part of the ${link('Result', 'client::Result')}, should be read by you to obtain the media. -If such a method also supports a ${link('Response Result', 'cmn::ResponseResult')}, it will return that by default. +If such a method also supports a ${link('Response Result', 'client::ResponseResult')}, it will return that by default. You can see it as meta-data for the actual media. To trigger a media download, you will have to set up the builder by making this call: `${ADD_PARAM_MEDIA_EXAMPLE}`. diff --git a/src/mako/api/lib/mbuild.mako b/src/mako/api/lib/mbuild.mako index 701c67691d..0a292722b6 100644 --- a/src/mako/api/lib/mbuild.mako +++ b/src/mako/api/lib/mbuild.mako @@ -412,7 +412,7 @@ match result { where = '' qualifier = 'pub ' add_args = '' - rtype = 'cmn::Result' + rtype = 'client::Result' response_schema = method_response(c, m) supports_download = m.get('supportsMediaDownload', False); @@ -420,7 +420,7 @@ match result { if response_schema: if not supports_download: reserved_params = ['alt'] - rtype = 'cmn::Result<(hyper::client::Response, %s)>' % (response_schema.id) + rtype = 'client::Result<(hyper::client::Response, %s)>' % (response_schema.id) mtype_param = 'RS' @@ -430,7 +430,7 @@ match result { if media_params: type_params = '<%s>' % mtype_param qualifier = '' - where = '\n\t\twhere ' + mtype_param + ': cmn::ReadSeek' + where = '\n\t\twhere ' + mtype_param + ': client::ReadSeek' add_args = (', mut reader: %s, reader_mime_type: mime::Mime' % mtype_param) + ", protocol: &'static str" for p in media_params: if p.protocol == 'simple': @@ -465,7 +465,7 @@ match result { if media_params: max_size = media_params[0].max_size if max_size > 0: - READER_SEEK += "if size > %i {\n\treturn Err(cmn::Error::UploadSizeLimitExceeded(size, %i))\n}" % (max_size, max_size) + READER_SEEK += "if size > %i {\n\treturn Err(client::Error::UploadSizeLimitExceeded(size, %i))\n}" % (max_size, max_size) special_cases = set() for possible_url in possible_urls: @@ -505,13 +505,13 @@ match result { % endif use std::io::{Read, Seek}; use hyper::header::{ContentType, ContentLength, Authorization, Bearer, UserAgent, Location}; - use cmn::ToParts; - let mut dd = cmn::DefaultDelegate; - let mut dlg: &mut dyn cmn::Delegate = match ${delegate} { + use client::ToParts; + let mut dd = client::DefaultDelegate; + let mut dlg: &mut dyn client::Delegate = match ${delegate} { Some(d) => d, None => &mut dd }; - dlg.begin(cmn::MethodInfo { id: "${m.id}", + dlg.begin(client::MethodInfo { id: "${m.id}", http_method: ${method_name_to_variant(m.httpMethod)} }); let mut params: Vec<(&str, String)> = Vec::with_capacity(${len(params) + len(reserved_params)} + ${paddfields}.len()); <% @@ -558,7 +558,7 @@ match result { for &field in [${', '.join(enclose_in('"', reserved_params + [p.name for p in field_params]))}].iter() { if ${paddfields}.contains_key(field) { ${delegate_finish}(false); - return Err(cmn::Error::FieldClash(field)); + return Err(client::Error::FieldClash(field)); } } for (name, value) in ${paddfields}.iter() { @@ -621,7 +621,7 @@ else { Some(value) => params.push(("key", value)), None => { ${delegate_finish}(false); - return Err(cmn::Error::MissingAPIKey) + return Err(client::Error::MissingAPIKey) } } % endif @@ -679,7 +679,7 @@ else { let mut request_value_reader = { let mut value = json::value::to_value(&self.${property(REQUEST_VALUE_PROPERTY_NAME)}).expect("serde to work"); - cmn::remove_json_null_values(&mut value); + client::remove_json_null_values(&mut value); let mut dst = io::Cursor::new(Vec::with_capacity(128)); json::to_writer(&mut dst, &value).unwrap(); dst @@ -703,7 +703,7 @@ else { Some(token) => token, None => { ${delegate_finish}(false); - return Err(cmn::Error::MissingToken(err)) + return Err(client::Error::MissingToken(err)) } } } @@ -719,7 +719,7 @@ else { should_ask_dlg_for_url = false; upload_url_from_server = false; let url = upload_url.as_ref().and_then(|s| Some(hyper::Url::parse(s).unwrap())).unwrap(); - hyper::client::Response::new(url, Box::new(cmn::DummyNetworkStream)).and_then(|mut res| { + hyper::client::Response::new(url, Box::new(client::DummyNetworkStream)).and_then(|mut res| { res.status = hyper::status::StatusCode::Ok; res.headers.set(Location(upload_url.as_ref().unwrap().clone())); Ok(res) @@ -728,7 +728,7 @@ else { % endif <%block filter="indent_by(resumable_media_param and 4 or 0)">\ % if request_value and simple_media_param: - let mut mp_reader: cmn::MultiPartReader = Default::default(); + let mut mp_reader: client::MultiPartReader = Default::default(); let (mut body_reader, content_type) = match protocol { "${simple_media_param.protocol}" => { mp_reader.reserve_exact(2); @@ -772,7 +772,7 @@ else { % if resumable_media_param: upload_url_from_server = true; if protocol == "${resumable_media_param.protocol}" { - req = req.header(cmn::XUploadContentType(reader_mime_type.clone())); + req = req.header(client::XUploadContentType(reader_mime_type.clone())); } % endif @@ -791,16 +791,16 @@ else { continue; } ${delegate_finish}(false); - return Err(cmn::Error::HttpError(err)) + return Err(client::Error::HttpError(err)) } Ok(mut res) => { if !res.status.is_success() { let mut json_err = String::new(); res.read_to_string(&mut json_err).unwrap(); - let json_server_error = json::from_str::(&json_err).ok(); - let server_error = json::from_str::(&json_err) - .or_else(|_| json::from_str::(&json_err).map(|r| r.error)) + let json_server_error = json::from_str::(&json_err).ok(); + let server_error = json::from_str::(&json_err) + .or_else(|_| json::from_str::(&json_err).map(|r| r.error)) .ok(); if let oauth2::Retry::After(d) = dlg.http_failure(&res, @@ -810,9 +810,9 @@ else { continue; } ${delegate_finish}(false); - return match json::from_str::(&json_err){ - Err(_) => Err(cmn::Error::Failure(res)), - Ok(serr) => Err(cmn::Error::BadRequest(serr)) + return match json::from_str::(&json_err){ + Err(_) => Err(client::Error::Failure(res)), + Ok(serr) => Err(client::Error::BadRequest(serr)) } } % if resumable_media_param: @@ -825,7 +825,7 @@ else { dlg.store_upload_url(Some(url_str)); } - cmn::ResumableUploadHelper { + client::ResumableUploadHelper { client: &mut client.borrow_mut(), delegate: dlg, start_at: if upload_url_from_server { Some(0) } else { None }, @@ -841,12 +841,12 @@ else { match upload_result { None => { ${delegate_finish}(false); - return Err(cmn::Error::Cancelled) + return Err(client::Error::Cancelled) } Some(Err(err)) => { ## Do not ask the delgate again, as it was asked by the helper ! ${delegate_finish}(false); - return Err(cmn::Error::HttpError(err)) + return Err(client::Error::HttpError(err)) } ## Now the result contains the actual resource, if any ... it will be ## decoded next @@ -856,7 +856,7 @@ else { ## delegate was called in upload() already - don't tell him again dlg.store_upload_url(None); ${delegate_finish}(false); - return Err(cmn::Error::Failure(res)) + return Err(client::Error::Failure(res)) } } } @@ -875,7 +875,7 @@ if enable_resource_parsing \ Ok(decoded) => (res, decoded), Err(err) => { dlg.response_json_decode_error(&json_response, &err); - return Err(cmn::Error::JsonDecodeError(json_response, err)); + return Err(client::Error::JsonDecodeError(json_response, err)); } } }\ @@ -901,7 +901,7 @@ if enable_resource_parsing \ /// * *${split_camelcase_s(item_name)}*: ${isinstance(item, (list, tuple)) and put_and(enclose_in("'", item)) or str(item)} % endfor pub fn ${upload_action_fn(api.terms.upload_action, p.type.suffix)}<${mtype_param}>(self, ${p.type.arg_name}: ${mtype_param}, mime_type: mime::Mime) -> ${rtype} - where ${mtype_param}: cmn::ReadSeek { + where ${mtype_param}: client::ReadSeek { self.${api.terms.action}(${p.type.arg_name}, mime_type, "${p.protocol}") } % endfor diff --git a/src/mako/api/lib/rbuild.mako b/src/mako/api/lib/rbuild.mako index 0f5be1e9d2..1c15fc8161 100644 --- a/src/mako/api/lib/rbuild.mako +++ b/src/mako/api/lib/rbuild.mako @@ -95,7 +95,7 @@ impl${rb_params} ${ThisType} { % endfor % endif pub fn ${mangle_ident(a)}${type_params}(&self${method_args}) -> ${RType}${mb_tparams} { - use cmn::ToParts; + use client::ToParts; % if part_prop and request_value: let parts = ${mangle_ident(REQUEST_VALUE_PROPERTY_NAME)}.to_parts(); % endif diff --git a/src/mako/cli/lib/engine.mako b/src/mako/cli/lib/engine.mako index 6319331f78..b336fb0064 100644 --- a/src/mako/cli/lib/engine.mako +++ b/src/mako/cli/lib/engine.mako @@ -31,7 +31,7 @@ <% hub_type_name = 'api::' + hub_type(c.schemas, util.canonical_name()) %>\ -use cmn::{InvalidOptionsError, CLIError, JsonTokenStorage, arg_from_str, writer_from_opts, parse_kv_arg, +use client::{InvalidOptionsError, CLIError, JsonTokenStorage, arg_from_str, writer_from_opts, parse_kv_arg, input_file_from_opts, input_mime_from_opts, FieldCursor, FieldError, CallType, UploadProtocol, calltype_from_str, remove_json_null_values, ComplexType, JsonType, JsonTypeInfo}; @@ -105,12 +105,12 @@ impl<'n> Engine<'n> { // Please note that this call will fail if any part of the opt can't be handled fn new(opt: ArgMatches<'n>) -> Result, InvalidOptionsError> { let (config_dir, secret) = { - let config_dir = match cmn::assure_config_dir_exists(opt.value_of("${CONFIG_DIR_ARG}").unwrap_or("${CONFIG_DIR}")) { + let config_dir = match client::assure_config_dir_exists(opt.value_of("${CONFIG_DIR_ARG}").unwrap_or("${CONFIG_DIR}")) { Err(e) => return Err(InvalidOptionsError::single(e, 3)), Ok(p) => p, }; - match cmn::application_secret_from_directory(&config_dir, "${application_secret_path(util.program_name())}", + match client::application_secret_from_directory(&config_dir, "${application_secret_path(util.program_name())}", "${api.credentials.replace('"', r'\"')}") { Ok(secret) => (config_dir, secret), Err(e) => return Err(InvalidOptionsError::single(e, 4)) diff --git a/src/mako/cli/main.rs.mako b/src/mako/cli/main.rs.mako index 977f0b4294..4badc0a592 100644 --- a/src/mako/cli/main.rs.mako +++ b/src/mako/cli/main.rs.mako @@ -30,7 +30,7 @@ use std::env; use std::io::{self, Write}; use clap::{App, SubCommand, Arg}; -mod cmn; +mod client; ${engine.new(c)}\ diff --git a/src/mako/deps.mako b/src/mako/deps.mako index 9c5bd2f332..bd9abc8944 100644 --- a/src/mako/deps.mako +++ b/src/mako/deps.mako @@ -62,7 +62,7 @@ def gen_type_cfg_path(id): return '$(API_DIR)/type-' + id + '.yaml' - CMN_SRC = '/src/cmn.rs' + CMN_SRC = '/src/client.rs' api_name = util.library_name(an, version) api_target = util.target_directory_name(an, version, suffix) @@ -101,7 +101,7 @@ except: print('Could not open JSON file at {}'.format(api_json)) %>\ -${api_common}: $(RUST_SRC)/${make.id}/cmn.rs $(lastword $(MAKEFILE_LIST)) ${gen_root_stamp} +${api_common}: $(RUST_SRC)/${make.id}/client.rs $(lastword $(MAKEFILE_LIST)) ${gen_root_stamp} @ echo "// COPY OF '$<'" > $@ @ echo "// DO NOT EDIT" >> $@ @cat $< >> $@ diff --git a/src/mako/lib/util.py b/src/mako/lib/util.py index 73b4a43e42..f557958d06 100644 --- a/src/mako/lib/util.py +++ b/src/mako/lib/util.py @@ -62,19 +62,19 @@ ADD_PARAM_MEDIA_EXAMPLE = "." + ADD_PARAM_FN + '("alt", "media")' SPACES_PER_TAB = 4 NESTED_TYPE_SUFFIX = 'item' -DELEGATE_TYPE = 'cmn::Delegate' +DELEGATE_TYPE = 'client::Delegate' REQUEST_PRIORITY = 100 -REQUEST_MARKER_TRAIT = 'cmn::RequestValue' -RESPONSE_MARKER_TRAIT = 'cmn::ResponseResult' -RESOURCE_MARKER_TRAIT = 'cmn::Resource' -CALL_BUILDER_MARKERT_TRAIT = 'cmn::CallBuilder' -METHODS_BUILDER_MARKER_TRAIT = 'cmn::MethodsBuilder' -PART_MARKER_TRAIT = 'cmn::Part' -NESTED_MARKER_TRAIT = 'cmn::NestedType' +REQUEST_MARKER_TRAIT = 'client::RequestValue' +RESPONSE_MARKER_TRAIT = 'client::ResponseResult' +RESOURCE_MARKER_TRAIT = 'client::Resource' +CALL_BUILDER_MARKERT_TRAIT = 'client::CallBuilder' +METHODS_BUILDER_MARKER_TRAIT = 'client::MethodsBuilder' +PART_MARKER_TRAIT = 'client::Part' +NESTED_MARKER_TRAIT = 'client::NestedType' REQUEST_VALUE_PROPERTY_NAME = 'request' DELEGATE_PROPERTY_NAME = 'delegate' -TO_PARTS_MARKER = 'cmn::ToParts' -UNUSED_TYPE_MARKER = 'cmn::UnusedType' +TO_PARTS_MARKER = 'client::ToParts' +UNUSED_TYPE_MARKER = 'client::UnusedType' PROTOCOL_TYPE_INFO = { 'simple' : { diff --git a/src/rust/api/cmn.rs b/src/rust/api/client.rs similarity index 99% rename from src/rust/api/cmn.rs rename to src/rust/api/client.rs index 6ae3e176e6..948a3f223e 100644 --- a/src/rust/api/cmn.rs +++ b/src/rust/api/client.rs @@ -727,7 +727,7 @@ impl<'a, A> ResumableUploadHelper<'a, A> } } -// Copy of src/rust/cli/cmn.rs +// Copy of src/rust/cli/client.rs // TODO(ST): Allow sharing common code between program types pub fn remove_json_null_values(value: &mut json::value::Value) { match *value { diff --git a/src/rust/api/mod.rs b/src/rust/api/mod.rs index fe90a251e2..b9babe5bc1 100644 --- a/src/rust/api/mod.rs +++ b/src/rust/api/mod.rs @@ -1 +1 @@ -pub mod cmn; \ No newline at end of file +pub mod client; diff --git a/src/rust/cli/cmn.rs b/src/rust/cli/client.rs similarity index 100% rename from src/rust/cli/cmn.rs rename to src/rust/cli/client.rs diff --git a/src/rust/cli/mod.rs b/src/rust/cli/mod.rs index fe90a251e2..b9babe5bc1 100644 --- a/src/rust/cli/mod.rs +++ b/src/rust/cli/mod.rs @@ -1 +1 @@ -pub mod cmn; \ No newline at end of file +pub mod client; diff --git a/src/rust/lib.rs b/src/rust/lib.rs index 2fafe5435c..aca0df4985 100644 --- a/src/rust/lib.rs +++ b/src/rust/lib.rs @@ -22,7 +22,7 @@ mod cli; #[cfg(test)] mod test_api { extern crate yup_hyper_mock as hyper_mock; - use super::api::cmn::*; + use super::api::client::*; use self::hyper_mock::*; use std::io::Read; use std::default::Default; @@ -159,7 +159,7 @@ bar\r\n\ #[cfg(test)] mod test_cli { - use super::cli::cmn::*; + use super::cli::client::*; use std::default::Default;