use core::marker::PhantomData;
use crate::parse::{FromEure, ParseContext, ParseError, ParseErrorKind};
use crate::text::{Language, Text};
pub trait MustBeTextMarker: Copy {
const CONTENT: &'static str;
const LANGUAGE: Language;
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct MustBeText<M: MustBeTextMarker>(PhantomData<M>);
impl<M: MustBeTextMarker> MustBeText<M> {
pub const fn new() -> Self {
MustBeText(PhantomData)
}
}
impl<M: MustBeTextMarker> FromEure<'_> for MustBeText<M> {
type Error = ParseError;
fn parse(ctx: &ParseContext<'_>) -> Result<Self, Self::Error> {
let text: Text = ctx.parse()?;
if text.content == M::CONTENT && text.language.is_compatible_with(&M::LANGUAGE) {
Ok(MustBeText(PhantomData))
} else {
Err(ParseError {
node_id: ctx.node_id(),
kind: ParseErrorKind::LiteralMismatch {
expected: alloc::format!("{:?} {:?}", M::LANGUAGE, M::CONTENT),
actual: alloc::format!("{:?} {:?}", text.language, text.content),
},
})
}
}
}