use serde::{Deserialize, Serialize};
pub const PORTAL: &str = "https://mods.factorio.com";
pub const PORTAL_ASSETS: &str = "https://assets-mod.factorio.com";
pub const MIRROR: &str = "https://mods-storage.re146.dev";
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct ReleaseInfo {
pub factorio_version: String,
pub dependencies: Option<Vec<String>>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Release {
pub file_size: Option<u64>,
pub download_url: String,
pub file_name: String,
pub info_json: ReleaseInfo,
pub released_at: String,
pub version: String,
pub sha1: String,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct PortalMod {
pub name: String,
#[serde(default)]
pub title: String,
#[serde(default)]
pub owner: String,
#[serde(default)]
pub summary: String,
#[serde(default)]
pub downloads_count: u64,
pub category: Option<String>,
pub score: Option<f64>,
pub last_highlighted_at: Option<String>,
pub latest_release: Option<Release>,
pub releases: Option<Vec<Release>>,
#[serde(default)]
pub tags: Option<Vec<String>>,
#[serde(default)]
pub thumbnail: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PortalModFull {
#[serde(flatten)]
pub mod_: PortalMod,
pub description: Option<String>,
pub changelog: Option<String>,
pub homepage: Option<String>,
pub source_url: Option<String>,
pub created_at: Option<String>,
pub updated_at: Option<String>,
pub license: Option<serde_json::Value>,
#[serde(default)]
pub images: Vec<PortalImage>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct PortalImage {
pub thumbnail: String,
pub url: String,
}
pub fn encode_component(text: &str) -> String {
let mut out = String::new();
for byte in text.bytes() {
if byte.is_ascii_alphanumeric() || b"-_.!~*'()".contains(&byte) {
out.push(byte as char);
} else {
use std::fmt::Write;
write!(out, "%{byte:02X}").expect("string write");
}
}
out
}
pub fn thumbnail_url(thumbnail: Option<&str>) -> Option<String> {
let thumbnail = thumbnail.filter(|s| !s.is_empty() && !s.contains("/.thumb.png"))?;
Some(if thumbnail.starts_with("http") {
thumbnail.into()
} else {
format!("{PORTAL_ASSETS}{thumbnail}")
})
}
pub fn mirror_url(name: &str, version: &str) -> String {
format!("{MIRROR}/{}/{version}.zip", encode_component(name))
}
pub fn catalogue_url() -> String {
format!("{PORTAL}/api/mods?page_size=max")
}
pub fn mod_full_url(name: &str) -> String {
format!("{PORTAL}/api/mods/{}/full", encode_component(name))
}
pub fn mods_by_name_urls(names: &[String]) -> Vec<String> {
names
.chunks(50)
.map(|chunk| {
format!(
"{}&{}",
catalogue_url(),
chunk
.iter()
.map(|n| format!("namelist={}", encode_component(n)))
.collect::<Vec<_>>()
.join("&")
)
})
.collect()
}