-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce new
cgp-field
constructs (#36)
* Add field, product, and sum types * Modify symbol! macro to use Cons and Nil to construct product * Implement parse_product macro function * Separate product type and product expr macro * Export product! and Product! macros * Add Sum! macro * Update derive(HasField) macro to use Value instead * Add changelog
- Loading branch information
1 parent
473260b
commit fdeaa90
Showing
18 changed files
with
250 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use syn::parse::{Parse, ParseStream}; | ||
use syn::punctuated::Punctuated; | ||
use syn::token::Comma; | ||
use syn::{Expr, Type}; | ||
|
||
pub struct ParsePunctuated<T>(pub Punctuated<T, Comma>); | ||
|
||
impl<T: Parse> Parse for ParsePunctuated<T> { | ||
fn parse(input: ParseStream) -> syn::Result<Self> { | ||
let types = Punctuated::parse_terminated(input)?; | ||
Ok(ParsePunctuated(types)) | ||
} | ||
} | ||
|
||
pub fn make_product_type(input: TokenStream) -> TokenStream { | ||
let types: ParsePunctuated<Type> = syn::parse2(input).unwrap(); | ||
|
||
types.0.iter().rfold(quote! { Nil }, |res, item| { | ||
quote! { | ||
Cons< #item , #res > | ||
} | ||
}) | ||
} | ||
|
||
pub fn make_sum_type(input: TokenStream) -> TokenStream { | ||
let types: ParsePunctuated<Type> = syn::parse2(input).unwrap(); | ||
|
||
types.0.iter().rfold(quote! { Void }, |res, item| { | ||
quote! { | ||
Either< #item , #res > | ||
} | ||
}) | ||
} | ||
|
||
pub fn make_product_expr(input: TokenStream) -> TokenStream { | ||
let types: ParsePunctuated<Expr> = syn::parse2(input).unwrap(); | ||
|
||
types.0.iter().rfold(quote! { Nil }, |res, item| { | ||
quote! { | ||
Cons( #item , #res ) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod field; | ||
pub mod helper; | ||
pub mod product; | ||
pub mod symbol; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use quote::quote; | ||
|
||
use crate::product::{make_product_expr, make_product_type, make_sum_type}; | ||
|
||
#[test] | ||
fn test_product_type() { | ||
let derived = make_product_type(quote! { | ||
Foo, | ||
Bar<T>, | ||
Baz<T, U>, | ||
}); | ||
|
||
let expected = quote! { | ||
Cons< | ||
Foo, | ||
Cons< | ||
Bar<T>, | ||
Cons< | ||
Baz<T, U>, | ||
Nil> > > | ||
}; | ||
|
||
assert_eq!(derived.to_string(), expected.to_string()); | ||
} | ||
|
||
#[test] | ||
fn test_product_ident() { | ||
let derived = make_product_expr(quote! { | ||
foo, | ||
bar, | ||
baz, | ||
}); | ||
|
||
let expected = quote! { | ||
Cons( | ||
foo, | ||
Cons( | ||
bar, | ||
Cons( | ||
baz, | ||
Nil ) ) ) | ||
}; | ||
|
||
assert_eq!(derived.to_string(), expected.to_string()); | ||
} | ||
|
||
#[test] | ||
fn test_product_expr() { | ||
let derived = make_product_expr(quote! { | ||
foo.0, | ||
Bar { bar }, | ||
Baz::baz(), | ||
}); | ||
|
||
let expected = quote! { | ||
Cons( | ||
foo.0, | ||
Cons( | ||
Bar { bar }, | ||
Cons( | ||
Baz::baz(), | ||
Nil ) ) ) | ||
}; | ||
|
||
assert_eq!(derived.to_string(), expected.to_string()); | ||
} | ||
|
||
#[test] | ||
fn test_sum_type() { | ||
let derived = make_sum_type(quote! { | ||
Foo, | ||
Bar<T>, | ||
Baz<T, U>, | ||
}); | ||
|
||
let expected = quote! { | ||
Either< | ||
Foo, | ||
Either< | ||
Bar<T>, | ||
Either< | ||
Baz<T, U>, | ||
Void> > > | ||
}; | ||
|
||
assert_eq!(derived.to_string(), expected.to_string()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.