fuchsia_triage/metrics/
context.rs

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use nom::error::{ErrorKind, ParseError};
use nom::{
    AsBytes, Compare, CompareResult, Err, IResult, InputIter, InputLength, InputTake,
    InputTakeAtPosition, Needed, Offset, ParseTo, Slice,
};
use std::num::NonZero;
use std::str::{CharIndices, Chars};

/// Parsing context used to store additional information.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ParsingContext<'a> {
    /// The input of the parser.
    input: &'a str,
    // The current namespace.
    namespace: &'a str,
}

impl<'a> ParsingContext<'a> {
    pub fn new(input: &'a str, namespace: &'a str) -> Self {
        Self { input, namespace }
    }
    pub fn into_inner(self) -> &'a str {
        self.input
    }
}

impl AsBytes for ParsingContext<'_> {
    fn as_bytes(&self) -> &[u8] {
        self.input.as_bytes()
    }
}

impl<'a, T> Compare<T> for ParsingContext<'a>
where
    &'a str: Compare<T>,
{
    fn compare(&self, t: T) -> CompareResult {
        self.input.compare(t)
    }
    fn compare_no_case(&self, t: T) -> CompareResult {
        self.input.compare_no_case(t)
    }
}

impl<'a> InputIter for ParsingContext<'a> {
    type Item = char;
    type Iter = CharIndices<'a>;
    type IterElem = Chars<'a>;
    fn iter_indices(&self) -> Self::Iter {
        self.input.char_indices()
    }
    fn iter_elements(&self) -> Self::IterElem {
        self.input.chars()
    }
    fn position<P>(&self, predicate: P) -> Option<usize>
    where
        P: Fn(Self::Item) -> bool,
    {
        self.input.position(predicate)
    }
    fn slice_index(&self, count: usize) -> Result<usize, Needed> {
        self.input.slice_index(count)
    }
}

impl InputLength for ParsingContext<'_> {
    fn input_len(&self) -> usize {
        self.input.len()
    }
}

impl InputTake for ParsingContext<'_> {
    fn take(&self, count: usize) -> Self {
        Self::new(&self.input[..count], self.namespace)
    }

    fn take_split(&self, count: usize) -> (Self, Self) {
        let (s0, s1) = self.input.split_at(count);
        (ParsingContext::new(s1, self.namespace), ParsingContext::new(s0, self.namespace))
    }
}

impl<'a, R> Slice<R> for ParsingContext<'a>
where
    &'a str: Slice<R>,
{
    fn slice(&self, range: R) -> Self {
        Self::new(self.input.slice(range), self.namespace)
    }
}

impl InputTakeAtPosition for ParsingContext<'_> {
    type Item = char;
    fn split_at_position<P, E: ParseError<Self>>(&self, predicate: P) -> IResult<Self, Self, E>
    where
        P: Fn(Self::Item) -> bool,
    {
        self.input
            .position(predicate)
            .map(|idx| Self::take_split(self, idx))
            .ok_or(Err::Incomplete(Needed::Size(NonZero::new(1).unwrap())))
    }

    fn split_at_position1<P, E: ParseError<Self>>(
        &self,
        predicate: P,
        e: ErrorKind,
    ) -> IResult<Self, Self, E>
    where
        P: Fn(Self::Item) -> bool,
    {
        match self.input.position(predicate) {
            Some(0) => Err(Err::Error(E::from_error_kind(*self, e))),
            Some(idx) => Ok(Self::take_split(self, idx)),
            None => Err(Err::Incomplete(Needed::Size(NonZero::new(1).unwrap()))),
        }
    }

    fn split_at_position_complete<P, E: ParseError<Self>>(
        &self,
        predicate: P,
    ) -> IResult<Self, Self, E>
    where
        P: Fn(Self::Item) -> bool,
    {
        match self.split_at_position(predicate) {
            Err(Err::Incomplete(_)) => Ok(Self::take_split(self, self.input.input_len())),
            elt => elt,
        }
    }
    fn split_at_position1_complete<P, E: ParseError<Self>>(
        &self,
        predicate: P,
        e: ErrorKind,
    ) -> IResult<Self, Self, E>
    where
        P: Fn(Self::Item) -> bool,
    {
        match self.input.position(predicate) {
            Some(0) => Err(Err::Error(E::from_error_kind(*self, e))),
            Some(idx) => Ok(Self::take_split(self, idx)),
            None => Ok(Self::take_split(self, self.input.input_len())),
        }
    }
}

impl Offset for ParsingContext<'_> {
    fn offset(&self, second: &Self) -> usize {
        self.input.offset(second.input)
    }
}

impl<'a, R> ParseTo<R> for ParsingContext<'a>
where
    &'a str: ParseTo<R>,
{
    fn parse_to(&self) -> Option<R> {
        self.input.parse_to()
    }
}