use std::sync::Arc; use { ast, SyntaxNode, SyntaxRoot, TreeRoot, AstNode, SyntaxKind::*, }; // ArrayType #[derive(Debug, Clone, Copy)] pub struct ArrayType> { syntax: SyntaxNode, } impl AstNode for ArrayType { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { ARRAY_TYPE => Some(ArrayType { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl ArrayType {} // Attr #[derive(Debug, Clone, Copy)] pub struct Attr> { syntax: SyntaxNode, } impl AstNode for Attr { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { ATTR => Some(Attr { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl Attr { pub fn value(&self) -> Option> { self.syntax() .children() .filter_map(TokenTree::cast) .next() } } // ConstDef #[derive(Debug, Clone, Copy)] pub struct ConstDef> { syntax: SyntaxNode, } impl AstNode for ConstDef { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { CONST_DEF => Some(ConstDef { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl ast::NameOwner for ConstDef {} impl ast::AttrsOwner for ConstDef {} impl ConstDef {} // DynTraitType #[derive(Debug, Clone, Copy)] pub struct DynTraitType> { syntax: SyntaxNode, } impl AstNode for DynTraitType { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { DYN_TRAIT_TYPE => Some(DynTraitType { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl DynTraitType {} // EnumDef #[derive(Debug, Clone, Copy)] pub struct EnumDef> { syntax: SyntaxNode, } impl AstNode for EnumDef { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { ENUM_DEF => Some(EnumDef { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl ast::NameOwner for EnumDef {} impl ast::AttrsOwner for EnumDef {} impl EnumDef {} // File #[derive(Debug, Clone, Copy)] pub struct File> { syntax: SyntaxNode, } impl AstNode for File { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { FILE => Some(File { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl File { pub fn functions<'a>(&'a self) -> impl Iterator> + 'a { self.syntax() .children() .filter_map(FnDef::cast) } } // FnDef #[derive(Debug, Clone, Copy)] pub struct FnDef> { syntax: SyntaxNode, } impl AstNode for FnDef { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { FN_DEF => Some(FnDef { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl ast::NameOwner for FnDef {} impl ast::AttrsOwner for FnDef {} impl FnDef {} // FnPointerType #[derive(Debug, Clone, Copy)] pub struct FnPointerType> { syntax: SyntaxNode, } impl AstNode for FnPointerType { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { FN_POINTER_TYPE => Some(FnPointerType { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl FnPointerType {} // ForType #[derive(Debug, Clone, Copy)] pub struct ForType> { syntax: SyntaxNode, } impl AstNode for ForType { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { FOR_TYPE => Some(ForType { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl ForType {} // ImplItem #[derive(Debug, Clone, Copy)] pub struct ImplItem> { syntax: SyntaxNode, } impl AstNode for ImplItem { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { IMPL_ITEM => Some(ImplItem { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl ImplItem {} // ImplTraitType #[derive(Debug, Clone, Copy)] pub struct ImplTraitType> { syntax: SyntaxNode, } impl AstNode for ImplTraitType { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { IMPL_TRAIT_TYPE => Some(ImplTraitType { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl ImplTraitType {} // Module #[derive(Debug, Clone, Copy)] pub struct Module> { syntax: SyntaxNode, } impl AstNode for Module { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { MODULE => Some(Module { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl ast::NameOwner for Module {} impl ast::AttrsOwner for Module {} impl Module {} // Name #[derive(Debug, Clone, Copy)] pub struct Name> { syntax: SyntaxNode, } impl AstNode for Name { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { NAME => Some(Name { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl Name {} // NameRef #[derive(Debug, Clone, Copy)] pub struct NameRef> { syntax: SyntaxNode, } impl AstNode for NameRef { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { NAME_REF => Some(NameRef { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl NameRef {} // NamedField #[derive(Debug, Clone, Copy)] pub struct NamedField> { syntax: SyntaxNode, } impl AstNode for NamedField { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { NAMED_FIELD => Some(NamedField { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl ast::NameOwner for NamedField {} impl ast::AttrsOwner for NamedField {} impl NamedField {} // NeverType #[derive(Debug, Clone, Copy)] pub struct NeverType> { syntax: SyntaxNode, } impl AstNode for NeverType { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { NEVER_TYPE => Some(NeverType { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl NeverType {} // NominalDef #[derive(Debug, Clone, Copy)] pub enum NominalDef> { StructDef(StructDef), EnumDef(EnumDef), } impl AstNode for NominalDef { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { STRUCT_DEF => Some(NominalDef::StructDef(StructDef { syntax })), ENUM_DEF => Some(NominalDef::EnumDef(EnumDef { syntax })), _ => None, } } fn syntax(&self) -> &SyntaxNode { match self { NominalDef::StructDef(inner) => inner.syntax(), NominalDef::EnumDef(inner) => inner.syntax(), } } } impl ast::AttrsOwner for NominalDef {} impl NominalDef {} // ParenType #[derive(Debug, Clone, Copy)] pub struct ParenType> { syntax: SyntaxNode, } impl AstNode for ParenType { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { PAREN_TYPE => Some(ParenType { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl ParenType {} // PathType #[derive(Debug, Clone, Copy)] pub struct PathType> { syntax: SyntaxNode, } impl AstNode for PathType { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { PATH_TYPE => Some(PathType { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl PathType {} // PlaceholderType #[derive(Debug, Clone, Copy)] pub struct PlaceholderType> { syntax: SyntaxNode, } impl AstNode for PlaceholderType { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { PLACEHOLDER_TYPE => Some(PlaceholderType { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl PlaceholderType {} // PointerType #[derive(Debug, Clone, Copy)] pub struct PointerType> { syntax: SyntaxNode, } impl AstNode for PointerType { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { POINTER_TYPE => Some(PointerType { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl PointerType {} // ReferenceType #[derive(Debug, Clone, Copy)] pub struct ReferenceType> { syntax: SyntaxNode, } impl AstNode for ReferenceType { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { REFERENCE_TYPE => Some(ReferenceType { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl ReferenceType {} // SliceType #[derive(Debug, Clone, Copy)] pub struct SliceType> { syntax: SyntaxNode, } impl AstNode for SliceType { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { SLICE_TYPE => Some(SliceType { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl SliceType {} // StaticDef #[derive(Debug, Clone, Copy)] pub struct StaticDef> { syntax: SyntaxNode, } impl AstNode for StaticDef { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { STATIC_DEF => Some(StaticDef { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl ast::NameOwner for StaticDef {} impl ast::AttrsOwner for StaticDef {} impl StaticDef {} // StructDef #[derive(Debug, Clone, Copy)] pub struct StructDef> { syntax: SyntaxNode, } impl AstNode for StructDef { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { STRUCT_DEF => Some(StructDef { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl ast::NameOwner for StructDef {} impl ast::AttrsOwner for StructDef {} impl StructDef { pub fn fields<'a>(&'a self) -> impl Iterator> + 'a { self.syntax() .children() .filter_map(NamedField::cast) } } // TokenTree #[derive(Debug, Clone, Copy)] pub struct TokenTree> { syntax: SyntaxNode, } impl AstNode for TokenTree { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { TOKEN_TREE => Some(TokenTree { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl TokenTree {} // TraitDef #[derive(Debug, Clone, Copy)] pub struct TraitDef> { syntax: SyntaxNode, } impl AstNode for TraitDef { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { TRAIT_DEF => Some(TraitDef { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl ast::NameOwner for TraitDef {} impl ast::AttrsOwner for TraitDef {} impl TraitDef {} // TupleType #[derive(Debug, Clone, Copy)] pub struct TupleType> { syntax: SyntaxNode, } impl AstNode for TupleType { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { TUPLE_TYPE => Some(TupleType { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl TupleType {} // TypeDef #[derive(Debug, Clone, Copy)] pub struct TypeDef> { syntax: SyntaxNode, } impl AstNode for TypeDef { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { TYPE_DEF => Some(TypeDef { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl ast::NameOwner for TypeDef {} impl ast::AttrsOwner for TypeDef {} impl TypeDef {} // TypeRef #[derive(Debug, Clone, Copy)] pub enum TypeRef> { ParenType(ParenType), TupleType(TupleType), NeverType(NeverType), PathType(PathType), PointerType(PointerType), ArrayType(ArrayType), SliceType(SliceType), ReferenceType(ReferenceType), PlaceholderType(PlaceholderType), FnPointerType(FnPointerType), ForType(ForType), ImplTraitType(ImplTraitType), DynTraitType(DynTraitType), } impl AstNode for TypeRef { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { PAREN_TYPE => Some(TypeRef::ParenType(ParenType { syntax })), TUPLE_TYPE => Some(TypeRef::TupleType(TupleType { syntax })), NEVER_TYPE => Some(TypeRef::NeverType(NeverType { syntax })), PATH_TYPE => Some(TypeRef::PathType(PathType { syntax })), POINTER_TYPE => Some(TypeRef::PointerType(PointerType { syntax })), ARRAY_TYPE => Some(TypeRef::ArrayType(ArrayType { syntax })), SLICE_TYPE => Some(TypeRef::SliceType(SliceType { syntax })), REFERENCE_TYPE => Some(TypeRef::ReferenceType(ReferenceType { syntax })), PLACEHOLDER_TYPE => Some(TypeRef::PlaceholderType(PlaceholderType { syntax })), FN_POINTER_TYPE => Some(TypeRef::FnPointerType(FnPointerType { syntax })), FOR_TYPE => Some(TypeRef::ForType(ForType { syntax })), IMPL_TRAIT_TYPE => Some(TypeRef::ImplTraitType(ImplTraitType { syntax })), DYN_TRAIT_TYPE => Some(TypeRef::DynTraitType(DynTraitType { syntax })), _ => None, } } fn syntax(&self) -> &SyntaxNode { match self { TypeRef::ParenType(inner) => inner.syntax(), TypeRef::TupleType(inner) => inner.syntax(), TypeRef::NeverType(inner) => inner.syntax(), TypeRef::PathType(inner) => inner.syntax(), TypeRef::PointerType(inner) => inner.syntax(), TypeRef::ArrayType(inner) => inner.syntax(), TypeRef::SliceType(inner) => inner.syntax(), TypeRef::ReferenceType(inner) => inner.syntax(), TypeRef::PlaceholderType(inner) => inner.syntax(), TypeRef::FnPointerType(inner) => inner.syntax(), TypeRef::ForType(inner) => inner.syntax(), TypeRef::ImplTraitType(inner) => inner.syntax(), TypeRef::DynTraitType(inner) => inner.syntax(), } } } impl TypeRef {}