Skip to content

Commit 462f62a

Browse files
committed
cargo fmt
1 parent 22fdb0e commit 462f62a

31 files changed

Lines changed: 539 additions & 162 deletions

autorust/codegen/src/autorust_toml.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ impl<'a> PackageConfig {
116116
let deny: HashSet<&str> = self.tags.deny.iter().map(String::as_str).collect();
117117
tags.retain(|tag| !deny.contains(tag.name()));
118118
}
119-
let mut deny_contains: Vec<&str> = self.tags.deny_contains.iter().map(String::as_str).collect();
119+
let mut deny_contains: Vec<&str> =
120+
self.tags.deny_contains.iter().map(String::as_str).collect();
120121
if self.tags.deny_contains_preview.unwrap_or_default() {
121122
deny_contains.push("preview");
122123
}
@@ -325,7 +326,10 @@ mod tests {
325326
default = "package-resources-2021-04"
326327
"#,
327328
)?;
328-
assert_eq!(Some("package-resources-2021-04".to_string()), config.tags.default);
329+
assert_eq!(
330+
Some("package-resources-2021-04".to_string()),
331+
config.tags.default
332+
);
329333
Ok(())
330334
}
331335

autorust/codegen/src/cargo_toml.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ use crate::Result;
22
use crate::{config_parser::Tag, jinja::CargoToml};
33
use camino::Utf8Path;
44

5-
pub fn create(package_name: &str, tags: &[&Tag], default_tag: &Tag, has_xml: bool, path: &Utf8Path) -> Result<()> {
5+
pub fn create(
6+
package_name: &str,
7+
tags: &[&Tag],
8+
default_tag: &Tag,
9+
has_xml: bool,
10+
path: &Utf8Path,
11+
) -> Result<()> {
612
let default_tag = &default_tag.rust_feature_name();
713

814
// https://docs.rs/about/metadata
@@ -22,7 +28,9 @@ pub fn create(package_name: &str, tags: &[&Tag], default_tag: &Tag, has_xml: boo
2228

2329
pub fn get_default_tag<'a>(tags: &[&'a Tag], default_tag: Option<&str>) -> &'a Tag {
2430
let default_tag = tags.iter().find(|tag| Some(tag.name()) == default_tag);
25-
let is_preview = default_tag.map(|tag| tag.name().contains("preview")).unwrap_or_default();
31+
let is_preview = default_tag
32+
.map(|tag| tag.name().contains("preview"))
33+
.unwrap_or_default();
2634
let stable_tag = tags.iter().find(|tag| !tag.name().contains("preview"));
2735
match (default_tag, is_preview, stable_tag) {
2836
(Some(default_tag), false, _) => default_tag,
@@ -94,13 +102,20 @@ mod tests {
94102
];
95103
let tags: Vec<_> = tags.into_iter().map(Tag::new).collect();
96104
let tags: Vec<_> = tags.iter().collect();
97-
assert_eq!("package-2020-04", get_default_tag(&tags, Some("package-2020-04")).name());
105+
assert_eq!(
106+
"package-2020-04",
107+
get_default_tag(&tags, Some("package-2020-04")).name()
108+
);
98109
Ok(())
99110
}
100111

101112
#[test]
102113
fn specified_preview() -> Result<()> {
103-
let tags = vec!["package-preview-2022-05", "package-2019-06-preview", "package-2019-04-preview"];
114+
let tags = vec![
115+
"package-preview-2022-05",
116+
"package-2019-06-preview",
117+
"package-2019-04-preview",
118+
];
104119
let tags: Vec<_> = tags.into_iter().map(Tag::new).collect();
105120
let tags: Vec<_> = tags.iter().collect();
106121
assert_eq!(

autorust/codegen/src/codegen_operations/function_code.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ pub(crate) struct ClientFunctionCode {
1717
}
1818

1919
impl ClientFunctionCode {
20-
pub fn new(operation: &WebOperationGen, parameters: &FunctionParams, in_operation_group: bool) -> Result<Self> {
20+
pub fn new(
21+
operation: &WebOperationGen,
22+
parameters: &FunctionParams,
23+
in_operation_group: bool,
24+
) -> Result<Self> {
2125
let fname = operation.function_name()?;
2226
let summary = operation.0.summary.clone();
2327
let description = operation.0.description.clone();
@@ -41,7 +45,9 @@ impl ToTokens for ClientFunctionCode {
4145
}
4246
for param in self.parameters.required_params() {
4347
let FunctionParam {
44-
variable_name, type_name, ..
48+
variable_name,
49+
type_name,
50+
..
4551
} = param;
4652
let mut type_name = type_name.clone();
4753
let is_vec = type_name.is_vec();
@@ -54,7 +60,9 @@ impl ToTokens for ClientFunctionCode {
5460
}
5561
for param in self.parameters.optional_params() {
5662
let FunctionParam {
57-
variable_name, type_name, ..
63+
variable_name,
64+
type_name,
65+
..
5866
} = param;
5967
if type_name.is_vec() {
6068
params.push(quote! { #variable_name: Vec::new() });

autorust/codegen/src/codegen_operations/operation_module.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ use proc_macro2::{Ident, TokenStream};
22
use quote::{quote, ToTokens};
33

44
use super::{
5-
request_builder_into_future::RequestBuilderIntoFutureCode, request_builder_send::RequestBuilderSendCode,
6-
request_builder_setter::RequestBuilderSettersCode, request_builder_struct::RequestBuilderStructCode, response_code::ResponseCode,
5+
request_builder_into_future::RequestBuilderIntoFutureCode,
6+
request_builder_send::RequestBuilderSendCode,
7+
request_builder_setter::RequestBuilderSettersCode,
8+
request_builder_struct::RequestBuilderStructCode, response_code::ResponseCode,
79
};
810
pub struct OperationModuleCode {
911
pub module_name: Ident,

autorust/codegen/src/codegen_operations/operations.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,36 @@ impl OperationCode {
3434
// get the content-types from the operation, else the spec, else default to json
3535
let consumes = operation
3636
.pick_consumes()
37-
.unwrap_or_else(|| cg.spec.pick_consumes().unwrap_or(content_type::APPLICATION_JSON))
37+
.unwrap_or_else(|| {
38+
cg.spec
39+
.pick_consumes()
40+
.unwrap_or(content_type::APPLICATION_JSON)
41+
})
3842
.to_string();
3943
let produces = operation
4044
.pick_produces()
41-
.unwrap_or_else(|| cg.spec.pick_produces().unwrap_or(content_type::APPLICATION_JSON))
45+
.unwrap_or_else(|| {
46+
cg.spec
47+
.pick_produces()
48+
.unwrap_or(content_type::APPLICATION_JSON)
49+
})
4250
.to_string();
4351

4452
let lro = operation.0.long_running_operation;
4553
let lro_options = operation.0.long_running_operation_options.clone();
4654

4755
let request_builder = SetRequestCode::new(operation, parameters, consumes);
4856
let in_operation_group = operation.0.in_group();
49-
let client_function_code = ClientFunctionCode::new(operation, parameters, in_operation_group)?;
50-
let request_builder_struct_code = RequestBuilderStructCode::new(parameters, in_operation_group, lro, lro_options.clone());
57+
let client_function_code =
58+
ClientFunctionCode::new(operation, parameters, in_operation_group)?;
59+
let request_builder_struct_code =
60+
RequestBuilderStructCode::new(parameters, in_operation_group, lro, lro_options.clone());
5161
let request_builder_setters_code = RequestBuilderSettersCode::new(parameters);
5262
let response_code = ResponseCode::new(cg, operation, produces)?;
53-
let request_builder_send_code = RequestBuilderSendCode::new(new_request_code, request_builder, response_code.clone())?;
54-
let request_builder_intofuture_code = RequestBuilderIntoFutureCode::new(response_code.clone(), lro, lro_options)?;
63+
let request_builder_send_code =
64+
RequestBuilderSendCode::new(new_request_code, request_builder, response_code.clone())?;
65+
let request_builder_intofuture_code =
66+
RequestBuilderIntoFutureCode::new(response_code.clone(), lro, lro_options)?;
5567

5668
let module_code = OperationModuleCode {
5769
module_name: operation.function_name()?,

autorust/codegen/src/codegen_operations/request_builder_setter.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ impl ToTokens for RequestBuilderSettersCode {
2121
fn to_tokens(&self, tokens: &mut TokenStream) {
2222
for param in self.parameters.optional_params() {
2323
let FunctionParam {
24-
variable_name, type_name, ..
24+
variable_name,
25+
type_name,
26+
..
2527
} = param;
2628
let is_vec = type_name.is_vec();
2729
let mut type_name = type_name.clone();

autorust/codegen/src/codegen_operations/web_operation_gen.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ pub struct Pageable {
143143
/// Creating a function name from the path and verb when an operationId is not specified.
144144
/// All azure-rest-api-specs operations should have an operationId.
145145
fn create_function_name(verb: &WebVerb, path: &str) -> String {
146-
let mut path = path.split('/').filter(|&x| !x.is_empty()).collect::<Vec<_>>();
146+
let mut path = path
147+
.split('/')
148+
.filter(|&x| !x.is_empty())
149+
.collect::<Vec<_>>();
147150
path.insert(0, verb.as_str());
148151
path.join("_")
149152
}
@@ -165,7 +168,10 @@ mod tests {
165168
verb: WebVerb::Get,
166169
..Default::default()
167170
});
168-
assert_eq!(Some("private_clouds".to_owned()), operation.rust_module_name());
171+
assert_eq!(
172+
Some("private_clouds".to_owned()),
173+
operation.rust_module_name()
174+
);
169175
assert_eq!("create_or_update", operation.rust_function_name());
170176
}
171177

autorust/codegen/src/config_parser.rs

Lines changed: 85 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,21 @@ impl Tag {
7777
/// Receives the AutoRest configuration file and parses it to its various configurations (by tags/API versions),
7878
/// according to its extension.
7979
/// e.g. for "path/to/config.md", it will get parsed as CommonMark [Literate Tag](http://azure.github.io/autorest/user/literate-file-formats/configuration.html).
80-
pub fn parse_configurations_from_autorest_config_file(config_file: &Utf8Path) -> Result<Configuration> {
81-
let extension = config_file
82-
.extension()
83-
.ok_or_else(|| Error::with_message(ErrorKind::Parse, || format!("expected md extension {config_file}")))?;
80+
pub fn parse_configurations_from_autorest_config_file(
81+
config_file: &Utf8Path,
82+
) -> Result<Configuration> {
83+
let extension = config_file.extension().ok_or_else(|| {
84+
Error::with_message(ErrorKind::Parse, || {
85+
format!("expected md extension {config_file}")
86+
})
87+
})?;
8488
match extension.to_lowercase().as_str() {
8589
"md" => {
8690
use literate_config::*;
87-
let cmark_content =
88-
std::fs::read_to_string(config_file).with_context(ErrorKind::Io, || format!("reading the md file {config_file}"))?;
91+
let cmark_content = std::fs::read_to_string(config_file)
92+
.with_context(ErrorKind::Io, || {
93+
format!("reading the md file {config_file}")
94+
})?;
8995
Ok(parse_configuration(&cmark_content)?)
9096
}
9197
_ => Err(Error::with_message(ErrorKind::Io, || {
@@ -118,12 +124,13 @@ mod literate_config {
118124
let root = parse_document(&arena, cmark_content, &ComrakOptions::default());
119125

120126
// Get the AST node corresponding with "## Configuration".
121-
let configuration_heading_node = get_configuration_section_heading_node(root).ok_or_else(|| {
122-
Error::message(
123-
ErrorKind::Parse,
124-
"no `## Configuration` heading in the AutoRest literate configuration file",
125-
)
126-
})?;
127+
let configuration_heading_node =
128+
get_configuration_section_heading_node(root).ok_or_else(|| {
129+
Error::message(
130+
ErrorKind::Parse,
131+
"no `## Configuration` heading in the AutoRest literate configuration file",
132+
)
133+
})?;
127134

128135
let mut tags = Vec::new();
129136
let mut basic_info = BasicInformation::default();
@@ -133,15 +140,27 @@ mod literate_config {
133140
let mut current_node = configuration_heading_node.next_sibling();
134141
while let Some(node) = current_node {
135142
if is_basic_information(node) {
136-
let yaml = extract_yaml(node)?
137-
.ok_or_else(|| Error::message(ErrorKind::Parse, "expected configuration tag to contain a YAML code block"))?;
138-
basic_info = serde_yaml::from_str(&yaml).context(ErrorKind::DataConversion, "reading basic information block yaml")?;
143+
let yaml = extract_yaml(node)?.ok_or_else(|| {
144+
Error::message(
145+
ErrorKind::Parse,
146+
"expected configuration tag to contain a YAML code block",
147+
)
148+
})?;
149+
basic_info = serde_yaml::from_str(&yaml).context(
150+
ErrorKind::DataConversion,
151+
"reading basic information block yaml",
152+
)?;
139153
} else if let Some(tag_name) = get_tag_name(node) {
140154
// Extract the configuration from the first node inside the tag heading ("Tag: ..."),
141155
// by looking at the first YAML code block.
142-
let yaml = extract_yaml(node)?
143-
.ok_or_else(|| Error::message(ErrorKind::Parse, "Expected configuration tag to contain a YAML code block."))?;
144-
let mut tag: Tag = serde_yaml::from_str(&yaml).context(ErrorKind::Parse, "reading configuration block yaml")?;
156+
let yaml = extract_yaml(node)?.ok_or_else(|| {
157+
Error::message(
158+
ErrorKind::Parse,
159+
"Expected configuration tag to contain a YAML code block.",
160+
)
161+
})?;
162+
let mut tag: Tag = serde_yaml::from_str(&yaml)
163+
.context(ErrorKind::Parse, "reading configuration block yaml")?;
145164
for input_file in tag.input_files.iter_mut() {
146165
*input_file = input_file.replace('\\', "/");
147166
}
@@ -167,7 +186,9 @@ mod literate_config {
167186
// from https://github.com/kivikakk/comrak/blob/main/examples/headers.rs
168187
fn collect_text<'a>(node: &'a AstNode<'a>, output: &mut String) {
169188
match node.data.borrow().value {
170-
NodeValue::Text(ref literal) | NodeValue::Code(NodeCode { ref literal, .. }) => output.push_str(literal),
189+
NodeValue::Text(ref literal) | NodeValue::Code(NodeCode { ref literal, .. }) => {
190+
output.push_str(literal)
191+
}
171192
NodeValue::LineBreak | NodeValue::SoftBreak => output.push(' '),
172193
_ => {
173194
for n in node.children() {
@@ -179,7 +200,9 @@ mod literate_config {
179200

180201
/// Returns the first "## Configuration" AST Node.
181202
/// There should only be one per Literate Configuration file.
182-
fn get_configuration_section_heading_node<'a>(root: &'a AstNode<'a>) -> Option<&'a AstNode<'a>> {
203+
fn get_configuration_section_heading_node<'a>(
204+
root: &'a AstNode<'a>,
205+
) -> Option<&'a AstNode<'a>> {
183206
root.children().find(|node| {
184207
if is_header_at_level(node, 2) {
185208
let mut text = String::new();
@@ -220,19 +243,33 @@ mod literate_config {
220243
fn extract_yaml<'a>(configuration_tag_heading_node: &'a AstNode<'a>) -> Result<Option<String>> {
221244
let mut current_node = configuration_tag_heading_node
222245
.next_sibling()
223-
.ok_or_else(|| Error::message(ErrorKind::Parse, "markdown ended unexpectedly after configuration tag heading"))?;
246+
.ok_or_else(|| {
247+
Error::message(
248+
ErrorKind::Parse,
249+
"markdown ended unexpectedly after configuration tag heading",
250+
)
251+
})?;
224252
loop {
225-
if let NodeValue::CodeBlock(NodeCodeBlock { info, literal, fenced, .. }) = &current_node.data.borrow().value {
253+
if let NodeValue::CodeBlock(NodeCodeBlock {
254+
info,
255+
literal,
256+
fenced,
257+
..
258+
}) = &current_node.data.borrow().value
259+
{
226260
if !fenced {
227261
continue;
228262
}
229263
if info.trim_start().to_lowercase().starts_with("yaml") {
230264
return Ok(Some(literal.to_owned()));
231265
}
232266
}
233-
current_node = current_node
234-
.next_sibling()
235-
.ok_or_else(|| Error::message(ErrorKind::Parse, "markdown ended unexpectedly after configuration tag heading"))?;
267+
current_node = current_node.next_sibling().ok_or_else(|| {
268+
Error::message(
269+
ErrorKind::Parse,
270+
"markdown ended unexpectedly after configuration tag heading",
271+
)
272+
})?;
236273
}
237274
}
238275
}
@@ -277,7 +314,9 @@ mod tests {
277314
fn test_get_input_file_api_version() {
278315
assert_eq!(
279316
Some("2019-05-05-preview".to_owned()),
280-
get_input_file_api_version("Microsoft.AlertsManagement/preview/2019-05-05-preview/ActionRules.json")
317+
get_input_file_api_version(
318+
"Microsoft.AlertsManagement/preview/2019-05-05-preview/ActionRules.json"
319+
)
281320
);
282321
}
283322

@@ -317,8 +356,14 @@ input-file:
317356
assert_eq!(1, tags.len());
318357
assert_eq!("package-2019-06", tags[0].tag);
319358
assert_eq!(5, tags[0].input_files.len());
320-
assert_eq!("Microsoft.Storage/stable/2019-06-01/storage.json", tags[0].input_files[0]);
321-
assert_eq!("Microsoft.Storage/stable/2019-06-01/blob.json", tags[0].input_files[1]);
359+
assert_eq!(
360+
"Microsoft.Storage/stable/2019-06-01/storage.json",
361+
tags[0].input_files[0]
362+
);
363+
assert_eq!(
364+
"Microsoft.Storage/stable/2019-06-01/blob.json",
365+
tags[0].input_files[1]
366+
);
322367
Ok(())
323368
}
324369

@@ -354,12 +399,21 @@ input-file:
354399
assert_eq!(2, tags.len());
355400
assert_eq!("package-2019-06", tags[0].tag);
356401
assert_eq!(5, tags[0].input_files.len());
357-
assert_eq!("Microsoft.Storage/stable/2019-06-01/storage.json", tags[0].input_files[0]);
358-
assert_eq!("Microsoft.Storage/stable/2019-06-01/blob.json", tags[0].input_files[1]);
402+
assert_eq!(
403+
"Microsoft.Storage/stable/2019-06-01/storage.json",
404+
tags[0].input_files[0]
405+
);
406+
assert_eq!(
407+
"Microsoft.Storage/stable/2019-06-01/blob.json",
408+
tags[0].input_files[1]
409+
);
359410

360411
assert_eq!("package-2015-05-preview", tags[1].tag);
361412
assert_eq!(1, tags[1].input_files.len());
362-
assert_eq!("Microsoft.Storage/preview/2015-05-01-preview/storage.json", tags[1].input_files[0]);
413+
assert_eq!(
414+
"Microsoft.Storage/preview/2015-05-01-preview/storage.json",
415+
tags[1].input_files[0]
416+
);
363417
Ok(())
364418
}
365419

0 commit comments

Comments
 (0)