use std::ops::{Add, AddAssign};
pub trait Span: core::fmt::Debug + Clone + core::ops::AddAssign {}
pub trait SpanExt: Sized + Clone {
fn combine(&mut self, span: Self);
fn combined(&self, span: Self) -> Self {
let mut new = self.clone();
new.combine(span);
new
}
}
impl<S: Span> SpanExt for S {
fn combine(&mut self, span: Self) {
*self += span;
}
}
impl<S: Span> SpanExt for Option<S> {
fn combine(&mut self, span: Self) {
match span {
Some(new_span) => match self {
Some(s) => *s += new_span,
None => *self = Some(new_span),
},
None => {
*self = None;
}
}
}
}
pub trait Spanned {
type Span: Span;
fn span(&self) -> Option<Self::Span>;
}
#[cfg(feature = "smallvec")]
type KeysSmallVecArray = [String; 10];
#[cfg(feature = "smallvec")]
type KeysInner = smallvec_crate::SmallVec<KeysSmallVecArray>;
#[cfg(not(feature = "smallvec"))]
type KeysInner = Vec<String>;
#[derive(Debug, Default, Clone, Eq, PartialEq)]
#[repr(transparent)]
pub struct Keys(KeysInner);
impl Span for Keys {}
impl Keys {
pub fn new() -> Self {
Keys(KeysInner::new())
}
pub fn with_capacity(cap: usize) -> Self {
Keys(KeysInner::with_capacity(cap))
}
pub fn iter(&self) -> impl Iterator<Item = &String> {
self.0.iter()
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut String> {
self.0.iter_mut()
}
pub fn dotted(&self) -> String {
self.0.join(".")
}
pub fn push(&mut self, value: String) {
self.0.push(value)
}
pub fn into_inner(self) -> KeysInner {
self.0
}
}
impl AddAssign for Keys {
fn add_assign(&mut self, rhs: Self) {
self.0.extend(rhs.0.into_iter())
}
}
impl<S: ToString> Add<S> for Keys {
type Output = Self;
fn add(mut self, rhs: S) -> Self::Output {
self.push(rhs.to_string());
self
}
}
impl From<String> for Keys {
fn from(s: String) -> Self {
let mut v = Self::new();
v.push(s);
v
}
}
impl IntoIterator for Keys {
type Item = <KeysInner as IntoIterator>::Item;
#[cfg(feature = "smallvec")]
type IntoIter = smallvec_crate::IntoIter<KeysSmallVecArray>;
#[cfg(not(feature = "smallvec"))]
type IntoIter = std::vec::IntoIter<String>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}