#[cfg(feature = "serde")]
use super::super::runtime;
use super::{
super::{PatternError, PatternItem, TimeGranularity},
Parser,
};
use alloc::vec::Vec;
use core::str::FromStr;
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Pattern {
pub(crate) items: Vec<PatternItem>,
pub(crate) time_granularity: TimeGranularity,
}
impl Pattern {
#[cfg(feature = "datagen")]
pub(crate) fn items(&self) -> &[PatternItem] {
&self.items
}
#[cfg(feature = "datagen")]
pub(crate) fn items_mut(&mut self) -> &mut [PatternItem] {
&mut self.items
}
#[cfg(feature = "serde")]
pub(crate) fn to_runtime_pattern(&self) -> runtime::Pattern<'static> {
runtime::Pattern::from(self)
}
}
impl From<Vec<PatternItem>> for Pattern {
fn from(items: Vec<PatternItem>) -> Self {
Self {
time_granularity: items
.iter()
.copied()
.map(Into::into)
.max()
.unwrap_or_default(),
items,
}
}
}
impl From<&str> for Pattern {
fn from(items: &str) -> Self {
Self {
time_granularity: TimeGranularity::default(),
items: items.chars().map(|ch| ch.into()).collect(),
}
}
}
impl FromStr for Pattern {
type Err = PatternError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Parser::new(s).parse().map(Self::from)
}
}