use std::ops::Index;
use std::sync::Arc;
use std::collections::HashMap;
use util::typemap::Key;
use regex::Captures;
pub struct Parameters {
text: String,
matches: Vec<Option<(usize, usize)>>,
names: Arc<HashMap<String, usize>>,
}
impl Parameters {
pub(crate) fn new(names: Arc<HashMap<String, usize>>, text: &str, captures: Captures) -> Self {
Self {
names,
text: text.into(),
matches: captures
.iter()
.map(|capture| capture.map(|m| (m.start(), m.end())))
.collect(),
}
}
pub fn get(&self, index: usize) -> Option<&str> {
self.matches
.get(index + 1)
.and_then(|m| m.map(|(start, end)| &self.text[start..end]))
}
pub fn name(&self, name: &str) -> Option<&str> {
self.names.get(name).and_then(|&i| self.get(i - 1))
}
}
impl Key for Parameters {
type Value = Self;
}
impl Index<usize> for Parameters {
type Output = str;
fn index(&self, index: usize) -> &Self::Output {
self.get(index)
.unwrap_or_else(|| panic!("no parameter at index '{}'", index))
}
}
impl<'b> Index<&'b str> for Parameters {
type Output = str;
fn index<'a>(&'a self, name: &str) -> &'a str {
self.name(name)
.unwrap_or_else(|| panic!("no parameter named '{}'", name))
}
}