use proc_macro2::Span;
pub trait Expression: core::fmt::Debug {
#[must_use]
fn to_expr(&self) -> syn::Expr;
#[must_use]
fn to_pattern(&self) -> syn::Pat;
#[must_use]
fn to_type() -> syn::Type;
}
impl Expression for char {
#[inline]
fn to_expr(&self) -> syn::Expr {
syn::Expr::Lit(syn::ExprLit {
attrs: vec![],
lit: syn::Lit::Char(syn::LitChar::new(*self, Span::call_site())),
})
}
#[inline]
fn to_pattern(&self) -> syn::Pat {
syn::Pat::Lit(syn::ExprLit {
attrs: vec![],
lit: syn::Lit::Char(syn::LitChar::new(*self, Span::call_site())),
})
}
#[inline]
fn to_type() -> syn::Type {
syn::Type::Path(syn::TypePath {
qself: None,
path: syn::Path {
leading_colon: None,
segments: core::iter::once(syn::PathSegment {
ident: syn::Ident::new("char", Span::call_site()),
arguments: syn::PathArguments::None,
})
.collect(),
},
})
}
}
impl Expression for u8 {
#[inline]
fn to_expr(&self) -> syn::Expr {
syn::Expr::Lit(syn::ExprLit {
attrs: vec![],
lit: syn::Lit::Byte(syn::LitByte::new(*self, Span::call_site())),
})
}
#[inline]
fn to_pattern(&self) -> syn::Pat {
syn::Pat::Lit(syn::ExprLit {
attrs: vec![],
lit: syn::Lit::Byte(syn::LitByte::new(*self, Span::call_site())),
})
}
#[inline]
fn to_type() -> syn::Type {
syn::Type::Path(syn::TypePath {
qself: None,
path: syn::Path {
leading_colon: None,
segments: core::iter::once(syn::PathSegment {
ident: syn::Ident::new("u8", Span::call_site()),
arguments: syn::PathArguments::None,
})
.collect(),
},
})
}
}