Skip to content

Commit

Permalink
Validator trait
Browse files Browse the repository at this point in the history
  • Loading branch information
notdanilo committed Nov 24, 2023
1 parent 77a789e commit e97a261
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 21 deletions.
7 changes: 4 additions & 3 deletions ecosystem/python/parser/src/function/method/validator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::prelude::*;
use ligen::parser::ParserConfig;
use ligen::parser::{ParserConfig, Validator};
use ligen::ir::Method;

#[derive(Default)]
Expand All @@ -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(())
}
}
17 changes: 9 additions & 8 deletions ecosystem/python/parser/src/interface/validator.rs
Original file line number Diff line number Diff line change
@@ -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 {}
Expand All @@ -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)| {
Expand All @@ -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(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<WithSource<&Vec<Expr>>> for AttributesParser {
Expand Down Expand Up @@ -46,8 +48,8 @@ impl Parser<WithSource<&Keyword>> 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())
}
}
Expand All @@ -70,7 +72,7 @@ impl Parser<WithSource<&Expr>> 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())
}
Expand Down
2 changes: 1 addition & 1 deletion ecosystem/python/parser/src/parser/validator.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
12 changes: 7 additions & 5 deletions ecosystem/python/parser/src/types/type_/validator.rs
Original file line number Diff line number Diff line change
@@ -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 {}
Expand All @@ -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<Type>, 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(())
}
}
}
5 changes: 4 additions & 1 deletion ligen/parser/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
pub mod prelude;
pub mod assert;
pub mod utils;
mod validator;
mod parser;
pub use parser::*;

pub use parser::*;
pub use validator::*;
6 changes: 6 additions & 0 deletions ligen/parser/src/validator/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::{prelude::*, ParserConfig};

pub trait Validator {
type Input;
fn validate(&self, input: &mut Self::Input, config: &ParserConfig) -> Result<()>;
}

0 comments on commit e97a261

Please sign in to comment.