Skip to content

Commit

Permalink
- refactor: remove extra iteration to generate the ConfigGenReq.
Browse files Browse the repository at this point in the history
  • Loading branch information
laststylebender14 committed May 20, 2024
1 parent e4be0f0 commit eec7c1c
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/core/generator/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,30 +86,24 @@ impl Generator {
.into_iter()
.collect::<anyhow::Result<Vec<_>>>()?;

// create a config generation requests.
let config_gen_reqs = results
.iter()
.zip(paths.iter())
.map(|(resp, url)| ConfigGenerationRequest::new(url.as_ref(), resp))
.collect::<Vec<ConfigGenerationRequest>>();

// group requests with same domain name to have single config.
// and pass each group to from_json to generate the config.
let mut domain_groupings: HashMap<String, Vec<ConfigGenerationRequest>> =
HashMap::new();
for req in config_gen_reqs {
let url = Url::parse(req.url)?;
let domain = url.host_str().unwrap();

// group requests with same domain/host name to have single config.
// and pass each group to from_json to generate the config.
for (resp, url) in results.iter().zip(paths.iter()) {
let parsed_url = Url::parse(url.as_ref())?;
let domain = parsed_url.host_str().unwrap();
domain_groupings
.entry(domain.to_string())
.or_default()
.push(req);
.push(ConfigGenerationRequest::new(url.as_ref(), resp));
}

let mut configs = Vec::with_capacity(domain_groupings.len());

for (_, same_domain_group_req) in domain_groupings {
configs.push(ConfigModule::from(from_json(&same_domain_group_req)?));
for config_gen_req in domain_groupings.values() {
configs.push(ConfigModule::from(from_json(config_gen_req)?));
}

Ok(configs)
Expand Down

0 comments on commit eec7c1c

Please sign in to comment.