use super::super::{CstElement, Language};
use derive_more::{From, Into};
use rowan::SyntaxNode;
#[derive(Debug, Clone, PartialEq, Eq, Hash, From, Into)]
pub struct CstNodeMismatch<N, Lang: Language> {
found: SyntaxNode<Lang>,
_m: core::marker::PhantomData<N>,
}
impl<N: CstElement<Lang>, Lang: Language> core::fmt::Display for CstNodeMismatch<N, Lang>
where
Lang::Kind: core::fmt::Display,
{
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"syntax node mismatch: expected syntax node of kind {}, but found syntax node of kind {}",
N::KIND,
self.found.kind()
)
}
}
impl<N: CstElement<Lang>, Lang: Language> core::error::Error for CstNodeMismatch<N, Lang>
where
N: CstElement<Lang> + core::fmt::Debug,
Lang::Kind: core::fmt::Display,
{
}
impl<N, Lang: Language> CstNodeMismatch<N, Lang> {
#[inline]
pub const fn new(found: SyntaxNode<Lang>) -> Self {
Self {
found,
_m: core::marker::PhantomData,
}
}
#[inline]
pub const fn expected(&self) -> Lang::Kind
where
N: CstElement<Lang>,
{
N::KIND
}
#[inline]
pub const fn found(&self) -> &SyntaxNode<Lang> {
&self.found
}
#[inline]
pub fn into_components(self) -> (Lang::Kind, SyntaxNode<Lang>)
where
N: CstElement<Lang>,
{
(N::KIND, self.found)
}
}