mirror of
https://github.com/YGGverse/btracker-gemini.git
synced 2026-04-01 17:45:34 +00:00
implement search feature
This commit is contained in:
parent
fdd6e10e3a
commit
96c3df1b41
3 changed files with 119 additions and 84 deletions
42
src/route.rs
Normal file
42
src/route.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue