Skip to content

Commit

Permalink
Adding generics to TypeDefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
notdanilo committed Nov 2, 2023
1 parent a3a7f3a commit 71fb103
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ impl Parser<WithSource<StmtClassDef>> for FullParser {
let visibility = Visibility::Public;
let interfaces = self.parse_interfaces(&input.ast.bases)?;
let definition = self.parse_kind_definition(&input)?;
Ok(TypeDefinition { attributes, visibility, identifier, definition, interfaces })
let generics = Default::default();
Ok(TypeDefinition { attributes, visibility, identifier, generics, definition, interfaces })
}
}

Expand Down
13 changes: 10 additions & 3 deletions ecosystem/rust/parser/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ use ligen::parsing::parser::Parser;
use crate::identifier::IdentifierParser;
use crate::prelude::*;

pub struct PathParser;
#[derive(Default)]
pub struct PathParser {}

impl PathParser {
pub fn new() -> Self {
Default::default()
}
}

impl Parser<syn::Path> for PathParser {
type Output = Path;
Expand Down Expand Up @@ -53,14 +60,14 @@ mod test {

#[test]
fn identifier_as_path() -> Result<()> {
assert_eq(PathParser, mock::identifier_as_path(), quote! {
assert_eq(PathParser::default(), mock::identifier_as_path(), quote! {
u8
})
}

#[test]
fn path() -> Result<()> {
assert_eq(PathParser, mock::path(), quote! {
assert_eq(PathParser::default(), mock::path(), quote! {
std::convert::TryFrom
})
}
Expand Down
22 changes: 21 additions & 1 deletion ecosystem/rust/parser/src/types/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ use ligen::parsing::parser::Parser;
use crate::prelude::*;
use crate::types::type_::TypeParser;

pub struct GenericsParser;
#[derive(Default)]
pub struct GenericsParser {}

impl GenericsParser {
pub fn new() -> Self {
Default::default()
}
}

impl Parser<syn::PathArguments> for GenericsParser {
type Output = Generics;
Expand All @@ -24,3 +31,16 @@ impl Parser<syn::PathArguments> for GenericsParser {
Ok(Self::Output { types })
}
}

impl Parser<syn::Generics> for GenericsParser {
type Output = Generics;
fn parse(&self, input: syn::Generics) -> Result<Self::Output> {
let mut generics = Generics::default();
for generic in input.params {
if let syn::GenericParam::Type(type_) = generic {
generics.types.push(TypeParser.parse(type_.ident)?);
}
}
Ok(generics)
}
}
2 changes: 0 additions & 2 deletions ecosystem/rust/parser/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
mod type_definition;
mod type_;
mod primitive;
mod generics;

pub use type_definition::*;
pub use type_::*;
pub use primitive::*;
pub use generics::*;
9 changes: 8 additions & 1 deletion ecosystem/rust/parser/src/types/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ use crate::path::PathParser;

pub struct TypeParser;

impl Parser<syn::Ident> for TypeParser {
type Output = Type;
fn parse(&self, input: syn::Ident) -> Result<Self::Output> {
Ok(Type::Path(PathParser::default().parse(input)?))
}
}

impl Parser<syn::Path> for TypeParser {
type Output = Type;
fn parse(&self, path: syn::Path) -> Result<Self::Output> {
let mut path = PathParser.parse(path)?;
let mut path = PathParser::default().parse(path)?;
if path.segments.len() == 1 {
let segment = path.first_mut();
match segment.identifier.name.as_str() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub mod variant;

use crate::prelude::*;
use crate::types::GenericsParser;
use ligen::ir::{Enumeration, TypeDefinition};
use ligen::parsing::parser::Parser;
use crate::identifier::IdentifierParser;
Expand All @@ -28,7 +29,8 @@ impl Parser<syn::ItemEnum> for EnumerationParser {
let interfaces = Default::default();
let variants = VariantParser.parse(enumeration.variants)?;
let definition = Enumeration { variants }.into();
Ok(TypeDefinition { attributes, visibility, identifier, interfaces, definition })
let generics = GenericsParser::default().parse(enumeration.generics)?;
Ok(TypeDefinition { attributes, visibility, identifier, generics, interfaces, definition })
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod field;
pub use field::*;

use crate::prelude::*;
use crate::types::GenericsParser;
use ligen::ir::{Structure, TypeDefinition};
use ligen::parsing::parser::Parser;
use crate::identifier::IdentifierParser;
Expand Down Expand Up @@ -45,7 +46,8 @@ impl Parser<syn::ItemStruct> for StructureParser {
let interfaces = Default::default();
let fields = FieldParser.parse(structure.fields)?;
let definition = Structure { fields }.into();
Ok(Self::Output { attributes, visibility, identifier, interfaces, definition })
let generics = GenericsParser::default().parse(structure.generics)?;
Ok(Self::Output { attributes, visibility, identifier, generics, interfaces, definition })
}
}

Expand Down
4 changes: 3 additions & 1 deletion ligen/ir/src/types/type_definition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod kind_definition;

pub use kind_definition::*;

use crate::{prelude::*, Attributes, Visibility, Path};
use crate::{prelude::*, Attributes, Visibility, Path, Generics};
use crate::Identifier;

/// All the possible ways to define a type.
Expand All @@ -16,6 +16,8 @@ pub struct TypeDefinition {
pub visibility: Visibility,
/// Definition identifier.
pub identifier: Identifier,
/// Generic parameters.
pub generics: Generics,
/// Interfaces that this definition implements.
pub interfaces: Vec<Path>,
/// Specific definition of the kind (e.g. Structure, Enumeration).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn join_path(inputs: &Inputs) -> String {
.get(1)
.and_then(|input| serde_json::from_value::<Path>(input).ok());
if let (Some(separator), Some(path)) = (separator, path) {
path.to_string_with_separator(&separator)
path.to_string_with_separator(separator)
} else {
"<ligen:join_path error>".to_string()
}
Expand Down

0 comments on commit 71fb103

Please sign in to comment.