|
| 1 | +use std::num::NonZero; |
| 2 | + |
| 3 | +#[derive(Clone, Debug)] |
| 4 | +pub enum Parameter { |
| 5 | + Anonymous(NonZero<usize>), |
| 6 | + Indexed(NonZero<usize>), |
| 7 | + Named(String, NonZero<usize>), |
| 8 | +} |
| 9 | + |
| 10 | +impl PartialEq for Parameter { |
| 11 | + fn eq(&self, other: &Self) -> bool { |
| 12 | + self.index() == other.index() |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +impl Parameter { |
| 17 | + pub fn index(&self) -> NonZero<usize> { |
| 18 | + match self { |
| 19 | + Parameter::Anonymous(index) => *index, |
| 20 | + Parameter::Indexed(index) => *index, |
| 21 | + Parameter::Named(_, index) => *index, |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Debug)] |
| 27 | +pub struct Parameters { |
| 28 | + index: NonZero<usize>, |
| 29 | + pub list: Vec<Parameter>, |
| 30 | +} |
| 31 | + |
| 32 | +impl Parameters { |
| 33 | + pub fn new() -> Self { |
| 34 | + Self { |
| 35 | + index: 1.try_into().unwrap(), |
| 36 | + list: vec![], |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + pub fn count(&self) -> usize { |
| 41 | + let mut params = self.list.clone(); |
| 42 | + params.dedup(); |
| 43 | + params.len() |
| 44 | + } |
| 45 | + |
| 46 | + pub fn name(&self, index: NonZero<usize>) -> Option<String> { |
| 47 | + self.list.iter().find_map(|p| match p { |
| 48 | + Parameter::Anonymous(i) if *i == index => Some("?".to_string()), |
| 49 | + Parameter::Indexed(i) if *i == index => Some(format!("?{i}")), |
| 50 | + Parameter::Named(name, i) if *i == index => Some(name.to_owned()), |
| 51 | + _ => None, |
| 52 | + }) |
| 53 | + } |
| 54 | + |
| 55 | + pub fn index(&self, name: impl AsRef<str>) -> Option<NonZero<usize>> { |
| 56 | + self.list |
| 57 | + .iter() |
| 58 | + .find_map(|p| match p { |
| 59 | + Parameter::Named(n, index) if n == name.as_ref() => Some(index), |
| 60 | + _ => None, |
| 61 | + }) |
| 62 | + .copied() |
| 63 | + } |
| 64 | + |
| 65 | + pub fn next_index(&mut self) -> NonZero<usize> { |
| 66 | + let index = self.index; |
| 67 | + self.index = self.index.checked_add(1).unwrap(); |
| 68 | + index |
| 69 | + } |
| 70 | + |
| 71 | + pub fn push(&mut self, name: impl AsRef<str>) -> NonZero<usize> { |
| 72 | + match name.as_ref() { |
| 73 | + "" => { |
| 74 | + let index = self.next_index(); |
| 75 | + self.list.push(Parameter::Anonymous(index)); |
| 76 | + log::trace!("anonymous parameter at {index}"); |
| 77 | + index |
| 78 | + } |
| 79 | + name if name.starts_with(&['$', ':', '@', '#']) => { |
| 80 | + match self |
| 81 | + .list |
| 82 | + .iter() |
| 83 | + .find(|p| matches!(p, Parameter::Named(n, _) if name == n)) |
| 84 | + { |
| 85 | + Some(t) => { |
| 86 | + let index = t.index(); |
| 87 | + self.list.push(t.clone()); |
| 88 | + log::trace!("named parameter at {index} as {name}"); |
| 89 | + index |
| 90 | + } |
| 91 | + None => { |
| 92 | + let index = self.next_index(); |
| 93 | + self.list.push(Parameter::Named(name.to_owned(), index)); |
| 94 | + log::trace!("named parameter at {index} as {name}"); |
| 95 | + index |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + index => { |
| 100 | + // SAFETY: Garanteed from parser that the index is bigger that 0. |
| 101 | + let index: NonZero<usize> = index.parse().unwrap(); |
| 102 | + if index > self.index { |
| 103 | + self.index = index.checked_add(1).unwrap(); |
| 104 | + } |
| 105 | + self.list.push(Parameter::Indexed(index)); |
| 106 | + log::trace!("indexed parameter at {index}"); |
| 107 | + index |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments