use crate::common::{Combined, NoOp, NoOpSeq, Str, SurroundingSeqAccept};
use crate::{Output, SequenceAccept, Writable};
use std::fmt::Debug;
mod syntax;
#[cfg(test)]
mod tests;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Edition {
Rust2015,
Rust2018,
Rust2021,
Rust2024,
}
#[derive(Clone, Debug)]
pub struct UseType<Type>(pub Type);
#[derive(Clone, Debug)]
pub struct CfgAttr<Cond, Attr>(pub Cond, pub Attr);
#[derive(Clone, Debug)]
pub struct Cfg<Cond>(pub Cond);
#[derive(Clone, Debug)]
pub enum Doc<Value> {
Hidden,
Inline,
NoInline,
Alias(Value),
Test(Value),
}
impl Doc<NoOp> {
pub fn hidden() -> Doc<NoOp> {
Doc::Hidden
}
pub fn inline() -> Doc<NoOp> {
Doc::Inline
}
pub fn no_inline() -> Doc<NoOp> {
Doc::NoInline
}
}
#[derive(Clone, Debug)]
pub enum Target<Value> {
Os(Value),
Family(Value),
Arch(Value),
}
#[derive(Clone, Debug)]
pub struct Link<Arg>(pub Arg);
#[derive(Clone, Debug)]
pub struct NoMangle;
#[derive(Clone, Debug)]
pub struct AllowLints<Lints>(pub Lints);
#[derive(Clone, Debug)]
pub enum Deprecated<Msg, Since = NoOp> {
Basic,
Message(Msg),
Full { since: Since, note: Msg },
}
impl Default for Deprecated<NoOp, NoOp> {
fn default() -> Self {
Self::Basic
}
}
impl Deprecated<NoOp, NoOp> {
pub fn basic() -> Self {
Self::Basic
}
}
impl<Msg> Deprecated<Msg> {
pub fn with_message(msg: Msg) -> Self {
Self::Message(msg)
}
}
#[derive(Clone, Debug)]
pub struct MustUse;
#[derive(Clone, Debug)]
pub struct ModPub;
#[derive(Clone, Debug)]
pub struct ModUnsafe;
#[derive(Clone, Debug)]
pub struct ModUnsafeExtern<Abi>(pub Abi);
#[derive(Clone, Debug)]
pub struct Stmt<Expr>(pub Expr);
#[derive(Clone, Debug)]
pub struct LetStmt<Variable, Expr>(pub Variable, pub Expr);
#[derive(Clone, Debug)]
pub struct LetMutStmt<Variable, Expr>(pub Variable, pub Expr);
#[derive(Clone, Debug)]
pub struct AssignStmt<Variable, Expr>(pub Variable, pub Expr);
#[derive(Clone, Debug)]
pub struct AssignExpr<Variable, Expr>(pub Variable, pub Expr);
#[derive(Clone, Debug)]
pub struct ReturnStmt<Expr>(pub Expr);
#[derive(Clone, Debug)]
pub struct LetExpr<Pattern, Expr>(pub Pattern, pub Expr);
#[derive(Clone, Debug)]
pub struct RawStringLiteral<Content>(pub Content);
#[derive(Clone, Debug)]
pub struct ArrayFromElements<Elements>(pub Elements);
#[derive(Clone, Debug)]
pub struct ArrayInit<Default, Count>(pub Default, pub Count);
#[derive(Clone, Debug)]
pub struct AssociatedItem<Cont, Item>(pub Cont, pub Item);
#[derive(Clone, Debug)]
pub struct QuestionMarkAfter<Expr>(pub Expr);
#[derive(Clone, Debug)]
pub struct TypeAsTrait<Type, Trait>(pub Type, pub Trait);
#[derive(Clone, Debug)]
pub struct TypeAsType<Type1, Type2>(pub Type1, pub Type2);
#[derive(Clone, Debug)]
pub struct ExternBlock<Abi, Body> {
pub abi: Abi,
pub body: Body,
}
impl<Abi, Body> CanHaveAttributes for ExternBlock<Abi, Body> {
fn with_attributes<Attr>(self, attr: Attr) -> WithAttributes<Attr, Self> {
WithAttributes {
attr,
separator: AttributeSeparator::NewLine,
value: self,
}
}
}
#[derive(Clone, Debug)]
pub struct ModBlock<Name, Body> {
pub name: Name,
pub body: Body,
}
#[derive(Clone, Debug)]
pub struct IfBlock<Cond, Body>(pub Cond, pub Body);
#[derive(Clone, Debug)]
pub struct Else<Before, After>(pub Before, pub After);
#[derive(Clone, Debug)]
pub struct Block<Body>(pub Body);
#[derive(Clone, Debug)]
pub struct UnsafeBlock<Expr>(pub Expr);
#[derive(Clone, Debug)]
pub struct Closure<InputVars, Expr> {
pub input_vars: InputVars,
pub inside_block: Expr,
}
#[derive(Clone, Debug)]
pub struct FunctionCall<Recv, Name, Args> {
pub receiver: Recv,
pub is_assoc: bool,
pub name: Name,
pub args: Args,
}
#[derive(Clone, Debug)]
pub struct Turbofish<Name, Args>(pub Name, pub Args);
#[derive(Clone, Debug)]
pub struct MemberAccess<Owner, Member>(pub Owner, pub Member);
#[derive(Clone, Debug)]
pub struct FunctionDef<Mods, Name, Args, Return, Where, Body> {
pub mods: Mods,
pub name: Name,
pub args: Args,
pub return_type: Return,
pub where_conds: Where,
pub body: Body,
}
impl<Mods, Name, Args, Return, Where, Body> CanHaveAttributes
for FunctionDef<Mods, Name, Args, Return, Where, Body>
{
fn with_attributes<Attr>(self, attr: Attr) -> WithAttributes<Attr, Self> {
WithAttributes {
attr,
separator: AttributeSeparator::NewLine,
value: self,
}
}
}
#[derive(Clone, Debug)]
pub struct FunctionBodyDeclare;
#[derive(Clone, Debug)]
pub struct FunctionBodyImplement<Inner>(pub Inner);
#[derive(Clone, Debug)]
pub struct FunctionPtr<Args, Return> {
pub kind: FunctionPtrKind,
pub args: Args,
pub return_type: Return,
}
#[derive(Clone, Debug)]
pub enum FunctionPtrKind {
FnPtr,
Fn,
FnMut,
FnOnce,
}
#[derive(Clone, Debug)]
pub struct AssociatedTypeEquals<Type, Value>(pub Type, pub Value);
#[derive(Clone, Debug)]
pub struct DynOf<Type>(pub Type);
#[derive(Clone, Debug)]
pub struct RefOf<Expr>(pub Expr);
#[derive(Clone, Debug)]
pub struct RefMutOf<Expr>(pub Expr);
#[derive(Clone, Debug)]
pub struct Dereference<Expr>(pub Expr);
#[derive(Clone, Debug)]
pub struct ParenthesesAround<Expr>(pub Expr);
pub struct ImplOf<Type>(pub Type);
#[derive(Clone, Debug)]
pub struct LifetimedRefOf<'l, Type>(pub &'l str, pub Type);
#[derive(Clone, Debug)]
pub struct RawConstOf<Expr>(pub Expr);
#[derive(Clone, Debug)]
pub struct RawMutOf<Expr>(pub Expr);
#[derive(Clone, Debug)]
pub struct ConstPtr<Type>(pub Type);
#[derive(Clone, Debug)]
pub struct MutPtr<Type>(pub Type);
#[derive(Clone, Debug)]
pub struct TypeDeclare<Value>(pub Value);
#[derive(Clone, Debug)]
pub struct TraitDef<Mods, Name, TypeVars, SuperTraits, Body> {
pub mods: Mods,
pub name: Name,
pub type_variables: TypeVars,
pub super_traits: SuperTraits,
pub body: Body,
}
impl<Mods, Name, TypeVars, SuperTraits, Body> CanHaveAttributes
for TraitDef<Mods, Name, TypeVars, SuperTraits, Body>
{
fn with_attributes<Attr>(self, attr: Attr) -> WithAttributes<Attr, Self> {
WithAttributes {
attr,
separator: AttributeSeparator::NewLine,
value: self,
}
}
}
#[derive(Clone, Debug)]
pub struct TraitImpl<Mods, TypeVars, Trait, Recv, Where, Body> {
pub mods: Mods,
pub type_variables: TypeVars,
pub the_trait: Trait,
pub receiver: Recv,
pub where_conds: Where,
pub body: Body,
}
impl<Mods, TypeVars, Trait, Recv, Where, Body> CanHaveAttributes
for TraitImpl<Mods, TypeVars, Trait, Recv, Where, Body>
{
fn with_attributes<Attr>(self, attr: Attr) -> WithAttributes<Attr, Self> {
WithAttributes {
attr,
separator: AttributeSeparator::NewLine,
value: self,
}
}
}
#[derive(Clone, Debug)]
pub struct Impl<Mods, TypeVars, Recv, Where, Body> {
pub mods: Mods,
pub type_variables: TypeVars,
pub receiver: Recv,
pub where_conds: Where,
pub body: Body,
}
impl<Mods, TypeVars, Recv, Where, Body> CanHaveAttributes
for Impl<Mods, TypeVars, Recv, Where, Body>
{
fn with_attributes<Attr>(self, attr: Attr) -> WithAttributes<Attr, Self> {
WithAttributes {
attr,
separator: AttributeSeparator::NewLine,
value: self,
}
}
}
#[derive(Clone, Debug)]
pub struct StructDef<Mods, Name, Elements> {
pub mods: Mods,
pub kind: StructKind<Name, Elements>,
}
impl<Mods, Name, Elements> CanHaveAttributes for StructDef<Mods, Name, Elements> {
fn with_attributes<Attr>(self, attr: Attr) -> WithAttributes<Attr, Self> {
WithAttributes {
attr,
separator: AttributeSeparator::NewLine,
value: self,
}
}
}
#[derive(Clone, Debug)]
pub enum StructKind<Name, Elements> {
Tuple(Name, Elements),
NamedFields(Name, Elements),
}
#[derive(Clone, Debug)]
pub struct StructCall<Name, Body> {
pub name: Name,
pub body: Body,
}
#[derive(Clone, Debug)]
pub struct StructFields<Fields>(pub Fields);
#[derive(Clone, Debug)]
pub struct DeclareField<Mods, Name, FieldType> {
pub mods: Mods,
pub name: Name,
pub field_type: FieldType,
}
impl<Mods, Name, Value> CanHaveAttributes for DeclareField<Mods, Name, Value> {
fn with_attributes<Attr>(self, attr: Attr) -> WithAttributes<Attr, Self> {
WithAttributes {
attr,
separator: AttributeSeparator::NewLine,
value: self,
}
}
}
#[derive(Clone, Debug)]
pub struct FillOutField<Name, Value>(pub Name, pub Value);
#[derive(Clone, Debug)]
pub struct NamedTuple<Name, Args> {
pub name: Name,
pub args: Args,
}
#[derive(Clone, Debug)]
pub struct AnonTuple<Args>(pub Args);
pub type UnitType = AnonTuple<NoOpSeq>;
impl AnonTuple<NoOpSeq> {
pub fn unit() -> Self {
Self(NoOpSeq)
}
}
#[derive(Clone, Debug)]
pub struct WithAttributes<Attr, Value> {
pub attr: Attr,
pub separator: AttributeSeparator,
pub value: Value,
}
#[derive(Copy, Clone, Debug)]
pub enum AttributeSeparator {
Space,
NewLine,
}
pub trait CanHaveAttributes: Sized {
fn with_attributes<Attr>(self, attr: Attr) -> WithAttributes<Attr, Self>;
}
#[derive(Clone, Debug)]
pub struct EnumDef<Mods, Name, Entries> {
pub mods: Mods,
pub name: Name,
pub entries: Entries,
}
impl<Mods, Name, Entries> CanHaveAttributes for EnumDef<Mods, Name, Entries> {
fn with_attributes<Attr>(self, attr: Attr) -> WithAttributes<Attr, Self> {
WithAttributes {
attr,
separator: AttributeSeparator::NewLine,
value: self,
}
}
}
#[derive(Clone, Debug)]
pub struct Parameterized<Name, TypeArgs>(pub Name, pub TypeArgs);
#[derive(Clone, Debug)]
pub struct BoundedTypeVar<TypeVar, Bounds>(pub TypeVar, pub Bounds);
#[derive(Clone, Debug)]
pub struct Lifetime<'l>(pub &'l str);
#[derive(Clone, Debug)]
pub struct FunctionParam<Name, Type>(pub Name, pub Type);
impl<Name, Type> CanHaveAttributes for FunctionParam<Name, Type> {
fn with_attributes<Attr>(self, attr: Attr) -> WithAttributes<Attr, Self> {
WithAttributes {
attr,
separator: AttributeSeparator::Space,
value: self,
}
}
}
#[derive(Debug)]
pub struct AttributesAccept<'o, O, Sep> {
inner: SurroundingSeqAccept<'o, O, Str<&'static str>, Combined<Str<&'static str>, Sep>>,
}
impl<'o, O, Sep> AttributesAccept<'o, O, Sep> {
pub fn with_separator(output: &'o mut O, separator: Sep) -> Self {
Self {
inner: SurroundingSeqAccept::new(output, Str("#["), Combined(Str("]"), separator)),
}
}
}
impl<'o, O, Sep> SequenceAccept<O> for AttributesAccept<'o, O, Sep>
where
O: Output,
Sep: Writable<O>,
{
async fn accept<W: Writable<O>>(&mut self, writable: &W) -> Result<(), O::Error> {
self.inner.accept(writable).await
}
}