Skip to content

Commit 6d03297

Browse files
authored
simplify use of default_tag (#1506)
Most of the time, tag is analogous to "API version", but not always. The idea is to allow the following: * Allow the generated crate user to specify which tags they want if they want something specific * Allow the SDK developer to rely on the "usual" behavior where it's API version, and use the "latest" without having to continuously update the SDK crate based on a steady churn of the generated crates * When user uses both the generated crate and the SDK-crate, it doesn't violate either of the above ideals * Allow users to use both generated crates and the SDK-crate _without_ multiple imports of the same crate as the generated crates can be huge. (For reference, azure_svc_blobstorage is ~5M of source, and each feature is ~1M) This change modifies the generated import behavior thusly: * Any tag that is selected by feature name is available for use via `use crate_name::tag_name::models;` * If the `default_tag` feature is selected, then the tag is included by feature name (and can be used via `use crate_name::tag_name::models;` * If the `default_tag` feature is selected, then the implementation for that specific tag is imported at the top level for the crate, such that it can _also_ be used as `use crate_name::models;`
1 parent 85d13b3 commit 6d03297

File tree

579 files changed

+1691
-3344
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

579 files changed

+1691
-3344
lines changed

services/autorust/codegen/src/gen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub fn gen_crate(package_name: &str, spec: &SpecReadme, run_config: &RunConfig,
9090
let default_tag = cargo_toml::get_default_tag(tags, default_tag_name);
9191

9292
cargo_toml::create(package_name, tags, default_tag, has_xml, &cargo_toml_path).context(ErrorKind::CodeGen, "cargo_toml::create")?;
93-
lib_rs::create(tags, lib_rs_path, false).context(ErrorKind::CodeGen, "lib_rs::create")?;
93+
lib_rs::create(tags, default_tag, lib_rs_path, false).context(ErrorKind::CodeGen, "lib_rs::create")?;
9494
let readme = ReadmeMd {
9595
package_name,
9696
readme_url: readme_md::url(spec.readme().as_str()),

services/autorust/codegen/src/lib_rs.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,54 @@ use crate::{ErrorKind, Result, ResultExt};
33
use camino::Utf8Path;
44
use proc_macro2::{Ident, TokenStream};
55
use quote::{quote, ToTokens};
6+
use std::convert::{TryFrom, TryInto};
67

7-
pub fn create(tags: &[&Tag], path: &Utf8Path, print_writing_file: bool) -> Result<()> {
8-
write_file(path, &create_body(tags)?.into_token_stream(), print_writing_file)
8+
pub fn create(tags: &[&Tag], default_tag: &Tag, path: &Utf8Path, print_writing_file: bool) -> Result<()> {
9+
write_file(path, &create_body(tags, default_tag)?.into_token_stream(), print_writing_file)
910
}
1011

1112
struct Feature {
1213
pub feature_name: String,
1314
pub mod_name: Ident,
1415
}
1516

17+
impl TryFrom<&&Tag> for Feature {
18+
type Error = crate::Error;
19+
fn try_from(tag: &&Tag) -> Result<Self> {
20+
let feature_name = tag.rust_feature_name();
21+
let mod_name = parse_ident(&tag.rust_mod_name()).context(ErrorKind::Parse, "mod name")?;
22+
Ok(Feature { feature_name, mod_name })
23+
}
24+
}
25+
1626
struct BodyCode {
27+
pub default: Feature,
1728
pub features: Vec<Feature>,
1829
}
1930

20-
fn create_body(tags: &[&Tag]) -> Result<BodyCode> {
21-
let features: Vec<Feature> = tags
22-
.iter()
23-
.map(|tag| {
24-
let feature_name = tag.rust_feature_name();
25-
let mod_name = parse_ident(&tag.rust_mod_name()).context(ErrorKind::Parse, "mod name")?;
26-
Ok(Feature { feature_name, mod_name })
27-
})
28-
.collect::<Result<_>>()?;
29-
Ok(BodyCode { features })
31+
fn create_body(tags: &[&Tag], default_tag: &Tag) -> Result<BodyCode> {
32+
let features: Vec<Feature> = tags.iter().map(|tag| tag.try_into()).collect::<Result<_>>()?;
33+
let default = (&default_tag).try_into()?;
34+
35+
Ok(BodyCode { features, default })
3036
}
3137

3238
impl ToTokens for BodyCode {
3339
fn to_tokens(&self, tokens: &mut TokenStream) {
3440
let mut cfgs = TokenStream::new();
41+
3542
for feature in &self.features {
36-
let feature_name = &feature.feature_name;
37-
let mod_name = &feature.mod_name;
43+
let Feature { feature_name, mod_name } = feature;
3844
cfgs.extend(quote! {
3945
#[cfg(feature = #feature_name)]
4046
pub mod #mod_name;
41-
#[cfg(all(feature = #feature_name, not(feature = "without_tag_import")))]
47+
});
48+
}
49+
50+
{
51+
let Feature { feature_name, mod_name } = &self.default;
52+
cfgs.extend(quote! {
53+
#[cfg(all(feature="default_tag", feature = #feature_name))]
4254
pub use #mod_name::*;
4355
});
4456
}

services/autorust/codegen/templates/Cargo.toml.jinja

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ all-features = true
3838
default = ["default_tag", "enable_reqwest"]
3939
enable_reqwest = ["azure_core/enable_reqwest"]
4040
enable_reqwest_rustls = ["azure_core/enable_reqwest_rustls"]
41-
without_tag_import = []
4241
default_tag = ["{{default_tag}}"]
4342
{%- for feature in features %}
4443
"{{feature}}" = []

services/mgmt/activedirectory/Cargo.toml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

services/mgmt/activedirectory/src/lib.rs

Lines changed: 9 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

services/mgmt/addons/Cargo.toml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

services/mgmt/addons/src/lib.rs

Lines changed: 3 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

services/mgmt/adhybridhealthservice/Cargo.toml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

services/mgmt/adhybridhealthservice/src/lib.rs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

services/mgmt/adp/Cargo.toml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)