Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Sugar for universal type (#13)
Browse files Browse the repository at this point in the history
* Add syntactic sugar for the Universal Type
* Fix for is_a macro not understanding parsed types
* Formatting
* Output using syntactic sugar
  • Loading branch information
Cypher1 authored Feb 17, 2022
1 parent 4dc27d3 commit b620acf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ibis/demo.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
["p_de", "d", "read", "Serializable"],
["p_de", "e", "read", "number_or_string"],
["p_f", "f", "write", "ibis.ProductType(name: String, age: Int)"],
["p_g", "g", "read", "name: ibis.UniversalType"],
["p_g", "g", "read", "name: *"],
["p_h", "h", "read", "ibis.ProductType(name: String, age: Int)"],
["p_i", "i", "read", "name: String"],
["p_j", "j", "read", "age: Int"]
Expand Down
9 changes: 6 additions & 3 deletions ibis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ macro_rules! apply {

#[macro_export]
macro_rules! is_a {
($type: expr, $parent: expr) => {
($type.name().starts_with(&($parent.name() + "(")) && $type.name().ends_with(")"))
};
($type: expr, $parent: expr) => {{
use crate::type_parser::read_type;
let name = $type.name();
let ty = read_type(&name);
ty.name == $parent.name()
}};
}

#[macro_export]
Expand Down
5 changes: 4 additions & 1 deletion ibis/src/type_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ fn type_args(input: &str) -> IResult<&str, Vec<Type>> {
}

fn type_structure(input: &str) -> IResult<&str, Type> {
let (input, (name, args)) = tuple((name, opt(type_args)))(input)?;
let (input, (mut name, args)) = tuple((name, opt(type_args)))(input)?;
if name == "*" {
name = "ibis.UniversalType";
}
Ok((input, Type::new(name, args.unwrap_or_default())))
}

Expand Down
10 changes: 9 additions & 1 deletion ibis/src/type_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ impl<'a> std::fmt::Display for Type<'a> {
// write!(f, "{}: ", self.args[0])?;
// format_arg_set(f, " & ", &self.args[1..])
} else {
let res = write!(f, "{}", self.name)?;
let res = write!(
f,
"{}",
if self.name == "ibis.UniversalType" {
"*"
} else {
self.name
}
)?;
if self.args.is_empty() {
Ok(res)
} else {
Expand Down

0 comments on commit b620acf

Please sign in to comment.