mod decoder;
mod encoder;
mod lexer;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::prelude::*;
use indexmap::IndexMap;
use serde::Serialize;
use std::ops::Index;
pub use decoder::{DecodeError, Decoder};
pub use encoder::{EncodeError, Encoder};
#[derive(Clone, PartialEq, Serialize)]
#[cfg_attr(feature = "debug", derive(Debug))]
#[cfg_attr(
feature = "bevy_reflect",
derive(Reflect),
reflect(PartialEq, Serialize)
)]
#[cfg_attr(all(feature = "bevy_reflect", feature = "debug"), reflect(Debug))]
pub struct Script {
#[cfg_attr(feature = "bevy_reflect", reflect(ignore))]
pub states: IndexMap<StateId, i32>,
pub start_state: Option<StateId>,
pub start_pattern: Option<PatternId>,
#[cfg_attr(feature = "bevy_reflect", reflect(ignore))]
pub samples: IndexMap<SampleId, String>,
#[cfg_attr(feature = "bevy_reflect", reflect(ignore))]
pub patterns: IndexMap<PatternId, Pattern>,
}
#[derive(Clone, PartialEq, Serialize)]
#[cfg_attr(feature = "debug", derive(Debug))]
#[cfg_attr(
feature = "bevy_reflect",
derive(Reflect),
reflect(PartialEq, Serialize)
)]
#[cfg_attr(all(feature = "bevy_reflect", feature = "debug"), reflect(Debug))]
pub struct Pattern {
pub sequences: Vec<Sequence>,
#[cfg_attr(feature = "bevy_reflect", reflect(ignore))]
pub state_tables: Vec<StateTable>,
}
pub type StateId = String;
pub fn default_state_id() -> StateId {
"default".to_string()
}
#[derive(Clone, Eq, Hash, PartialEq, Serialize)]
#[cfg_attr(feature = "debug", derive(Debug))]
#[cfg_attr(
feature = "bevy_reflect",
derive(Reflect),
reflect(Hash, PartialEq, Serialize)
)]
#[cfg_attr(all(feature = "bevy_reflect", feature = "debug"), reflect(Debug))]
pub struct PatternId(String);
impl PatternId {
pub fn new(value: String) -> Self {
let mut chars = value.chars();
let value = match chars.next() {
None => String::new(),
Some(f) => f.to_lowercase().collect::<String>() + chars.as_str(),
};
Self(value)
}
pub fn as_str(&self) -> &str {
&self.0
}
}
pub fn end_pattern_id() -> PatternId {
PatternId::new("end".to_string())
}
pub type SampleId = String;
#[derive(Clone, PartialEq, Serialize)]
#[cfg_attr(feature = "debug", derive(Debug))]
#[cfg_attr(
feature = "bevy_reflect",
derive(Reflect),
reflect(PartialEq, Serialize)
)]
#[cfg_attr(all(feature = "bevy_reflect", feature = "debug"), reflect(Debug))]
pub struct Sequence(pub(crate) Vec<SampleId>);
impl Index<usize> for Sequence {
type Output = SampleId;
fn index(&self, index: usize) -> &Self::Output {
&self.0[index]
}
}
impl<'a> IntoIterator for &'a Sequence {
type Item = &'a SampleId;
type IntoIter = std::slice::Iter<'a, SampleId>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
impl Sequence {
#[inline]
pub fn len(&self) -> usize {
self.0.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
#[derive(Clone, PartialEq, Serialize)]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct StateTable(pub(crate) IndexMap<StateId, PatternId>);
impl StateTable {
pub fn get(&self, state: &StateId) -> Option<&PatternId> {
self.0.get(state)
}
}