use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IndexConfig {
pub dl: String,
pub api: String,
#[serde(rename = "auth-required", default)]
pub auth_required: bool,
}
impl IndexConfig {
#[must_use]
pub fn new(api_host: impl Into<String>) -> Self {
Self {
dl: "/api/v1/crates/{crate}/{version}/download".to_owned(),
api: api_host.into(),
auth_required: false,
}
}
}
#[cfg(test)]
mod tests {
use super::IndexConfig;
#[test]
fn config_json_round_trip() {
let c = IndexConfig::new("http://localhost:8080");
let s = serde_json::to_string(&c).unwrap();
let c2: IndexConfig = serde_json::from_str(&s).unwrap();
assert_eq!(c, c2);
}
#[test]
fn dl_template_matches_spec() {
let c = IndexConfig::new("x");
assert!(c.dl.contains("{crate}"));
assert!(c.dl.contains("{version}"));
}
}