-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathcargo_toml.rs
79 lines (71 loc) · 2.15 KB
/
cargo_toml.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::config_parser::Tag;
use std::{
fs::File,
io::{prelude::*, LineWriter},
path::Path,
};
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("IoError")]
IoError { source: std::io::Error },
}
pub fn create(crate_name: &str, tags: &[&Tag], default_tag: Option<&str>, path: &Path) -> Result<()> {
let file = File::create(path).map_err(|source| Error::IoError { source })?;
let mut file = LineWriter::new(file);
let default = get_default_feature(tags, default_tag);
file.write_all(
format!(
r#"# generated by AutoRust
[package]
name = "{}"
version = "0.2.0"
edition = "2018"
license = "MIT"
description = "generated REST API bindings"
[dependencies]
azure_core = {{ path = "../../../sdk/core", version = "0.1", default-features = false }}
serde = {{ version = "1.0", features = ["derive"] }}
serde_json = "1.0"
bytes = "1.0"
thiserror = "1.0"
http = "0.2"
url = "2.2"
futures = "0.3"
[dev-dependencies]
azure_identity = {{ path = "../../../sdk/identity", version = "0.1" }}
tokio = {{ version = "1.0", features = ["macros"] }}
[package.metadata.docs.rs]
all-features = true
[features]
default = ["{}", "enable_reqwest"]
enable_reqwest = ["azure_core/enable_reqwest"]
enable_reqwest_rustls = ["azure_core/enable_reqwest_rustls"]
no-default-version = []
"#,
crate_name, default
)
.as_bytes(),
)
.map_err(|source| Error::IoError { source })?;
for tag in tags {
file.write_all(format!("\"{}\" = []\n", tag.rust_feature_name()).as_bytes())
.map_err(|source| Error::IoError { source })?;
}
Ok(())
}
fn get_default_feature(tags: &[&Tag], default_tag: Option<&str>) -> String {
if let Some(default_tag) = default_tag {
if let Some(tag) = tags.iter().find(|tag| tag.name() == default_tag) {
return tag.rust_feature_name();
}
}
let feature = tags
.iter()
.map(|tag| tag.rust_feature_name())
.find(|feature| !feature.contains("preview"));
match feature {
Some(feature) => feature,
None => tags[0].rust_feature_name(),
}
}