Files
fomod-manager/src/nexus/url.rs

45 lines
1.1 KiB
Rust

use url::Url;
#[derive(Debug)]
pub struct NXMUrl {
pub game: String,
pub mod_id: String,
pub file: String,
pub key: String,
pub user_id: String,
pub expires: String,
}
impl NXMUrl {
pub fn parse_url(raw: &str) -> Option<Self> {
// Example url
//nxm://skyrimspecialedition/mods/266/files/725705?key=xHxcFyx2_i5rhY1YdsSbyA&expires=1773086056&user_id=123456
let url = Url::parse(raw).ok()?;
let segs: Vec<&str> = url.path_segments()?.collect();
let game = url.host_str()?;
let mod_id = segs.get(1)?.parse().ok()?;
let file_id = segs.get(3)?.parse().ok()?;
let key = url.query_pairs().find(|(k, _)| k == "key")?.1;
let expires = url
.query_pairs()
.find(|(k, _)| k == "expires")?
.1
.parse()
.ok()?;
let user_id = url.query_pairs().find(|(k, _)| k == "user_id")?.1;
Some(Self {
game: game.to_owned(),
mod_id,
file: file_id,
key: key.to_string(),
user_id: user_id.parse().ok()?,
expires,
})
}
}