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
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use std::{
    borrow::Cow,
    iter::FromIterator,
    ops::{Deref, DerefMut},
};

/// An individual key-value pair
#[derive(Debug, Default)]
pub struct Capture<'key, 'value> {
    key: Cow<'key, str>,
    value: Cow<'value, str>,
}

impl<'key, 'value> Capture<'key, 'value> {
    /// Build a new Capture from the provided key and value. Passing a
    /// &str here is preferable, but a String will also work.
    pub fn new(key: impl Into<Cow<'key, str>>, value: impl Into<Cow<'value, str>>) -> Self {
        Self {
            key: key.into(),
            value: value.into(),
        }
    }

    /// returns the name of this capture
    pub fn name(&self) -> &str {
        &self.key
    }

    /// returns the value of this capture
    pub fn value(&self) -> &str {
        &self.value
    }

    /// transforms this potentially-borrowed Capture into a 'static
    /// capture that can outlive the source data. This allocates new
    /// strings if needed, and should be avoided unless necessary for
    /// a particular application
    pub fn into_owned(self) -> Capture<'static, 'static> {
        Capture {
            key: self.key.to_string().into(),
            value: self.value.to_string().into(),
        }
    }
}

/// Captured params and a wildcard
#[derive(Debug, Default)]
pub struct Captures<'keys, 'values> {
    pub(crate) params: Vec<Capture<'keys, 'values>>,
    pub(crate) wildcard: Option<Cow<'values, str>>,
}

impl<'keys, 'values> Captures<'keys, 'values> {
    /// Builds a new empty Captures
    pub fn new() -> Self {
        Self::default()
    }

    /// Transforms this Captures into a 'static Captures which can
    /// outlive the source data. This allocates new strings if needed,
    /// and should be avoided unless necessary for a particular
    /// application
    pub fn into_owned(self) -> Captures<'static, 'static> {
        Captures {
            params: self.params.into_iter().map(|c| c.into_owned()).collect(),
            wildcard: self.wildcard.map(|c| c.to_string().into()),
        }
    }

    /// returns a slice of captures
    pub fn params(&self) -> &[Capture] {
        &self.params[..]
    }

    /// set the captured wildcard to the provided &str or
    /// String. Prefer passing a &str if available.
    pub fn set_wildcard(&mut self, wildcard: impl Into<Cow<'values, str>>) {
        self.wildcard = Some(wildcard.into());
    }

    /// returns what the * wildcard matched, if any
    pub fn wildcard(&self) -> Option<&str> {
        self.wildcard.as_deref()
    }

    /// checks the list of params for a matching key
    pub fn get(&self, key: &str) -> Option<&str> {
        self.params.iter().find_map(|capture| {
            if capture.key == key {
                Some(&*capture.value)
            } else {
                None
            }
        })
    }

    /// Add the provided Capture (or capture-like) to the end of the params
    pub fn push(&mut self, capture: impl Into<Capture<'keys, 'values>>) {
        self.params.push(capture.into());
    }
}

impl<'keys, 'values> Deref for Captures<'keys, 'values> {
    type Target = Vec<Capture<'keys, 'values>>;

    fn deref(&self) -> &Self::Target {
        &self.params
    }
}

impl<'keys, 'values> DerefMut for Captures<'keys, 'values> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.params
    }
}

impl<'key, 'value> From<(&'key str, &'value str)> for Capture<'key, 'value> {
    fn from(kv: (&'key str, &'value str)) -> Self {
        Self {
            key: kv.0.into(),
            value: kv.1.into(),
        }
    }
}

impl<'keys, 'values, F> From<F> for Captures<'keys, 'values>
where
    F: IntoIterator<Item = (&'keys str, &'values str)>,
{
    fn from(f: F) -> Self {
        f.into_iter().collect()
    }
}

impl<'keys, 'values> FromIterator<(&'keys str, &'values str)> for Captures<'keys, 'values> {
    fn from_iter<T: IntoIterator<Item = (&'keys str, &'values str)>>(iter: T) -> Self {
        Self {
            params: iter.into_iter().map(Into::into).collect(),
            wildcard: None,
        }
    }
}

impl<'keys, 'values> Extend<(&'keys str, &'values str)> for Captures<'keys, 'values> {
    fn extend<T: IntoIterator<Item = (&'keys str, &'values str)>>(&mut self, iter: T) {
        for (k, v) in iter {
            self.params.push(Capture::new(k, v));
        }
    }
}