diff --git a/Cargo.lock b/Cargo.lock index e308ef1..15f3fba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -59,6 +59,16 @@ version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" +[[package]] +name = "dismantler" +version = "0.0.1" +dependencies = [ + "proc-macro2", + "quote", + "rustfmt", + "syn", +] + [[package]] name = "env_logger" version = "0.4.3" @@ -160,9 +170,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.49" +version = "1.0.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57a8eca9f9c4ffde41714334dee777596264c7825420f521abc92b5b5deb63a5" +checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2" dependencies = [ "unicode-ident", ] @@ -313,16 +323,6 @@ dependencies = [ "ucd-util", ] -[[package]] -name = "rust-typed" -version = "0.1.1" -dependencies = [ - "proc-macro2", - "quote", - "rustfmt", - "syn", -] - [[package]] name = "rustc_version" version = "0.2.3" diff --git a/Cargo.toml b/Cargo.toml index b2e79a8..4e0cf41 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,15 +1,14 @@ [package] -name = "rust-typed" -version = "0.1.1" +name = "dismantler" +version = "0.0.1" edition = "2021" description = "Get the types of a struct" license = "MIT OR Apache-2.0" -keywords = ["rust", "type", "type-it", "typed", "protocol"] +keywords = ["type", "dismantle", "typed", "protocol", "dismantler"] authors = ["Viktor Lott"] categories = ["development-tools::procedural-macro-helpers"] repository = "https://github.com/viktorlott/typed" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] proc-macro2 = "1.0.49" diff --git a/README.md b/README.md index 886f85d..3aeabc5 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,62 @@ -# rust-typed +# Dismantler -`Typed` is a procedural macro that is used for disassembling `structs` and `fns` into their inner `type` components that are then accompanied with documentation and examples. The `Typed` structures will be wrapped into a module and reassigned with a name (default `core`), this also goes for the `static` and `generic` fields. +[github](https://github.com/viktorlott/typed) +[crates.io](https://crates.io/crates/dismantle) -#### *Project is still under development* +`Dismantler` is a procedural macro that is used for disassembling `structs` and `fns` into their inner `type` components that are then accompanied with documentation and examples. The `Dismantler` structures will be wrapped into a module and reassigned with a name (default `core`), this also goes for the `static` and `generic` fields. + + +#### **Project is still under development** + +## Examples +```toml +[dependencies] +dismantler = "0.0.1" +``` + +```rust +use dismantler::dismantle; + +#[dismantle] +struct Container { + a: i32, + b: Vec, + c: Vec, + d: C, + e: T +} + +#[dismantle] +struct Tuple(i32, i32); + +#[dismantle] +struct Tuple2(i32, T); + +fn main() { + let a: Container::a = 10; + let b: Container::b = vec![a]; + let c: Container::c = vec![10]; + let c: as Container::protocol>::c = c; + let d: as Container::protocol>::d = 10; + let container: Container::core = Container::core { a, b, c, d, e: 10 }; + + assert!(container.a == a); +} +``` + +### More examples -# Struct example ```rust -#[type_it] +use dismantler::dismantle; + +#[dismantle] struct Containter { current: u8, buffer: Vec, another: T, } -#[type_it] +#[dismantle] struct Area(i32); ``` - Will let you access the struct types as followed: @@ -35,38 +78,16 @@ trait Trait: Container::protocol { fn extend(&mut self, val: Container::protocol::another); } ``` -### More -````rust -#[type_it] -struct Container { - a: i32, - b: Vec, - c: Vec, - d: C, - e: T -} -#[type_it] -struct Tuple(i32, i32); - -#[type_it] -struct Tuple2(i32, T); -fn main() { - let a: Container::fields::a = 10; - let b: Container::b = vec![a]; - let c: Container::c = vec![10]; - let c: as Container::protocol>::c = c; - let d: as Container::protocol>::d = 10; - let container: Container::core = Container::core { a, b, c, d, e: 10 }; +### Current support: + - [x] **Struct** — `static` and `generic` types + - [ ] **Function** — `static` and `generic` types - assert!(container.a == a); -} -```` -# Disassembler +### Disassembler ```rust -#[type_it] +#[dismantle] struct #name { #(#ident: #ty)* } @@ -92,10 +113,12 @@ pub mod #name { } ``` -# Future plans +### Future plans #### Renaming disassembler ```rust -#[type_it = "MContainer"] +use dismantler::dismantle; + +#[dismantle = "MContainer"] struct Containter { current: u8, buffer: Vec, diff --git a/src/builder.rs b/src/builder.rs index 62fa273..f5f456b 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -165,14 +165,16 @@ impl ToTokens for TypeModule { impl ToTokens for TypeModuleInner { fn to_tokens(&self, tokens: &mut TokenStream2) { let type_decls: &Vec = &self.type_decls; + let field_decls = type_decls.iter().map(|t| t.ident.clone()).collect::>(); + let type_decls = quote!(#(#type_decls)*); let struct_decl = &self.struct_decl; let generic_decl = &self.generic_decl; let inner_decls = quote!( - pub mod fields { - #type_decls + pub mod field { + #(pub struct #field_decls;)* } #type_decls diff --git a/src/lib.rs b/src/lib.rs index e86135c..4f8fb4f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,20 +1,22 @@ +#![doc = include_str!("../README.md")] + use proc_macro::TokenStream; -mod tools; mod builder; +mod tools; -/// Use `type_it` as a proc-macro. +/// Use `dismantle` as a proc-macro. /// - *Every field should be documented so it's easier to know what is what.* -/// +/// /// # Example /// ```no_run -/// #[type_it] +/// #[dismantle] /// struct Containter { /// current: u8, /// buffer: Vec, /// another: T, /// } -/// #[type_it] +/// #[dismantle] /// struct Area(i32); /// ``` /// - Will let you access the struct types as followed: @@ -22,7 +24,7 @@ mod builder; /// let current: Container::current = 10; /// let buffer: Container::buffer = vec![current]; /// let another: as Container::protocol>::another = 20; -/// let container: Container::core = +/// let container: Container::core = /// Container::core { /// current, /// buffer, @@ -33,13 +35,11 @@ mod builder; /// ```no_run /// trait Trait: Container::protocol { /// fn retrieve(&self) -> Container::protocol::buffer; -/// fn extend(&mut self, val: Container::protocol::another); +/// fn extend(&mut self, val: Container::protocol::another); /// } /// ``` -/// +/// #[proc_macro_attribute] -pub fn type_it(_attr: TokenStream, item: TokenStream) -> TokenStream { +pub fn dismantle(_attr: TokenStream, item: TokenStream) -> TokenStream { builder::codegen(_attr, item) } - - diff --git a/tests/typed-test.rs b/tests/typed-test.rs index 6c99335..91509cf 100644 --- a/tests/typed-test.rs +++ b/tests/typed-test.rs @@ -1,9 +1,9 @@ #![allow(unused_variables)] -extern crate rust_typed; -use rust_typed::type_it; +extern crate dismantler; +use dismantler::dismantle; -#[type_it] +#[dismantle] struct Container { a: i32, b: Vec, @@ -12,19 +12,22 @@ struct Container { e: T } -#[type_it] +#[dismantle] struct Tuple(i32, i32); -#[type_it] +#[dismantle] struct Tuple2(i32, T); fn main() { - let a: Container::fields::a = 10; + let a: Container::a = 10; let b: Container::b = vec![a]; let c: Container::c = vec![10]; + let c: Container::c = c; let c: as Container::protocol>::c = c; let d: as Container::protocol>::d = 10; let container: Container::core = Container::core { a, b, c, d, e: 10 }; + + assert!(container.a == a); }