Skip to content

Commit e96260b

Browse files
committed
feat(visuals): defs are now more readable
This works with a new `indent` and `unindent` filters respectively. There are a few things to consider, but I have understood how it works and can handle it. There is some overhead just to give me nicer visuals ... might choose a different route, like annotations.
1 parent 615a124 commit e96260b

5 files changed

Lines changed: 80 additions & 73 deletions

File tree

gen/youtube3/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ let mut hub = YouTube::new(hyper::Client::new(), auth);
100100
[builder-pattern]: http://en.wikipedia.org/wiki/Builder_pattern
101101
[google-go-api]: https://github.com/google/google-api-go-client
102102

103-
104103
# License
105104
The **youtube3** library was generated by Sebastian Thiel, and is placed
106105
under the *MIT* license.

gen/youtube3/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@
100100
//! [google-go-api]: https://github.com/google/google-api-go-client
101101
//!
102102
//!
103-
//!
104103
#![feature(core)]
105104
#![allow(non_snake_case)]
106105

@@ -147,8 +146,8 @@ pub use cmn::{Hub, Resource, Part, ResponseResult, RequestResult, NestedType};
147146
/// // Provide your own `AuthenticatorDelegate` to adjust the way it operates and get feedback about what's going on
148147
/// // You probably want to bring in your own `TokenStorage` to persist tokens and retrieve them from storage.
149148
/// let auth = Authenticator::new(&secret, DefaultAuthenticatorDelegate,
150-
/// hyper::Client::new(),
151-
/// <MemoryStorage as Default>::default(), None);
149+
/// hyper::Client::new(),
150+
/// <MemoryStorage as Default>::default(), None);
152151
/// let mut hub = YouTube::new(hyper::Client::new(), auth);
153152
/// # }
154153
/// ```

src/mako/lib/lib.mako

Lines changed: 64 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<%! from util import (activity_split, put_and, md_italic, split_camelcase_s, canonical_type_name,
2-
rust_test_fn_invisible, rust_doc_test_norun, rust_doc_comment, markdown_rust_block) %>\
2+
rust_test_fn_invisible, rust_doc_test_norun, rust_doc_comment, markdown_rust_block,
3+
unindent, indent) %>\
34
<%namespace name="util" file="util.mako"/>\
45
56
## If rust-doc is True, examples will be made to work for rust doc tests. Otherwise they are set
67
## for github markdown.
7-
<%def name="docs(c, rust_doc=True)">\
8+
<%def name="docs(c, rust_doc=True)" filter="unindent">\
89
<%
910
# fr == fattest resource, the fatter, the more important, right ?
1011
fr = None
@@ -16,86 +17,83 @@
1617
resource, activity = activity_split(an)
1718
amap.setdefault(resource, list()).append(activity)
1819
%>\
19-
# Features
20+
# Features
2021

21-
Handle the following *Resources* with ease ...
22+
Handle the following *Resources* with ease ...
2223

23-
% for r in sorted(amap.keys()):
24-
* ${split_camelcase_s(r)} (${put_and(md_italic(sorted(amap[r])))})
25-
% endfor
24+
% for r in sorted(amap.keys()):
25+
* ${split_camelcase_s(r)} (${put_and(md_italic(sorted(amap[r])))})
26+
% endfor
2627

27-
# Structure of this Library
28+
# Structure of this Library
2829

29-
The API is structured into the following primary items:
30+
The API is structured into the following primary items:
3031

31-
* **Hub**
32-
* a central object to maintain state and allow accessing all *Activities*
33-
* **Resources**
34-
* primary types that you can apply *Activities* to
35-
* a collection of properties and *Parts*
36-
* **Parts**
37-
* a collection of properties
38-
* never directly used in *Activities*
39-
* **Activities**
40-
* operations to apply to *Resources*
32+
* **Hub**
33+
* a central object to maintain state and allow accessing all *Activities*
34+
* **Resources**
35+
* primary types that you can apply *Activities* to
36+
* a collection of properties and *Parts*
37+
* **Parts**
38+
* a collection of properties
39+
* never directly used in *Activities*
40+
* **Activities**
41+
* operations to apply to *Resources*
4142

42-
Generally speaking, you can invoke *Activities* like this:
43+
Generally speaking, you can invoke *Activities* like this:
4344

44-
```Rust,ignore
45-
let r = hub.resource().activity(...).${api.terms.action}()
46-
```
45+
```Rust,ignore
46+
let r = hub.resource().activity(...).${api.terms.action}()
47+
```
4748

48-
Or specifically ...
49+
Or specifically ...
4950

50-
```ignore
51+
```ignore
5152
% for an, a in c.sta_map[fr.id].iteritems():
52-
<%
53-
resource, activity = activity_split(an)
54-
%>\
55-
let r = hub.${resource}().${activity}(...).${api.terms.action}()
53+
<% resource, activity = activity_split(an) %>\
54+
let r = hub.${resource}().${activity}(...).${api.terms.action}()
5655
% endfor
57-
```
58-
59-
The `resource()` and `activity(...)` calls create [builders][builder-pattern]. The second one dealing with `Activities`
60-
supports various methods to configure the impending operation (not shown here). It is made such that all required arguments have to be
61-
specified right away (i.e. `(...)`), whereas all optional ones can be [build up][builder-pattern] as desired.
62-
The `${api.terms.action}()` method performs the actual communication with the server and returns the respective result.
56+
```
6357

64-
# Usage (*TODO*)
58+
The `resource()` and `activity(...)` calls create [builders][builder-pattern]. The second one dealing with `Activities`
59+
supports various methods to configure the impending operation (not shown here). It is made such that all required arguments have to be
60+
specified right away (i.e. `(...)`), whereas all optional ones can be [build up][builder-pattern] as desired.
61+
The `${api.terms.action}()` method performs the actual communication with the server and returns the respective result.
6562

66-
${'##'} Instantiating the Hub
63+
# Usage (*TODO*)
6764

68-
${self.hub_usage_example(rust_doc)}\
65+
${'##'} Instantiating the Hub
6966

70-
**TODO** Example calls - there should soon be a generator able to do that with proper inputs
71-
${'##'} About error handling
67+
${self.hub_usage_example(rust_doc) | indent}\
7268

73-
${'##'} About Customization/Callbacks
69+
**TODO** Example calls - there should soon be a generator able to do that with proper inputs
70+
${'##'} About error handling
7471

75-
[builder-pattern]: http://en.wikipedia.org/wiki/Builder_pattern
76-
[google-go-api]: https://github.com/google/google-api-go-client
72+
${'##'} About Customization/Callbacks
7773

74+
[builder-pattern]: http://en.wikipedia.org/wiki/Builder_pattern
75+
[google-go-api]: https://github.com/google/google-api-go-client
7876
</%def>
7977

8078
## Sets up a hub ready for use. You must wrap it into a test function for it to work
8179
## Needs test_prelude.
82-
<%def name="test_hub(hub_type)">\
83-
use oauth2::{Authenticator, DefaultAuthenticatorDelegate, ApplicationSecret, MemoryStorage};
84-
use std::default::Default;
85-
86-
use ${util.library_name()}::${hub_type};
87-
88-
// Get an ApplicationSecret instance by some means. It contains the `client_id` and `client_secret`,
89-
// among other things.
90-
let secret: ApplicationSecret = Default::default();
91-
// Instantiate the authenticator. It will choose a suitable authentication flow for you,
92-
// unless you replace `None` with the desired Flow
93-
// Provide your own `AuthenticatorDelegate` to adjust the way it operates and get feedback about what's going on
94-
// You probably want to bring in your own `TokenStorage` to persist tokens and retrieve them from storage.
95-
let auth = Authenticator::new(&secret, DefaultAuthenticatorDelegate,
96-
hyper::Client::new(),
97-
<MemoryStorage as Default>::default(), None);
98-
let mut hub = ${hub_type}::new(hyper::Client::new(), auth);\
80+
<%def name="test_hub(hub_type)" filter="unindent">\
81+
use oauth2::{Authenticator, DefaultAuthenticatorDelegate, ApplicationSecret, MemoryStorage};
82+
use std::default::Default;
83+
84+
use ${util.library_name()}::${hub_type};
85+
86+
// Get an ApplicationSecret instance by some means. It contains the `client_id` and `client_secret`,
87+
// among other things.
88+
let secret: ApplicationSecret = Default::default();
89+
// Instantiate the authenticator. It will choose a suitable authentication flow for you,
90+
// unless you replace `None` with the desired Flow
91+
// Provide your own `AuthenticatorDelegate` to adjust the way it operates and get feedback about what's going on
92+
// You probably want to bring in your own `TokenStorage` to persist tokens and retrieve them from storage.
93+
let auth = Authenticator::new(&secret, DefaultAuthenticatorDelegate,
94+
hyper::Client::new(),
95+
<MemoryStorage as Default>::default(), None);
96+
let mut hub = ${hub_type}::new(hyper::Client::new(), auth);\
9997
</%def>
10098

10199
## You will still have to set the filter for your comment type - either nothing, or rust_doc_comment !
@@ -116,11 +114,11 @@ ${self.test_hub(canonical_type_name(canonicalName))}\
116114
</%block>
117115
</%def>
118116

119-
<%def name="license()">\
120-
# License
121-
The **${util.library_name()}** library was generated by ${put_and(copyright.authors)}, and is placed
122-
under the *${copyright.license_abbrev}* license.
123-
You can read the full text at the repository's [license file][repo-license].
117+
<%def name="license()" filter="unindent">\
118+
# License
119+
The **${util.library_name()}** library was generated by ${put_and(copyright.authors)}, and is placed
120+
under the *${copyright.license_abbrev}* license.
121+
You can read the full text at the repository's [license file][repo-license].
124122
125-
[repo-license]: ${cargo.repo_base_url + 'LICENSE.md'}
123+
[repo-license]: ${cargo.repo_base_url + 'LICENSE.md'}
126124
</%def>

src/mako/lib/util.mako

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ DO NOT EDIT !\
99
</%def>
1010
1111
## This will only work within a substitution, not within python code
12-
<%def name="to_api_version(v)">\
12+
<%def name="to_api_version(v)" buffered="True">\
1313
<% assert len(v) >= 2 and v[0] == 'v'%>\
1414
## convert it once to int, just to be sure it is an int
1515
${v[1:]}\
1616
</%def>
1717

18-
<%def name="repository_url()">\
18+
<%def name="repository_url()" buffered="True">\
1919
${cargo.repo_base_url}/${OUTPUT_DIR}\
2020
</%def>
2121

22-
<%def name="library_name()">\
22+
<%def name="library_name()" buffered="True">\
2323
${util.library_name(name, version)}\
2424
</%def>
2525

src/mako/lib/util.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import collections
33

44
re_linestart = re.compile('^', flags=re.MULTILINE)
5+
re_first_4_spaces = re.compile('^ ', flags=re.MULTILINE)
56

67
USE_FORMAT = 'use_format_field'
78
TYPE_MAP = {'boolean' : 'bool',
@@ -43,6 +44,16 @@ def rust_comment(s):
4344
def hash_comment(s):
4445
return re_linestart.sub('# ', s)
4546

47+
# remove the first indentation (must be spaces !)
48+
def unindent(s):
49+
return re_first_4_spaces.sub('', s)
50+
51+
# add 4 spaces to the beginning of a line.
52+
# useful if you have defs embedded in an unindent block - they need to counteract.
53+
# It's a bit itchy, but logical
54+
def indent(s):
55+
return re_linestart.sub(' ', s)
56+
4657
# return s, with trailing newline
4758
def trailing_newline(s):
4859
if not s.endswith('\n'):

0 commit comments

Comments
 (0)