mirror of
https://github.com/YGGverse/btracker-gemini.git
synced 2026-03-31 17:15:30 +00:00
42 lines
941 B
Rust
42 lines
941 B
Rust
use std::str::FromStr;
|
|
|
|
use librqbit_core::Id20;
|
|
use regex::Regex;
|
|
use url::Url;
|
|
|
|
pub enum Route {
|
|
Info(Id20),
|
|
List { page: Option<usize> },
|
|
NotFound,
|
|
Search,
|
|
}
|
|
|
|
impl Route {
|
|
pub fn from_url(url: &Url) -> Self {
|
|
let p = url.path().to_lowercase();
|
|
let q = url.query();
|
|
|
|
if p.is_empty() {
|
|
return Self::List { page: None };
|
|
}
|
|
|
|
if let Ok(id) = Id20::from_str(p.trim_matches('/')) {
|
|
return Self::Info(id);
|
|
}
|
|
|
|
if p == "/search" && q.is_none() {
|
|
return Self::Search;
|
|
}
|
|
|
|
if Regex::new(r"^/(|search)").unwrap().is_match(&p) {
|
|
return Self::List {
|
|
page: Regex::new(r"/(\d+)$").unwrap().captures(&p).map(|c| {
|
|
c.get(1)
|
|
.map_or(1, |p| p.as_str().parse::<usize>().unwrap_or(1))
|
|
}),
|
|
};
|
|
}
|
|
|
|
Self::NotFound
|
|
}
|
|
}
|