-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add boilerplate for new cgp-field crates * Add HasField trait * Add symbol! macro * Implement derive_fields macro * Add test for symbol * Test derive HasField * Re-export cgp-field in cgp-core * Reformat imports * Make derive_field a derive macro * Auto derive HasField for contexts that implement Deref * Fix clippy * Fix test
- Loading branch information
1 parent
b76801f
commit 778fb3f
Showing
22 changed files
with
375 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,23 @@ | ||
[package] | ||
name = "cgp-field-macro-lib" | ||
version = "0.1.0" | ||
edition = { workspace = true } | ||
license = { workspace = true } | ||
repository = { workspace = true } | ||
authors = { workspace = true } | ||
rust-version = { workspace = true } | ||
readme = "README.md" | ||
keywords = ["context-generic programming"] | ||
description = """ | ||
Context-generic programming core macros | ||
""" | ||
|
||
[package.metadata.docs.rs] | ||
all-features = true | ||
|
||
[dependencies] | ||
syn = { version = "2.0.37", features = [ "full" ] } | ||
quote = "1.0.33" | ||
proc-macro2 = "1.0.67" | ||
itertools = "0.11.0" | ||
prettyplease = "0.2.20" |
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,58 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::ToTokens; | ||
use syn::{parse_quote, Fields, ItemImpl, ItemStruct}; | ||
|
||
use crate::symbol::symbol_from_string; | ||
|
||
pub fn derive_has_field_impls(item_struct: &ItemStruct) -> Vec<ItemImpl> { | ||
let struct_ident = &item_struct.ident; | ||
|
||
let (impl_generics, ty_generics, where_clause) = item_struct.generics.split_for_impl(); | ||
|
||
let mut item_impls = Vec::new(); | ||
|
||
if let Fields::Named(fields) = &item_struct.fields { | ||
for field in fields.named.iter() { | ||
let field_ident = field.ident.as_ref().unwrap(); | ||
|
||
let field_symbol = symbol_from_string(&field_ident.to_string()); | ||
|
||
let field_type = &field.ty; | ||
|
||
let item_impl: ItemImpl = parse_quote! { | ||
impl #impl_generics HasField< #field_symbol > | ||
for #struct_ident #ty_generics | ||
#where_clause | ||
{ | ||
type Field = #field_type; | ||
|
||
fn get_field( | ||
&self, | ||
key: ::core::marker::PhantomData< #field_symbol >, | ||
) -> &Self::Field | ||
{ | ||
&self. #field_ident | ||
} | ||
} | ||
}; | ||
|
||
item_impls.push(item_impl); | ||
} | ||
} | ||
|
||
item_impls | ||
} | ||
|
||
pub fn derive_fields(input: TokenStream) -> TokenStream { | ||
let item_struct: ItemStruct = syn::parse2(input).unwrap(); | ||
|
||
let item_impls = derive_has_field_impls(&item_struct); | ||
|
||
let mut output = TokenStream::new(); | ||
|
||
for item_impl in item_impls { | ||
output.extend(item_impl.to_token_stream()); | ||
} | ||
|
||
output | ||
} |
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,8 @@ | ||
pub mod field; | ||
pub mod symbol; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
pub use field::derive_fields; | ||
pub use symbol::make_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,23 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::ToTokens; | ||
use syn::punctuated::Punctuated; | ||
use syn::token::Comma; | ||
use syn::{parse_quote, LitStr, Type}; | ||
|
||
pub fn symbol_from_string(value: &str) -> Type { | ||
let char_types = <Punctuated<Type, Comma>>::from_iter( | ||
value | ||
.chars() | ||
.map(|c: char| -> Type { parse_quote!( Char< #c > ) }), | ||
); | ||
|
||
parse_quote!( ( #char_types ) ) | ||
} | ||
|
||
pub fn make_symbol(input: TokenStream) -> TokenStream { | ||
let literal: LitStr = syn::parse2(input).unwrap(); | ||
|
||
let symbol = symbol_from_string(&literal.value()); | ||
|
||
symbol.to_token_stream() | ||
} |
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::field::derive_fields; | ||
use crate::tests::helper::equal::equal_token_stream; | ||
|
||
#[test] | ||
fn test_basic_derive_fields() { | ||
let derived = derive_fields(quote! { | ||
pub struct Foo { | ||
pub bar: Bar, | ||
pub baz: Baz, | ||
} | ||
}); | ||
|
||
let expected = quote! { | ||
impl HasField<(Char<'b'>, Char<'a'>, Char<'r'>)> for Foo { | ||
type Field = Bar; | ||
|
||
fn get_field( | ||
&self, | ||
key: ::core::marker::PhantomData<(Char<'b'>, Char<'a'>, Char<'r'>)>, | ||
) -> &Self::Field { | ||
&self.bar | ||
} | ||
} | ||
|
||
impl HasField<(Char<'b'>, Char<'a'>, Char<'z'>)> for Foo { | ||
type Field = Baz; | ||
|
||
fn get_field( | ||
&self, | ||
key: ::core::marker::PhantomData<(Char<'b'>, Char<'a'>, Char<'z'>)>, | ||
) -> &Self::Field { | ||
&self.baz | ||
} | ||
} | ||
}; | ||
|
||
assert!(equal_token_stream(&derived, &expected)); | ||
} | ||
|
||
#[test] | ||
fn test_generic_derive_fields() { | ||
let derived = derive_fields(quote! { | ||
pub struct Foo<FooParamA, FooParamB: Clone> | ||
where | ||
FooParamA: Eq, | ||
{ | ||
pub bar: Bar<FooParamA>, | ||
pub baz: Baz<String>, | ||
} | ||
}); | ||
|
||
let expected = quote! { | ||
impl<FooParamA, FooParamB: Clone> HasField<(Char<'b'>, Char<'a'>, Char<'r'>)> | ||
for Foo<FooParamA, FooParamB> | ||
where | ||
FooParamA: Eq, | ||
{ | ||
type Field = Bar<FooParamA>; | ||
|
||
fn get_field( | ||
&self, | ||
key: ::core::marker::PhantomData<(Char<'b'>, Char<'a'>, Char<'r'>)>, | ||
) -> &Self::Field { | ||
&self.bar | ||
} | ||
} | ||
|
||
impl<FooParamA, FooParamB: Clone> HasField<(Char<'b'>, Char<'a'>, Char<'z'>)> | ||
for Foo<FooParamA, FooParamB> | ||
where | ||
FooParamA: Eq, | ||
{ | ||
type Field = Baz<String>; | ||
|
||
fn get_field( | ||
&self, | ||
key: ::core::marker::PhantomData<(Char<'b'>, Char<'a'>, Char<'z'>)>, | ||
) -> &Self::Field { | ||
&self.baz | ||
} | ||
} | ||
}; | ||
|
||
assert!(equal_token_stream(&derived, &expected)); | ||
} |
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,7 @@ | ||
use proc_macro2::TokenStream; | ||
|
||
use crate::tests::helper::format::format_token_stream; | ||
|
||
pub fn equal_token_stream(left: &TokenStream, right: &TokenStream) -> bool { | ||
format_token_stream(left) == format_token_stream(right) | ||
} |
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,7 @@ | ||
use prettyplease::unparse; | ||
use proc_macro2::TokenStream; | ||
use syn::parse_file; | ||
|
||
pub fn format_token_stream(stream: &TokenStream) -> String { | ||
unparse(&parse_file(&stream.to_string()).unwrap()) | ||
} |
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,2 @@ | ||
pub mod equal; | ||
pub mod format; |
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,3 @@ | ||
pub mod field; | ||
pub mod helper; | ||
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,31 @@ | ||
use quote::quote; | ||
|
||
use crate::symbol::make_symbol; | ||
use crate::tests::helper::equal::equal_token_stream; | ||
|
||
#[test] | ||
fn test_symbol_macro() { | ||
let symbol = make_symbol(quote!("hello_world")); | ||
|
||
let derived = quote! { | ||
type Symbol = #symbol; | ||
}; | ||
|
||
let expected = quote! { | ||
type Symbol = ( | ||
Char<'h'>, | ||
Char<'e'>, | ||
Char<'l'>, | ||
Char<'l'>, | ||
Char<'o'>, | ||
Char<'_'>, | ||
Char<'w'>, | ||
Char<'o'>, | ||
Char<'r'>, | ||
Char<'l'>, | ||
Char<'d'>, | ||
); | ||
}; | ||
|
||
assert!(equal_token_stream(&derived, &expected)); | ||
} |
Oops, something went wrong.