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
165
166
167
168
169
170
use proc_macro2::Span;

#[cfg(feature = "derive")]
use quote::ToTokens;

use std::{
    collections::VecDeque,
    iter::Fuse,
    mem,
    ops::{Deref, DerefMut},
};

pub(crate) fn dummy_ident() -> proc_macro2::Ident {
    proc_macro2::Ident::new("__dummy__", Span::mixed_site())
}

////////////////////////////////////////////////////////////////////////////////////

#[cfg(feature = "derive")]
pub fn spanned_err(tokens: &dyn ToTokens, display: &dyn std::fmt::Display) -> crate::Error {
    use syn::spanned::Spanned;
    crate::Error::new(tokens.span(), display)
}

////////////////////////////////////////////////////////////////////////////////////

#[derive(Clone, Debug)]
pub struct Peekable2<I: Iterator> {
    iter: Fuse<I>,
    queue: VecDeque<I::Item>,
}

impl Peekable2<std::ops::Range<u8>> {
    #[allow(clippy::new_ret_no_self)]
    pub fn new<I: IntoIterator>(iter: I) -> Peekable2<I::IntoIter> {
        Peekable2 {
            iter: iter.into_iter().fuse(),
            queue: VecDeque::new(),
        }
    }
}

impl<I: Iterator> Peekable2<I> {
    pub fn is_empty(&mut self) -> bool {
        self.peek().is_none()
    }

    pub fn peek(&mut self) -> Option<&I::Item> {
        if self.queue.is_empty() {
            self.queue.push_back(self.iter.next()?);
        }
        Some(&self.queue[0])
    }
    pub fn peek2(&mut self) -> Option<&I::Item> {
        while self.queue.len() < 2 {
            self.queue.push_back(self.iter.next()?);
        }
        Some(&self.queue[1])
    }
}

impl<I> Iterator for Peekable2<I>
where
    I: Iterator,
{
    type Item = I::Item;

    fn next(&mut self) -> Option<I::Item> {
        if let opt @ Some(_) = self.queue.pop_front() {
            opt
        } else {
            self.iter.next()
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let (low, high) = self.iter.size_hint();
        let len = self.queue.len();
        (low + len, high.map(|x| x.saturating_add(len)))
    }
}

////////////////////////////////////////////////////////////////////////////////////

/// A result wrapper which panics if it's the error variant is not handled,
/// by calling `.into_result()`.
#[derive(Debug, Clone)]
pub struct LinearResult {
    errors: Result<(), crate::Error>,
}

impl Drop for LinearResult {
    fn drop(&mut self) {
        mem::replace(&mut self.errors, Ok(())).expect("Expected LinearResult to be handled");
    }
}

impl LinearResult {
    #[inline]
    pub fn new(res: Result<(), crate::Error>) -> Self {
        Self { errors: res }
    }

    #[inline]
    pub fn ok() -> Self {
        Self::new(Ok(()))
    }
}

impl From<Result<(), crate::Error>> for LinearResult {
    #[inline]
    fn from(res: Result<(), crate::Error>) -> Self {
        Self::new(res)
    }
}

impl Deref for LinearResult {
    type Target = Result<(), crate::Error>;

    fn deref(&self) -> &Result<(), crate::Error> {
        &self.errors
    }
}

impl DerefMut for LinearResult {
    fn deref_mut(&mut self) -> &mut Result<(), crate::Error> {
        &mut self.errors
    }
}

#[allow(dead_code)]
impl LinearResult {
    #[inline]
    pub fn into_result(mut self) -> Result<(), crate::Error> {
        mem::replace(&mut self.errors, Ok(()))
    }

    #[inline]
    pub fn take(&mut self) -> Result<(), crate::Error> {
        self.replace(Ok(()))
    }

    #[inline]
    pub fn replace(&mut self, other: Result<(), crate::Error>) -> Result<(), crate::Error> {
        mem::replace(&mut self.errors, other)
    }

    #[inline]
    pub fn push_err<E>(&mut self, err: E)
    where
        E: Into<crate::Error>,
    {
        let err = err.into();
        match &mut self.errors {
            this @ Ok(_) => *this = Err(err),
            Err(e) => e.combine(err),
        }
    }

    #[inline]
    pub fn combine_err<E>(&mut self, res: Result<(), E>)
    where
        E: Into<crate::Error>,
    {
        if let Err(err) = res {
            let err = err.into();
            self.push_err(err);
        }
    }
}