Prefer iterators
@@ -329,14 +329,12 @@ impl WebOperationGen {
})
}
- pub fn success_responses(&self) -> IndexMap<StatusCode, Response> {
- let mut map = IndexMap::new();
- for (status_code, rsp) in &self.0.responses {
- if crate::status_codes::is_success(status_code) {
- map.insert(status_code.to_owned(), rsp.to_owned());
- }
- }
- map
+ pub fn success_responses(&self) -> IndexMap<&StatusCode, &Response> {
+ self.0
+ .responses
+ .iter()
+ .filter(|(status_code, _)| crate::status_codes::is_success(status_code))
+ .collect()
}
}
@@ -780,23 +778,27 @@ struct StatusResponseCode {
impl ResponseCode {
fn new(operation: &WebOperationGen, produces: String) -> Result<Self> {
- let mut status_responses = Vec::new();
let success_responses = operation.success_responses();
- for (status_code, rsp) in &success_responses {
- status_responses.push(StatusResponseCode {
+ let status_responses = success_responses
+ .iter()
+ .map(|(status_code, rsp)| {
+ Ok(StatusResponseCode {
status_code_name: get_status_code_ident(status_code)?,
response_type: create_response_type(rsp)?,
- });
- }
+ })
+ })
+ .collect::<Result<Vec<_>>>()?;
+
let headers = success_responses
- .into_iter()
- .flat_map(|(_, rsp)| rsp.headers)
+ .iter()
+ .flat_map(|(_, rsp)| &rsp.headers)
.filter_map(|(name, header)| match header {
- autorust_openapi::ReferenceOr::Item(header) => Some((name.clone(), HeaderCode::new(name, &header))),
+ autorust_openapi::ReferenceOr::Item(header) => Some((name.clone(), HeaderCode::new(name.clone(), header))),
_ => None,
})
- .collect::<IndexMap<_, _>>();
- let headers = headers.into_values().collect::<Result<Vec<_>>>()?;
+ .collect::<IndexMap<_, _>>()
+ .into_values()
+ .collect::<Result<Vec<_>>>()?;
Ok(Self {
status_responses,
pageable: operation.pageable(),
