chumsky/
chain.rs

1//! Traits that allow chaining parser outputs together.
2//!
3//! *“And what’s happened to the Earth?” “Ah. It’s been demolished.” “Has it,” said Arthur levelly. “Yes. It just
4//! boiled away into space.” “Look,” said Arthur, “I’m a bit upset about that.”*
5//!
6//! You usually don't need to interact with this trait, or even import it. It's only public so that you can see which
7//! types implement it. See [`Parser::chain`](super::Parser) for examples of its usage.
8
9use alloc::{string::String, vec::Vec};
10
11mod private {
12    use super::*;
13
14    pub trait Sealed<T> {}
15
16    impl<T> Sealed<T> for T {}
17    impl<T, A: Sealed<T>> Sealed<T> for (A, T) {}
18    impl<T> Sealed<T> for Option<T> {}
19    impl<T> Sealed<T> for Vec<T> {}
20    impl<T> Sealed<T> for Option<Vec<T>> {}
21    impl<T> Sealed<T> for Vec<Option<T>> {}
22    impl<T, A: Sealed<T>> Sealed<T> for Vec<(A, T)> {}
23    impl Sealed<char> for String {}
24    impl Sealed<char> for Option<String> {}
25}
26
27/// A utility trait that facilitates chaining parser outputs together into [`Vec`]s.
28///
29/// See [`Parser::chain`](super::Parser).
30#[allow(clippy::len_without_is_empty)]
31pub trait Chain<T>: private::Sealed<T> {
32    /// The number of items that this chain link consists of.
33    fn len(&self) -> usize;
34    /// Append the elements in this link to the chain.
35    fn append_to(self, v: &mut Vec<T>);
36}
37
38impl<T> Chain<T> for T {
39    fn len(&self) -> usize {
40        1
41    }
42    fn append_to(self, v: &mut Vec<T>) {
43        v.push(self);
44    }
45}
46
47impl<T, A: Chain<T>> Chain<T> for (A, T) {
48    fn len(&self) -> usize {
49        1
50    }
51    fn append_to(self, v: &mut Vec<T>) {
52        self.0.append_to(v);
53        v.push(self.1);
54    }
55}
56
57impl<T> Chain<T> for Option<T> {
58    fn len(&self) -> usize {
59        self.is_some() as usize
60    }
61    fn append_to(self, v: &mut Vec<T>) {
62        if let Some(x) = self {
63            v.push(x);
64        }
65    }
66}
67
68impl<T> Chain<T> for Vec<T> {
69    fn len(&self) -> usize {
70        self.as_slice().len()
71    }
72    fn append_to(mut self, v: &mut Vec<T>) {
73        v.append(&mut self);
74    }
75}
76
77impl Chain<char> for String {
78    // TODO: Quite inefficient
79    fn len(&self) -> usize {
80        self.chars().count()
81    }
82    fn append_to(self, v: &mut Vec<char>) {
83        v.extend(self.chars());
84    }
85}
86
87impl<T> Chain<T> for Option<Vec<T>> {
88    fn len(&self) -> usize {
89        self.as_ref().map_or(0, Chain::<T>::len)
90    }
91    fn append_to(self, v: &mut Vec<T>) {
92        if let Some(x) = self {
93            x.append_to(v);
94        }
95    }
96}
97
98impl Chain<char> for Option<String> {
99    fn len(&self) -> usize {
100        self.as_ref().map_or(0, Chain::<char>::len)
101    }
102    fn append_to(self, v: &mut Vec<char>) {
103        if let Some(x) = self {
104            x.append_to(v);
105        }
106    }
107}
108
109impl<T> Chain<T> for Vec<Option<T>> {
110    fn len(&self) -> usize {
111        self.iter().map(Chain::<T>::len).sum()
112    }
113    fn append_to(self, v: &mut Vec<T>) {
114        self
115            .into_iter()
116            .for_each(|x| x.append_to(v));
117    }
118}
119
120impl<T, A: Chain<T>> Chain<T> for Vec<(A, T)> {
121    fn len(&self) -> usize {
122        self.iter().map(Chain::<T>::len).sum()
123    }
124    fn append_to(self, v: &mut Vec<T>) {
125        self
126            .into_iter()
127            .for_each(|x| x.append_to(v));
128    }
129}