cogo_http/path/
mod.rs

1use std::collections::{BTreeMap};
2use crate::error::Result;
3
4#[derive(Clone,Debug)]
5pub struct Path {
6    // /{a}/{b}/{c}
7    pub url: String,
8    //key-value
9    pub map: Vec<(i32, String)>,
10}
11
12impl Path {
13    pub fn new(url: &str) -> Self {
14        let url = &url[0..url.find("?").unwrap_or(url.len())];
15        let args: Vec<&str> = url.split("/").collect();
16        let mut map = Vec::new();
17        let mut idx = 0;
18        for x in args {
19            if x.starts_with("{") && x.ends_with("}") {
20                map.push((idx, x.trim_start_matches("{").trim_end_matches("}").to_string()));
21            }
22            idx += 1;
23        }
24        Self {
25            url: url.to_string(),
26            map: map,
27        }
28    }
29
30    pub fn read_path_data(&self, url: &str) -> BTreeMap<String, String> {
31        let url = &url[0..url.find("?").unwrap_or(url.len())];
32        let args: Vec<&str> = url.split("/").collect();
33        let mut params = BTreeMap::new();
34        let mut idx = 0;
35        for x in args {
36            for (i, v) in &self.map {
37                if *i == idx {
38                    params.insert(v.to_string(), x.to_string());
39                }
40            }
41            idx += 1;
42        }
43        params
44    }
45}