#[allow(clippy::wildcard_imports)]
use crate::*;
use std::marker::PhantomData;
use std::ops::Deref;
#[derive(Clone)]
pub struct Discard<T>(PhantomData<T>);
impl<T: Parse> Parser for Discard<T> {
fn parser(tokens: &mut TokenIter) -> Result<Self> {
match T::parser(tokens).refine_err::<Self>() {
Ok(_) => Ok(Self(PhantomData)),
Err(e) => Err(e),
}
}
}
impl<T> ToTokens for Discard<T> {
#[inline]
#[mutants::skip]
fn to_tokens(&self, _tokens: &mut TokenStream) {
unimplemented!("Can not emit tokens for Discard<T>")
}
}
#[mutants::skip]
impl<T: std::fmt::Debug> std::fmt::Debug for Discard<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct(&format!("Discard<{}>", std::any::type_name::<T>()))
.finish()
}
}
#[derive(Clone)]
pub struct Skip<T>(PhantomData<T>);
impl<T: Parse> Parser for Skip<T> {
fn parser(tokens: &mut TokenIter) -> Result<Self> {
T::parser(tokens).refine_err::<Self>()?;
Ok(Self(PhantomData))
}
}
impl<T> ToTokens for Skip<T> {
#[inline]
fn to_tokens(&self, _tokens: &mut TokenStream) {
}
}
#[mutants::skip]
impl<T: std::fmt::Debug> std::fmt::Debug for Skip<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct(&format!("Skip<{}>", std::any::type_name::<T>()))
.finish()
}
}
pub struct Insert<T>(pub T);
impl<T: Default> Parser for Insert<T> {
#[inline]
fn parser(_tokens: &mut TokenIter) -> Result<Self> {
Ok(Self(T::default()))
}
}
impl<T: ToTokens> ToTokens for Insert<T> {
#[inline]
fn to_tokens(&self, tokens: &mut TokenStream) {
self.0.to_tokens(tokens);
}
}
impl<T> Deref for Insert<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[mutants::skip]
impl<T: std::fmt::Debug> std::fmt::Debug for Insert<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple(&format!("Insert<{}>", std::any::type_name::<T>()))
.field(&self.0)
.finish()
}
}
pub type OrDefault<T, D = T> = Either<T, Insert<D>>;
pub type Replace<T, U> = Cons<Skip<T>, Insert<U>>;
pub type AsDefault<T> = Replace<T, T>;
#[derive(Debug)]
pub struct Swap<A, B>(pub B, pub A);
impl<A: Parse, B: Parse> Parser for Swap<A, B> {
fn parser(tokens: &mut TokenIter) -> Result<Self> {
let a: A = tokens.parse()?;
let b: B = tokens.parse()?;
Ok(Self(b, a))
}
}
impl<A: ToTokens, B: ToTokens> ToTokens for Swap<A, B> {
#[inline]
fn to_tokens(&self, tokens: &mut TokenStream) {
self.0.to_tokens(tokens);
self.1.to_tokens(tokens);
}
}
#[derive(Debug)]
pub struct IntoLiteralString<T>(LiteralString, PhantomData<T>);
impl<T: ToTokens> IntoLiteralString<T> {
pub fn from(from: &T) -> Self {
Self(
LiteralString::from_str(from.tokens_to_string()),
PhantomData,
)
}
}
impl<T> IntoLiteralString<T> {
#[inline]
#[must_use]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
#[inline]
#[must_use]
pub fn into_inner(self) -> LiteralString {
self.0
}
}
impl<T: Parse + ToTokens> Parser for IntoLiteralString<T> {
fn parser(tokens: &mut TokenIter) -> Result<Self> {
Ok(Self(
LiteralString::from_str(tokens.parse::<T>()?.tokens_to_string()),
PhantomData,
))
}
}
impl<T> ToTokens for IntoLiteralString<T> {
#[inline]
fn to_tokens(&self, tokens: &mut TokenStream) {
self.0.to_tokens(tokens);
}
}
impl<T: Default + ToTokens> Default for IntoLiteralString<T> {
fn default() -> Self {
Self(
LiteralString::from_str(T::default().tokens_to_string()),
PhantomData,
)
}
}
#[cfg(feature = "proc_macro2")]
#[derive(Debug)]
pub struct IntoIdent<T>(CachedIdent, PhantomData<T>);
#[cfg(feature = "proc_macro2")]
impl<T: ToTokens> IntoIdent<T> {
pub fn from(from: &T) -> Result<Self> {
let mut string = from.tokens_to_string();
string.retain(|c| c.is_alphanumeric() || c == '_');
Ok(Self(CachedIdent::from_string(string)?, PhantomData))
}
}
#[cfg(feature = "proc_macro2")]
impl<T> IntoIdent<T> {
#[inline]
#[must_use]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
#[inline]
#[must_use]
pub fn into_inner(self) -> CachedIdent {
self.0
}
}
#[cfg(feature = "proc_macro2")]
impl<T> From<IntoIdent<T>> for Ident {
fn from(this: IntoIdent<T>) -> Self {
this.0.into_inner()
}
}
#[cfg(feature = "proc_macro2")]
impl<T: Parse + ToTokens> Parser for IntoIdent<T> {
fn parser(tokens: &mut TokenIter) -> Result<Self> {
let mut string = tokens.parse::<T>().refine_err::<Self>()?.tokens_to_string();
string.retain(|c| c.is_alphanumeric() || c == '_');
Ok(Self(CachedIdent::from_string(string)?, PhantomData))
}
}
#[cfg(feature = "proc_macro2")]
impl<T: ToTokens> ToTokens for IntoIdent<T> {
#[inline]
fn to_tokens(&self, tokens: &mut TokenStream) {
self.0.to_tokens(tokens);
}
}
#[cfg(feature = "proc_macro2")]
impl<T: Default + ToTokens> Default for IntoIdent<T> {
fn default() -> Self {
let mut string = T::default().tokens_to_string();
string.retain(|c| c.is_alphanumeric() || c == '_');
Self(
CachedIdent::from_string(string).expect("invalid default constructed IntoIdent"),
PhantomData,
)
}
}
#[derive(Debug)]
pub struct IntoTokenStream<T>(TokenStream, PhantomData<T>);
impl<T: ToTokens> IntoTokenStream<T> {
pub fn from(from: &T) -> Self {
Self(from.to_token_stream(), PhantomData)
}
}
impl<T> IntoTokenStream<T> {
#[inline]
#[must_use]
pub fn into_inner(self) -> TokenStream {
self.0
}
}
impl<T> Deref for IntoTokenStream<T> {
type Target = TokenStream;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T: Parse + ToTokens> Parser for IntoTokenStream<T> {
fn parser(tokens: &mut TokenIter) -> Result<Self> {
let tokenstream = tokens
.parse::<T>()
.refine_err::<Self>()?
.into_token_stream();
Ok(Self(tokenstream, PhantomData))
}
}
impl<T: ToTokens> ToTokens for IntoTokenStream<T> {
#[inline]
fn to_tokens(&self, tokens: &mut TokenStream) {
self.0.to_tokens(tokens);
}
}
impl<T: Default + ToTokens> Default for IntoTokenStream<T> {
fn default() -> Self {
Self(T::default().into_token_stream(), PhantomData)
}
}
pub type TokenStreamUntil<T> = IntoTokenStream<Cons<Vec<Cons<Except<T>, TokenTree>>, Expect<T>>>;