#![allow(non_snake_case)]
#![allow(dead_code)]
use crate::rout::inner::RuteInner;
use http::Method;
use crate::application::app;
pub struct Rute{
len:Option<usize>, pub nodes:RuteInner, pub hooks:Vec<fn(&mut app::Application)>, }
impl Rute {
pub fn new() -> Self {
Rute{
len:None,
nodes:RuteInner::new(),
hooks:Vec::new()
}
}
pub fn get(&mut self,uri:&str,F:fn(&mut app::Application)){
self.bind(uri, Method::GET, F)
}
pub fn post(&mut self,uri:&str,F:fn(&mut app::Application)){
self.bind(uri, Method::POST, F)
}
pub fn put(&mut self,uri:&str,F:fn(&mut app::Application)){
self.bind(uri, Method::PUT, F)
}
pub fn delete(&mut self,uri:&str,F:fn(&mut app::Application)){
self.bind(uri, Method::DELETE, F)
}
pub fn patch(&mut self,uri:&str,F:fn(&mut app::Application)){
self.bind(uri, Method::PATCH, F)
}
fn bind(&mut self,path:&str,mt:Method,F:fn(&mut app::Application)){
let hook_id = match self.len {
Some(len)=>{
self.hooks.insert(len, F);
self.len = Some(len+1);
len + 1
},
None=>{
self.len = Some(1);
self.hooks.insert(0, F);
1
}
};
self.nodes.insert(path.to_string(), mt, hook_id);
}
pub fn matching(&self,uri:&str,mt:&Method)->Option<usize>{
let resData= self.nodes.search(uri.to_string(), mt);
match resData {
Ok((id,_))=>{
return Some(id);
}
Err(e)=>{
println!("未匹配到路由:{:?}",e);
return None;
}
}
}
}