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
use std::{
    borrow::Cow,
    iter::FromIterator,
    vec::IntoIter,
};

use super::Extension;

#[derive(Debug, Clone, PartialEq, Default)]
pub struct Extensions<'s>(Vec<Extension<'s>>);

impl<'s> From<Vec<Extension<'s>>> for Extensions<'s> {
    fn from(v: Vec<Extension<'s>>) -> Self {
        Self(v)
    }
}

impl<'s> Extensions<'s> {
    pub fn new() -> Self {
        Self(Vec::new())
    }

    pub fn with_capacity(capacity: usize) -> Self {
        Self(Vec::with_capacity(capacity))
    }

    pub fn into_inner(self) -> Vec<Extension<'s>> {
        self.0
    }

    pub fn as_vec(&self) -> &Vec<Extension<'s>> {
        self.0.as_ref()
    }

    pub fn as_mut_vec(&mut self) -> &mut Vec<Extension<'s>> {
        self.0.as_mut()
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn clear(&mut self) -> usize {
        let cleared = self.len();
        self.0.clear();
        cleared
    }

    pub fn capacity(&self) -> usize {
        self.0.capacity()
    }

    pub fn reserve(mut self, additional: usize) -> Self {
        self.0.reserve(additional);
        self
    }

    pub fn shrink_to_fit(mut self) -> Self {
        self.0.shrink_to_fit();
        self
    }

    pub fn push(mut self, e: Extension<'s>) -> Self {
        self.0.push(e);
        self
    }

    pub fn push_other(self, key: Cow<'s, str>, value: Cow<'s, str>) -> Self {
        self.push(Extension::Other(key, value))
    }

    pub fn remove_item(&mut self, e: &Extension<'s>) -> Option<Extension<'s>> {
        match self.0.iter().position(|elem| elem == e) {
            Some(idx) => Some(self.0.remove(idx)),
            _ => None,
        }
    }

    pub fn remove_all(&mut self, e: &Extension<'s>) -> usize {
        let before = self.len();
        self.0.retain(|elem| elem != e);
        self.len() - before
    }

    pub fn no_body(self) -> Self {
        self.push(Extension::NoBody)
    }

    pub fn hierarchy(self) -> Self {
        self.push(Extension::Hierarchy)
    }

    pub fn flat_usage(self, level: u32) -> Self {
        self.push(Extension::FlatUsage(level.to_string().into()))
    }
}

impl ToString for Extensions<'_> {
    fn to_string(&self) -> String {
        self.0.iter().map(|e| e.to_string()).collect::<Vec<_>>().join("&")
    }
}

impl<'s> Extend<Extension<'s>> for Extensions<'s> {
    fn extend<T: IntoIterator<Item = Extension<'s>>>(&mut self, iter: T) {
        self.0.extend(iter)
    }
}

impl<'s> FromIterator<Extension<'s>> for Extensions<'s> {
    fn from_iter<T: IntoIterator<Item = Extension<'s>>>(iter: T) -> Self {
        Self(Vec::from_iter(iter))
    }
}

impl<'v, 's> IntoIterator for &'v Extensions<'s> {
    type IntoIter = <&'v Vec<Extension<'s>> as IntoIterator>::IntoIter;
    type Item = <&'v Vec<Extension<'s>> as IntoIterator>::Item;

    #[allow(clippy::into_iter_on_ref)]
    fn into_iter(self) -> Self::IntoIter {
        (&self.0).into_iter()
    }
}

impl<'v, 's> IntoIterator for &'v mut Extensions<'s> {
    type IntoIter = <&'v mut Vec<Extension<'s>> as IntoIterator>::IntoIter;
    type Item = <&'v mut Vec<Extension<'s>> as IntoIterator>::Item;

    #[allow(clippy::into_iter_on_ref)]
    fn into_iter(self) -> Self::IntoIter {
        (&mut self.0).into_iter()
    }
}

impl<'s> IntoIterator for Extensions<'s> {
    type IntoIter = IntoIter<Extension<'s>>;
    type Item = Extension<'s>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}