Skip to main content

mid_tools/re/
run.rs

1use std::str::Chars;
2
3use crate::{
4    free::Succ,
5    partition_map::Partition,
6    range_set::{self, RangeSet},
7};
8
9#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub enum Symbol<S> {
11    Atom(S),
12    Set(RangeSet<S>),
13}
14
15impl<S: Clone> Symbol<S> {
16    pub fn into_partitions(self) -> IntoPartitions<S> {
17        IntoPartitions(match self {
18            Symbol::Atom(a) => IntoPartitionsInner::Atom(Some(a)),
19            Symbol::Set(v) => IntoPartitionsInner::Set(v.into_ranges()),
20        })
21    }
22}
23
24impl<S: Clone + Succ> IntoIterator for Symbol<S> {
25    type IntoIter = IntoPartitions<S>;
26    type Item = Partition<S>;
27
28    #[inline]
29    fn into_iter(self) -> Self::IntoIter { self.into_partitions() }
30}
31
32#[derive(Debug)]
33enum IntoPartitionsInner<S> {
34    Atom(Option<S>),
35    Set(range_set::IntoRanges<S>),
36}
37
38#[derive(Debug)]
39pub struct IntoPartitions<S>(IntoPartitionsInner<S>);
40
41impl<S: Clone + Succ> Iterator for IntoPartitions<S> {
42    type Item = Partition<S>;
43
44    fn next(&mut self) -> Option<Self::Item> {
45        match &mut self.0 {
46            IntoPartitionsInner::Atom(a) => a.take().map(|a| (a.clone()..a.succ()).into()),
47            IntoPartitionsInner::Set(v) => v.next(),
48        }
49    }
50}
51
52#[derive(Debug, Clone, PartialEq, Eq)]
53pub enum Run<S, V = Vec<S>> {
54    Run(V),
55    Set(RangeSet<S>),
56}
57
58impl<'a> From<&'a str> for Run<char, Chars<'a>> {
59    #[inline]
60    fn from(value: &'a str) -> Self { Self::Run(value.chars()) }
61}
62
63// TODO: needs IntoChars
64// impl From<String> for Run<char, String> {
65//     #[inline]
66//     fn from(value: String) -> Self { Self::Run(value) }
67// }
68
69// impl From<Cow<'_, str>> for Run<char, String> {
70//     #[inline]
71//     fn from(value: Cow<'_, str>) -> Self { Self::Run(value.into_owned()) }
72// }
73
74#[derive(Debug, Clone)]
75enum IntoSymsInner<S, V> {
76    Run(V),
77    Set(Option<RangeSet<S>>),
78}
79
80#[derive(Debug, Clone)]
81pub struct IntoSyms<S, V>(IntoSymsInner<S, V>);
82
83impl<S, V: Iterator<Item = S>> Iterator for IntoSyms<S, V> {
84    type Item = Symbol<S>;
85
86    fn next(&mut self) -> Option<Self::Item> {
87        match &mut self.0 {
88            IntoSymsInner::Run(v) => v.next().map(Symbol::Atom),
89            IntoSymsInner::Set(s) => s.take().map(Symbol::Set),
90        }
91    }
92}
93
94pub trait IntoSymbols {
95    type Atom;
96    type Run: Iterator<Item = Symbol<Self::Atom>>;
97
98    fn into_symbols(self) -> Self::Run;
99}
100
101impl<S, V: IntoIterator<Item = S>> IntoSymbols for Run<S, V> {
102    type Atom = S;
103    type Run = IntoSyms<S, V::IntoIter>;
104
105    #[inline]
106    fn into_symbols(self) -> <Self as IntoSymbols>::Run {
107        IntoSyms(match self {
108            Self::Run(v) => IntoSymsInner::Run(v.into_iter()),
109            Self::Set(s) => IntoSymsInner::Set(Some(s)),
110        })
111    }
112}
113
114impl<T: IntoIterator> IntoSymbols for T {
115    type Atom = T::Item;
116    type Run = IntoSyms<T::Item, T::IntoIter>;
117
118    #[inline]
119    fn into_symbols(self) -> Self::Run { Run::Run(self).into_symbols() }
120}