1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use std::ops::Index;

#[allow(missing_docs)]
#[derive(Debug)]
pub struct Params<'input> {
    pub(super) path: &'input str,
    pub(super) params: Option<&'input [(usize, usize)]>,
}

#[allow(missing_docs)]
impl<'input> Params<'input> {
    pub fn is_empty(&self) -> bool {
        self.params.as_ref().map_or(true, |p| p.is_empty())
    }

    pub fn len(&self) -> usize {
        self.params.as_ref().map_or(0, |p| p.len())
    }

    pub fn get(&self, i: usize) -> Option<&str> {
        self.params
            .as_ref()
            .and_then(|p| p.get(i).and_then(|&(s, e)| self.path.get(s..e)))
    }
}

impl<'input> Index<usize> for Params<'input> {
    type Output = str;

    fn index(&self, i: usize) -> &Self::Output {
        self.get(i).expect("Out of range")
    }
}