#![deny(missing_docs)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Token {
Symbol(Box<str>),
List(Vec<Self>),
}
use Token::*;
impl Token {
pub fn symbol(self) -> Option<Box<str>> {
match self {
Symbol(string) => Some(string),
List(_) => None,
}
}
pub fn symbol_ref(&self) -> Option<&str> {
match self {
Symbol(string) => Some(string),
List(_) => None,
}
}
pub fn list(self) -> Option<Vec<Self>> {
match self {
Symbol(_) => None,
List(tokens) => Some(tokens),
}
}
pub fn list_ref(&self) -> Option<&Vec<Self>> {
match self {
Symbol(_) => None,
List(tokens) => Some(tokens),
}
}
}
impl From<String> for Token {
fn from(arg: String) -> Self {
Symbol(arg.into())
}
}
impl From<Box<str>> for Token {
fn from(arg: Box<str>) -> Self {
Symbol(arg)
}
}
impl From<&str> for Token {
fn from(arg: &str) -> Self {
Symbol(arg.into())
}
}
impl<T: Copy + Into<Self>> From<&T> for Token {
fn from(arg: &T) -> Self {
(*arg).into()
}
}
impl<T: Into<Self>> From<Vec<T>> for Token {
fn from(list: Vec<T>) -> Self {
List(list.into_iter().map(T::into).collect())
}
}
mod implement_display {
use std::fmt::{Display, Formatter, Result};
use crate::Token;
impl Display for Token {
fn fmt(&self, f: &mut Formatter) -> Result {
match self {
Self::Symbol(string) => write!(f, "{string:?}"),
Self::List(vec) => {
let mut first = true;
write!(f, "(")?;
for tok in vec {
if first {
first = false;
} else {
write!(f, " ")?;
}
write!(f, "{tok}")?;
}
write!(f, ")")
}
}
}
}
}
#[cfg(feature = "parser")]
mod parser;