1use crate::dirs::crate_prefix;
2use serde_derive::Deserialize;
3
4#[derive(Clone, Debug, Deserialize)]
6pub struct IndexConfig {
7 pub dl: String,
9 pub api: Option<String>,
11}
12
13impl IndexConfig {
14 #[must_use]
18 pub fn download_url(&self, name: &str, version: &str) -> Option<String> {
19 if !self.dl.contains("{crate}")
20 && !self.dl.contains("{version}")
21 && !self.dl.contains("{prefix}")
22 && !self.dl.contains("{lowerprefix}")
23 {
24 let mut new = String::with_capacity(self.dl.len() + name.len() + version.len() + 10);
25 new.push_str(&self.dl);
26 new.push('/');
27 new.push_str(name);
28 new.push('/');
29 new.push_str(version);
30 new.push_str("/download");
31 Some(new)
32 } else {
33 let mut prefix = String::with_capacity(5);
34 crate_prefix(&mut prefix, name, '/')?;
35 Some(
36 self.dl
37 .replace("{crate}", name)
38 .replace("{version}", version)
39 .replace("{prefix}", &prefix)
40 .replace("{lowerprefix}", &prefix.to_ascii_lowercase()),
41 )
42 }
43 }
44}