Coverage Report

Created: 2025-09-27 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/anstyle-parse-0.2.6/src/params.rs
Line
Count
Source
1
//! Fixed size parameters list with optional subparameters.
2
3
use core::fmt::{self, Debug, Formatter};
4
5
pub(crate) const MAX_PARAMS: usize = 32;
6
7
#[derive(Default, Clone, PartialEq, Eq)]
8
pub struct Params {
9
    /// Number of subparameters for each parameter.
10
    ///
11
    /// For each entry in the `params` slice, this stores the length of the param as number of
12
    /// subparams at the same index as the param in the `params` slice.
13
    ///
14
    /// At the subparam positions the length will always be `0`.
15
    subparams: [u8; MAX_PARAMS],
16
17
    /// All parameters and subparameters.
18
    params: [u16; MAX_PARAMS],
19
20
    /// Number of suparameters in the current parameter.
21
    current_subparams: u8,
22
23
    /// Total number of parameters and subparameters.
24
    len: usize,
25
}
26
27
impl Params {
28
    /// Returns the number of parameters.
29
    #[inline]
30
0
    pub fn len(&self) -> usize {
31
0
        self.len
32
0
    }
33
34
    /// Returns `true` if there are no parameters present.
35
    #[inline]
36
0
    pub fn is_empty(&self) -> bool {
37
0
        self.len == 0
38
0
    }
39
40
    /// Returns an iterator over all parameters and subparameters.
41
    #[inline]
42
0
    pub fn iter(&self) -> ParamsIter<'_> {
43
0
        ParamsIter::new(self)
44
0
    }
45
46
    /// Returns `true` if there is no more space for additional parameters.
47
    #[inline]
48
0
    pub(crate) fn is_full(&self) -> bool {
49
0
        self.len == MAX_PARAMS
50
0
    }
51
52
    /// Clear all parameters.
53
    #[inline]
54
0
    pub(crate) fn clear(&mut self) {
55
0
        self.current_subparams = 0;
56
0
        self.len = 0;
57
0
    }
58
59
    /// Add an additional parameter.
60
    #[inline]
61
0
    pub(crate) fn push(&mut self, item: u16) {
62
0
        self.subparams[self.len - self.current_subparams as usize] = self.current_subparams + 1;
63
0
        self.params[self.len] = item;
64
0
        self.current_subparams = 0;
65
0
        self.len += 1;
66
0
    }
67
68
    /// Add an additional subparameter to the current parameter.
69
    #[inline]
70
0
    pub(crate) fn extend(&mut self, item: u16) {
71
0
        self.subparams[self.len - self.current_subparams as usize] = self.current_subparams + 1;
72
0
        self.params[self.len] = item;
73
0
        self.current_subparams += 1;
74
0
        self.len += 1;
75
0
    }
76
}
77
78
impl<'a> IntoIterator for &'a Params {
79
    type IntoIter = ParamsIter<'a>;
80
    type Item = &'a [u16];
81
82
0
    fn into_iter(self) -> Self::IntoIter {
83
0
        self.iter()
84
0
    }
85
}
86
87
/// Immutable subparameter iterator.
88
pub struct ParamsIter<'a> {
89
    params: &'a Params,
90
    index: usize,
91
}
92
93
impl<'a> ParamsIter<'a> {
94
0
    fn new(params: &'a Params) -> Self {
95
0
        Self { params, index: 0 }
96
0
    }
97
}
98
99
impl<'a> Iterator for ParamsIter<'a> {
100
    type Item = &'a [u16];
101
102
0
    fn next(&mut self) -> Option<Self::Item> {
103
0
        if self.index >= self.params.len() {
104
0
            return None;
105
0
        }
106
107
        // Get all subparameters for the current parameter.
108
0
        let num_subparams = self.params.subparams[self.index];
109
0
        let param = &self.params.params[self.index..self.index + num_subparams as usize];
110
111
        // Jump to the next parameter.
112
0
        self.index += num_subparams as usize;
113
114
0
        Some(param)
115
0
    }
116
117
0
    fn size_hint(&self) -> (usize, Option<usize>) {
118
0
        let remaining = self.params.len() - self.index;
119
0
        (remaining, Some(remaining))
120
0
    }
121
}
122
123
impl Debug for Params {
124
0
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
125
0
        write!(f, "[")?;
126
127
0
        for (i, param) in self.iter().enumerate() {
128
0
            if i != 0 {
129
0
                write!(f, ";")?;
130
0
            }
131
132
0
            for (i, subparam) in param.iter().enumerate() {
133
0
                if i != 0 {
134
0
                    write!(f, ":")?;
135
0
                }
136
137
0
                subparam.fmt(f)?;
138
            }
139
        }
140
141
0
        write!(f, "]")
142
0
    }
143
}