Skip to content

Commit cb65ed9

Browse files
authored
[DOCS] Document client functions and user {major}.{minor} version in links (#53)
* Document all associated functions * Use major.minor version in doc attribute links This commit updates the api_generator to change references to master or current in documentation links to the major.minor version of the api_generator package version. With this change, documentation links to the correct version for the client version. * Update doc links with /master and /current
1 parent bc05305 commit cb65ed9

File tree

30 files changed

+595
-383
lines changed

30 files changed

+595
-383
lines changed

api_generator/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ reduce = "0.1.2"
1818
regex = "1.3.1"
1919
reqwest = "~0.9"
2020
rustfmt-nightly="1.4.8"
21+
semver = "0.9.0"
2122
serde = "~1"
2223
serde_json = "~1"
2324
serde_derive = "~1"
2425
syn = { version = "~0.11", features = ["full"] }
2526
rayon = "1.2.0"
27+
url = "2.1.1"
2628
void = "1.0.2"

api_generator/src/api_generator/code_gen/request/request_builder.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,12 @@ impl<'a> RequestBuilder<'a> {
483483
endpoint.documentation.url.as_ref(),
484484
) {
485485
(Some(d), Some(u)) if Url::parse(u).is_ok() => lit(format!(
486-
"Builder for the [{} API]({}). {}",
486+
"Builder for the [{} API]({})\n\n{}",
487487
api_name_for_docs, u, d
488488
)),
489-
(Some(d), None) => lit(format!("Builder for the {} API. {}", api_name_for_docs, d)),
489+
(Some(d), None) => lit(format!("Builder for the {} API\n\n{}", api_name_for_docs, d)),
490490
(None, Some(u)) if Url::parse(u).is_ok() => lit(format!(
491-
"Builder for the [{} API]({}).",
491+
"Builder for the [{} API]({})",
492492
api_name_for_docs, u
493493
)),
494494
_ => lit(format!("Builder for the {} API", api_name_for_docs)),
@@ -553,17 +553,27 @@ impl<'a> RequestBuilder<'a> {
553553
}
554554
};
555555

556-
let method_doc = match &endpoint.documentation.description {
557-
Some(description) => Some(doc(description)),
558-
_ => None,
556+
let api_name_for_docs = split_on_pascal_case(builder_name);
557+
let method_doc = match (
558+
endpoint.documentation.description.as_ref(),
559+
endpoint.documentation.url.as_ref(),
560+
) {
561+
(Some(d), Some(u)) if Url::parse(u).is_ok() => doc(format!(
562+
"[{} API]({})\n\n{}",
563+
api_name_for_docs, u, d)
564+
),
565+
(Some(d), None) => doc(format!("{} API\n\n{}", api_name_for_docs, d)),
566+
(None, Some(u)) if Url::parse(u).is_ok() => doc(format!(
567+
"[{} API]({})",
568+
api_name_for_docs, u
569+
)),
570+
_ => doc(format!("{} API", api_name_for_docs)),
559571
};
560572

561-
let clone_expr = {
562-
if is_root_method {
563-
quote!(&self)
564-
} else {
565-
quote!(&self.client)
566-
}
573+
let clone_expr = if is_root_method {
574+
quote!(&self)
575+
} else {
576+
quote!(&self.client)
567577
};
568578

569579
if enum_builder.contains_single_parameterless_part() {
@@ -610,7 +620,7 @@ impl<'a> RequestBuilder<'a> {
610620
}
611621

612622
/// builds the AST that represent the builder structs
613-
/// and the ctor function for the builder struct on the namespace client
623+
/// and the ctor function for the builder struct on the root/namespace client
614624
pub fn build(self) -> (Tokens, Tokens) {
615625
let builder_struct = Self::create_builder_struct(
616626
self.builder_name,

api_generator/src/api_generator/mod.rs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use serde::de::{MapAccess, Visitor};
1717
use std::marker::PhantomData;
1818
use std::str::FromStr;
1919
use void::Void;
20+
use url;
21+
use semver::Version;
2022

2123
mod code_gen;
2224

@@ -146,10 +148,64 @@ pub struct Body {
146148
pub serialize: Option<String>,
147149
}
148150

151+
lazy_static! {
152+
static ref MAJOR_MINOR_VERSION: Version = semver::Version::parse(env!("CARGO_PKG_VERSION")).unwrap();
153+
}
154+
155+
/// Wraps the URL string to replace master or current in URL path with the
156+
/// major.minor version of the api_generator.
157+
fn documentation_url_string<'de, D>(deserializer: D) -> Result<String, D::Error>
158+
where
159+
D: Deserializer<'de>,
160+
{
161+
let s = String::deserialize(deserializer)?;
162+
Ok(DocumentationUrlString::replace_version_in_url(s))
163+
}
164+
165+
/// A Documentation URL string
166+
#[derive(Debug, Deserialize, PartialEq, Clone)]
167+
pub struct DocumentationUrlString(#[serde(deserialize_with = "documentation_url_string")] pub String);
168+
169+
impl DocumentationUrlString {
170+
fn from_url(s: String) -> Self {
171+
let s = Self::replace_version_in_url(s);
172+
Self(s)
173+
}
174+
175+
fn replace_version_in_url(s: String) -> String {
176+
match url::Url::parse(&s) {
177+
Ok(u) => {
178+
let mut u = u;
179+
if u.path().contains("/master") {
180+
u.set_path(u.path().replace("/master", format!("/{}.{}", MAJOR_MINOR_VERSION.major, MAJOR_MINOR_VERSION.minor).as_str()).as_str());
181+
} else if u.path().contains("/current") {
182+
u.set_path(u.path().replace("/current", format!("/{}.{}", MAJOR_MINOR_VERSION.major, MAJOR_MINOR_VERSION.minor).as_str()).as_str());
183+
}
184+
u.into_string()
185+
},
186+
Err(_) => s
187+
}
188+
}
189+
}
190+
191+
impl core::ops::Deref for DocumentationUrlString {
192+
type Target = String;
193+
194+
fn deref(self: &'_ Self) -> &'_ Self::Target {
195+
&self.0
196+
}
197+
}
198+
199+
impl fmt::Display for DocumentationUrlString {
200+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
201+
write!(f, "{}", self.0)
202+
}
203+
}
204+
149205
/// Documentation for an API endpoint
150206
#[derive(Debug, PartialEq, Deserialize, Clone)]
151207
pub struct Documentation {
152-
pub url: Option<String>,
208+
pub url: Option<DocumentationUrlString>,
153209
pub description: Option<String>,
154210
}
155211

@@ -158,7 +214,7 @@ impl FromStr for Documentation {
158214

159215
fn from_str(s: &str) -> Result<Self, Self::Err> {
160216
Ok(Documentation {
161-
url: Some(s.to_string()),
217+
url: Some(DocumentationUrlString::from_url(s.to_owned())),
162218
description: None,
163219
})
164220
}

0 commit comments

Comments
 (0)