diff --git a/ecosystem/python/parser/src/function/method/validator.rs b/ecosystem/python/parser/src/function/method/validator.rs index fdeaeead..e3ad18c6 100644 --- a/ecosystem/python/parser/src/function/method/validator.rs +++ b/ecosystem/python/parser/src/function/method/validator.rs @@ -1,5 +1,5 @@ use crate::prelude::*; -use ligen::parser::ParserConfig; +use ligen::parser::{ParserConfig, Validator}; use ligen::ir::Method; #[derive(Default)] @@ -13,8 +13,9 @@ impl MethodValidator { } } -impl MethodValidator { - pub fn validate(&self, _method: &mut Method, _config: &ParserConfig) -> Result<()> { +impl Validator for MethodValidator { + type Input = Method; + fn validate(&self, _method: &mut Method, _config: &ParserConfig) -> Result<()> { Ok(()) } } \ No newline at end of file diff --git a/ecosystem/python/parser/src/interface/validator.rs b/ecosystem/python/parser/src/interface/validator.rs index 3a557330..72ea96ba 100644 --- a/ecosystem/python/parser/src/interface/validator.rs +++ b/ecosystem/python/parser/src/interface/validator.rs @@ -1,6 +1,6 @@ use crate::prelude::*; use ligen::ir::{Interface, Function, Identifier}; -use ligen::parser::ParserConfig; +use ligen::parser::{ParserConfig, Validator}; #[derive(Default)] pub struct InterfaceValidator {} @@ -9,13 +9,6 @@ impl InterfaceValidator { pub fn new() -> Self { Default::default() } -} - -impl InterfaceValidator { - pub fn validate(&self, interface: &mut Interface, config: &ParserConfig) -> Result<()> { - self.validate_constructor(interface, config)?; - Ok(()) - } fn validate_constructor(&self, interface: &mut Interface, _config: &ParserConfig) -> Result<()> { let indices = interface.methods.iter().enumerate().filter_map(|(i, method)| { @@ -35,4 +28,12 @@ impl InterfaceValidator { } Ok(()) } +} + +impl Validator for InterfaceValidator { + type Input = Interface; + fn validate(&self, interface: &mut Interface, config: &ParserConfig) -> Result<()> { + self.validate_constructor(interface, config)?; + Ok(()) + } } \ No newline at end of file diff --git a/ecosystem/python/parser/src/macro_attributes/attributes/mod.rs b/ecosystem/python/parser/src/macro_attributes/attributes/mod.rs index 6101476b..55c94ac5 100644 --- a/ecosystem/python/parser/src/macro_attributes/attributes/mod.rs +++ b/ecosystem/python/parser/src/macro_attributes/attributes/mod.rs @@ -11,6 +11,8 @@ use rustpython_parser::ast::{Expr, Keyword}; #[derive(Default)] pub struct AttributesParser { path_parser: PathParser, + identifier_parser: IdentifierParser, + literal_parser: LiteralParser, } impl Parser>> for AttributesParser { @@ -46,8 +48,8 @@ impl Parser> for AttributesParser { .as_ref() .map(|arg| arg.to_string()) .ok_or_else(|| Error::Message("Failed to parse attribute name".to_string()))?; - let identifier = IdentifierParser::default().parse(name, config)?; - let literal = LiteralParser::default().parse(&input.ast.value, config)?; + let identifier = self.identifier_parser.parse(name, config)?; + let literal = self.literal_parser.parse(&input.ast.value, config)?; Ok(Named::new(identifier, literal).into()) } } @@ -70,7 +72,7 @@ impl Parser> for AttributesParser { }, Expr::Attribute(expr) => { let name = expr.attr.to_string(); - let identifier = IdentifierParser::default().parse(name, config)?; + let identifier = self.identifier_parser.parse(name, config)?; let attributes = Attributes::default(); Ok(Group::new(identifier, attributes).into()) } diff --git a/ecosystem/python/parser/src/parser/validator.rs b/ecosystem/python/parser/src/parser/validator.rs index c900e1ed..4f34ae72 100644 --- a/ecosystem/python/parser/src/parser/validator.rs +++ b/ecosystem/python/parser/src/parser/validator.rs @@ -1,6 +1,6 @@ use crate::{prelude::*, function::method::validator::MethodValidator, interface::validator::InterfaceValidator}; use is_tree::prelude::*; -use ligen::{ir::{Library, Type, Method, Interface}, parser::ParserConfig}; +use ligen::{ir::{Library, Type, Method, Interface}, parser::{ParserConfig, Validator}}; use crate::types::type_::TypeValidator; #[derive(Default)] diff --git a/ecosystem/python/parser/src/types/type_/validator.rs b/ecosystem/python/parser/src/types/type_/validator.rs index cd6a37e3..2e65452f 100644 --- a/ecosystem/python/parser/src/types/type_/validator.rs +++ b/ecosystem/python/parser/src/types/type_/validator.rs @@ -1,5 +1,6 @@ use crate::prelude::*; -use ligen::{ir::{Type, Path}, parser::{ParserConfig, ParserConfigGet}}; +use is_tree::Visitor; +use ligen::{ir::{Type, Path}, parser::{ParserConfig, ParserConfigGet, Validator}}; #[derive(Default)] pub struct TypeValidator {} @@ -10,13 +11,14 @@ impl TypeValidator { } } -impl TypeValidator { - pub fn validate(&self, type_: &mut Type, config: &ParserConfig) -> Result<()> { +impl Validator for TypeValidator { + type Input = Type; + fn validate(&self, type_: &mut Visitor, config: &ParserConfig) -> Result<()> { let name = type_.path.last().identifier.name.as_str(); - // TODO: Move it to a validation step. It's hard to find it here. if config.get(Path::from("ligen::python::as-opaque").join(name)).is_some() { + println!("{}", type_.path); *type_ = Type::opaque(); } Ok(()) } -} \ No newline at end of file +} diff --git a/ligen/parser/src/lib.rs b/ligen/parser/src/lib.rs index 5e7e2ff2..8ffac148 100644 --- a/ligen/parser/src/lib.rs +++ b/ligen/parser/src/lib.rs @@ -1,5 +1,8 @@ pub mod prelude; pub mod assert; pub mod utils; +mod validator; mod parser; -pub use parser::*; \ No newline at end of file + +pub use parser::*; +pub use validator::*; \ No newline at end of file diff --git a/ligen/parser/src/validator/mod.rs b/ligen/parser/src/validator/mod.rs new file mode 100644 index 00000000..d8b9b8e8 --- /dev/null +++ b/ligen/parser/src/validator/mod.rs @@ -0,0 +1,6 @@ +use crate::{prelude::*, ParserConfig}; + +pub trait Validator { + type Input; + fn validate(&self, input: &mut Self::Input, config: &ParserConfig) -> Result<()>; +} \ No newline at end of file