Skip to content

Commit 80161f7

Browse files
committed
fix(types):prune unused and ToParts trait
* do not emit unused types. Sometimes though, rustc doesn't seem to detect that attributses are actually used * ToParts trait is used and implemented only when needed. Linters are back to 'normal'. Fixes #35
1 parent 0152138 commit 80161f7

7 files changed

Lines changed: 249 additions & 111 deletions

File tree

gen/youtube3/src/cmn.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ pub trait NestedType: MarkerTrait {}
5050
pub trait ReadSeek: Seek + Read {}
5151
impl<T: Seek + Read> ReadSeek for T {}
5252

53+
/// A trait for all types that can convert themselves into a *parts* string
54+
pub trait ToParts {
55+
fn to_parts(&self) -> String;
56+
}
57+
5358

5459
/// A utility type which can decode a server response that indicates error
5560
#[derive(Deserialize)]
@@ -98,7 +103,10 @@ pub trait Delegate {
98103
/// Called whenever a server response could not be decoded from json.
99104
/// It's for informational purposes only, the caller will return with an error
100105
/// accordingly.
101-
fn response_json_decode_error(&mut self, json_encoded_value: &str) {}
106+
/// # Arguments
107+
/// `&str` - The json-encoded value which failed to decode.
108+
/// `Error` - The decoder error
109+
fn response_json_decode_error(&mut self, _: &str, _: &serde::json::Error) {}
102110

103111
/// Called whenever the http request returns with a non-success status code.
104112
/// This can involve authentication issues, or anything else that very much

gen/youtube3/src/lib.rs

Lines changed: 181 additions & 77 deletions
Large diffs are not rendered by default.

src/mako/lib.rs.mako

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
<%
77
from util import (new_context, rust_comment, rust_doc_comment, rust_module_doc_comment,
88
rb_type, hub_type, mangle_ident, hub_type_params_s, hub_type_bounds,
9-
rb_type_params_s, find_fattest_resource, HUB_TYPE_PARAMETERS, METHODS_RESOURCE)
9+
rb_type_params_s, find_fattest_resource, HUB_TYPE_PARAMETERS, METHODS_RESOURCE,
10+
UNUSED_TYPE_MARKER, schema_markers)
1011
1112
c = new_context(schemas, resources, context.get('methods'))
1213
hub_type = hub_type(c.schemas, util.canonical_name())
@@ -21,9 +22,8 @@
2122
<%block filter="rust_module_doc_comment">\
2223
${lib.docs(c)}
2324
</%block>
24-
#![feature(core,io, old_io)]
25-
// DEBUG !! TODO: Remove this
26-
#![allow(dead_code, deprecated)]
25+
#![feature(core,io,thread_sleep)]
26+
// Unused attributes happen thanks to defined, but unused structures
2727
// We don't warn about this, as depending on the API, some data structures or facilities are never used.
2828
// Instead of pre-determining this, we just disable the lint. It's manually tuned to not have any
2929
// unused imports in fully featured APIs. Same with unused_mut ... .
@@ -49,9 +49,9 @@ use std::marker::PhantomData;
4949
use serde::json;
5050
use std::io;
5151
use std::fs;
52-
use std::old_io::timer::sleep;
52+
use std::thread::sleep;
5353

54-
pub use cmn::{MultiPartReader, MethodInfo, Result, CallBuilder, Hub, ReadSeek, Part, ResponseResult, RequestValue, NestedType, Delegate, DefaultDelegate, UnusedType};
54+
pub use cmn::{MultiPartReader, ToParts, MethodInfo, Result, CallBuilder, Hub, ReadSeek, Part, ResponseResult, RequestValue, NestedType, Delegate, DefaultDelegate};
5555

5656

5757
// ##############
@@ -119,7 +119,9 @@ impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> ${hub_type}${ht_params}
119119
// SCHEMAS ###
120120
// ##########
121121
% for s in c.schemas.values():
122+
% if UNUSED_TYPE_MARKER not in schema_markers(s, c, transitive=True):
122123
${schema.new(s, c)}
124+
% endif
123125
% endfor
124126
% endif
125127

src/mako/lib/mbuild.mako

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,10 @@ if enable_resource_parsing \
747747
res.read_to_string(&mut json_response).unwrap();
748748
match json::from_str(&json_response) {
749749
Ok(decoded) => (res, decoded),
750-
Err(err) => return Result::JsonDecodeError(err),
750+
Err(err) => {
751+
dlg.response_json_decode_error(&json_response, &err);
752+
return Result::JsonDecodeError(err);
753+
}
751754
}
752755
}\
753756
% if supports_download:

src/mako/lib/schema.mako

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from util import (schema_markers, rust_doc_comment, mangle_ident, to_rust_type, put_and,
33
IO_TYPES, activity_split, enclose_in, REQUEST_MARKER_TRAIT, mb_type, indent_all_but_first_by,
44
NESTED_TYPE_SUFFIX, RESPONSE_MARKER_TRAIT, split_camelcase_s, METHODS_RESOURCE, unique_type_name,
5-
PART_MARKER_TRAIT, canonical_type_name)
5+
PART_MARKER_TRAIT, canonical_type_name, TO_PARTS_MARKER, UNUSED_TYPE_MARKER)
66
77
default_traits = ('RustcEncodable', 'Clone', 'Default')
88
%>\
@@ -27,7 +27,7 @@ ${struct}(${to_rust_type(schemas, s.id, NESTED_TYPE_SUFFIX, s, allow_optionals=a
2727
<%
2828
et = unique_type_name(s.id)
2929
variant_type = lambda p: canonical_type_name(p.type_value)
30-
%>
30+
%>\
3131
pub enum ${et} {
3232
% for p in s.variant.map:
3333
${p.get('description', 'no description provided') | rust_doc_comment, indent_all_but_first_by(1)}
@@ -73,7 +73,7 @@ ${struct};
7373
allow_optionals = False
7474
# use optionals only when needed
7575
76-
## waiting for Default: https://github.com/rust-lang/rustc-serialize/issues/71
76+
# waiting for Default: https://github.com/rust-lang/rustc-serialize/issues/71
7777
if s.type == 'any':
7878
traits.remove('Default')
7979
@@ -105,11 +105,13 @@ impl Default for ${s_type} {
105105
% endif ## type == ?
106106
107107
% for marker_trait in nt_markers:
108+
% if marker_trait not in (TO_PARTS_MARKER, UNUSED_TYPE_MARKER):
108109
impl ${marker_trait} for ${s_type} {}
110+
% endif
109111
% endfor
110112
111-
% if REQUEST_MARKER_TRAIT in nt_markers and 'properties' in s:
112-
impl ${s_type} {
113+
% if TO_PARTS_MARKER in nt_markers and allow_optionals:
114+
impl ${TO_PARTS_MARKER} for ${s_type} {
113115
/// Return a comma separated list of members that are currently set, i.e. for which `self.member.is_some()`.
114116
/// The produced string is suitable for use as a parts list that indicates the parts you are sending, and/or
115117
/// the parts you want to see in the server response.

src/mako/lib/util.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
NESTED_MARKER_TRAIT = 'NestedType'
7272
REQUEST_VALUE_PROPERTY_NAME = 'request'
7373
DELEGATE_PROPERTY_NAME = 'delegate'
74+
TO_PARTS_MARKER = 'ToParts'
75+
UNUSED_TYPE_MARKER = 'UnusedType'
7476

7577
PROTOCOL_TYPE_INFO = {
7678
'simple' : {
@@ -365,39 +367,48 @@ def activity_input_type(schemas, p):
365367
def is_pod_property(p):
366368
return 'format' in p or p.get('type','') == 'boolean'
367369

370+
371+
def _traverse_schema_ids(s, c):
372+
ids = [s.id]
373+
used_by = s.used_by + s.parents
374+
375+
seen = set() # protect against loops, just to be sure ...
376+
while used_by:
377+
id = used_by.pop()
378+
if id in seen:
379+
continue
380+
seen.add(id)
381+
ids.append(id)
382+
383+
oid = c.schemas[id]
384+
used_by.extend(oid.used_by)
385+
used_by.extend(oid.parents)
386+
# end gather usages
387+
return ids
388+
368389
# Return sorted type names of all markers applicable to the given schema
369390
# This list is transitive. Thus, if the schema is used as child of someone with a trait, it
370391
# inherits this trait
371392
def schema_markers(s, c, transitive=True):
372393
res = set()
373-
ids = [s.id]
374-
375-
if transitive:
376-
used_by = s.used_by + s.parents
377-
378-
seen = set() # protect against loops, just to be sure ...
379-
while used_by:
380-
id = used_by.pop()
381-
if id in seen:
382-
continue
383-
seen.add(id)
384-
ids.append(id)
385-
386-
oid = c.schemas[id]
387-
used_by.extend(oid.used_by)
388-
used_by.extend(oid.parents)
389-
# end gather usages
390-
# end is transitive
394+
ids = transitive and _traverse_schema_ids(s, c) or [s.id]
391395

396+
has_activity = False
392397
for sid in ids:
393398
activities = c.sta_map.get(sid, dict())
394399
if len(activities) == 0:
395400
continue
401+
has_activity = True
396402
# it should have at least one activity that matches it's type to qualify for the Resource trait
397403
for fqan, iot in activities.iteritems():
398404
_, resource, _ = activity_split(fqan)
399405
if resource and activity_name_to_type_name(resource).lower() == sid.lower():
400406
res.add('cmn::%s' % RESOURCE_MARKER_TRAIT)
407+
m = c.fqan_map[fqan]
408+
params, _ = build_all_params(c, m)
409+
part_prop, _ = parts_from_params(params)
410+
if part_prop is not None and 'properties' in s:
411+
res.add(TO_PARTS_MARKER)
401412
if IO_RESPONSE in iot:
402413
res.add(RESPONSE_MARKER_TRAIT)
403414
if IO_REQUEST in iot:
@@ -412,8 +423,8 @@ def schema_markers(s, c, transitive=True):
412423
if len(c.sta_map.get(s.id, dict())) == 0:
413424
res.add(PART_MARKER_TRAIT)
414425

415-
if len(res) == 1 and PART_MARKER_TRAIT in res and len(s.used_by) == 0:
416-
res.add('UnusedType')
426+
if not has_activity:
427+
res.add(UNUSED_TYPE_MARKER)
417428

418429
return sorted(res)
419430

src/rust/cmn.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ pub trait NestedType: MarkerTrait {}
4848
pub trait ReadSeek: Seek + Read {}
4949
impl<T: Seek + Read> ReadSeek for T {}
5050

51+
/// A trait for all types that can convert themselves into a *parts* string
52+
pub trait ToParts {
53+
fn to_parts(&self) -> String;
54+
}
55+
5156

5257
/// A utility type which can decode a server response that indicates error
5358
#[derive(Deserialize)]
@@ -96,7 +101,10 @@ pub trait Delegate {
96101
/// Called whenever a server response could not be decoded from json.
97102
/// It's for informational purposes only, the caller will return with an error
98103
/// accordingly.
99-
fn response_json_decode_error(&mut self, json_encoded_value: &str) {}
104+
/// # Arguments
105+
/// `&str` - The json-encoded value which failed to decode.
106+
/// `Error` - The decoder error
107+
fn response_json_decode_error(&mut self, _: &str, _: &serde::json::Error) {}
100108

101109
/// Called whenever the http request returns with a non-success status code.
102110
/// This can involve authentication issues, or anything else that very much

0 commit comments

Comments
 (0)