diff options
| author | eichlan <eichlan@jotunn.office.penny-arcade.com> | 2026-06-23 15:31:51 -0700 |
|---|---|---|
| committer | eichlan <eichlan@jotunn.office.penny-arcade.com> | 2026-06-23 15:31:51 -0700 |
| commit | 062048ef6e3e060bcf841ea2ec92e026f09d8da0 (patch) | |
| tree | 22f7dad062b16f25fcfa1a28af13ac2937348ef0 | |
| parent | 7c299c86bbac76ec2ac199d68eb4dda093a64e3a (diff) | |
| download | crimtag-062048ef6e3e060bcf841ea2ec92e026f09d8da0.tar.gz crimtag-062048ef6e3e060bcf841ea2ec92e026f09d8da0.tar.bz2 crimtag-062048ef6e3e060bcf841ea2ec92e026f09d8da0.tar.xz crimtag-062048ef6e3e060bcf841ea2ec92e026f09d8da0.zip | |
Roorganized into workspace and packages.
This is to accomidate a new proc_macro package, which can't have
anything else in it.
| -rw-r--r-- | Cargo.lock | 7 | ||||
| -rw-r--r-- | Cargo.toml | 11 | ||||
| -rw-r--r-- | crimtag/Cargo.lock | 7 | ||||
| -rw-r--r-- | crimtag/Cargo.toml | 6 | ||||
| -rw-r--r-- | crimtag/src/context.rs (renamed from src/context.rs) | 0 | ||||
| -rw-r--r-- | crimtag/src/error.rs (renamed from src/error.rs) | 0 | ||||
| -rw-r--r-- | crimtag/src/lexer.rs (renamed from src/lexer.rs) | 0 | ||||
| -rw-r--r-- | crimtag/src/lib.rs (renamed from src/lib.rs) | 0 | ||||
| -rw-r--r-- | crimtag/src/parser.rs (renamed from src/parser.rs) | 0 | ||||
| -rw-r--r-- | crimtag/src/position.rs (renamed from src/position.rs) | 0 | ||||
| -rw-r--r-- | derive/Cargo.toml | 10 | ||||
| -rw-r--r-- | derive/src/lib.rs | 75 | ||||
| -rw-r--r-- | derive/tests/basic.rs | 15 |
13 files changed, 126 insertions, 5 deletions
| @@ -5,3 +5,10 @@ version = 4 | |||
| 5 | [[package]] | 5 | [[package]] |
| 6 | name = "crimtag" | 6 | name = "crimtag" |
| 7 | version = "0.1.0" | 7 | version = "0.1.0" |
| 8 | |||
| 9 | [[package]] | ||
| 10 | name = "derive" | ||
| 11 | version = "0.1.0" | ||
| 12 | dependencies = [ | ||
| 13 | "crimtag", | ||
| 14 | ] | ||
| @@ -1,6 +1,7 @@ | |||
| 1 | [package] | 1 | [workspace] |
| 2 | name = "crimtag" | 2 | members = ["derive","crimtag"] |
| 3 | version = "0.1.0" | 3 | default-members = ["derive","crimtag"] |
| 4 | edition = "2024" | 4 | resolver = "3" |
| 5 | 5 | ||
| 6 | [dependencies] | 6 | [workspace.dependencies] |
| 7 | crimtag = { path = "crimtag"} | ||
diff --git a/crimtag/Cargo.lock b/crimtag/Cargo.lock new file mode 100644 index 0000000..f7e6686 --- /dev/null +++ b/crimtag/Cargo.lock | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | # This file is automatically @generated by Cargo. | ||
| 2 | # It is not intended for manual editing. | ||
| 3 | version = 4 | ||
| 4 | |||
| 5 | [[package]] | ||
| 6 | name = "crimtag" | ||
| 7 | version = "0.1.0" | ||
diff --git a/crimtag/Cargo.toml b/crimtag/Cargo.toml new file mode 100644 index 0000000..ee022ca --- /dev/null +++ b/crimtag/Cargo.toml | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | [package] | ||
| 2 | name = "crimtag" | ||
| 3 | version = "0.1.0" | ||
| 4 | edition = "2024" | ||
| 5 | |||
| 6 | [dependencies] | ||
diff --git a/src/context.rs b/crimtag/src/context.rs index 6f8e04f..6f8e04f 100644 --- a/src/context.rs +++ b/crimtag/src/context.rs | |||
diff --git a/src/error.rs b/crimtag/src/error.rs index bbb5d6f..bbb5d6f 100644 --- a/src/error.rs +++ b/crimtag/src/error.rs | |||
diff --git a/src/lexer.rs b/crimtag/src/lexer.rs index d4d39e9..d4d39e9 100644 --- a/src/lexer.rs +++ b/crimtag/src/lexer.rs | |||
diff --git a/src/lib.rs b/crimtag/src/lib.rs index e8f9e9f..e8f9e9f 100644 --- a/src/lib.rs +++ b/crimtag/src/lib.rs | |||
diff --git a/src/parser.rs b/crimtag/src/parser.rs index 4f7ff6a..4f7ff6a 100644 --- a/src/parser.rs +++ b/crimtag/src/parser.rs | |||
diff --git a/src/position.rs b/crimtag/src/position.rs index 24f5309..24f5309 100644 --- a/src/position.rs +++ b/crimtag/src/position.rs | |||
diff --git a/derive/Cargo.toml b/derive/Cargo.toml new file mode 100644 index 0000000..65f3a75 --- /dev/null +++ b/derive/Cargo.toml | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | [package] | ||
| 2 | name = "derive" | ||
| 3 | version = "0.1.0" | ||
| 4 | edition = "2024" | ||
| 5 | |||
| 6 | [lib] | ||
| 7 | proc-macro = true | ||
| 8 | |||
| 9 | [dev-dependencies] | ||
| 10 | crimtag = { path = "../crimtag"} | ||
diff --git a/derive/src/lib.rs b/derive/src/lib.rs new file mode 100644 index 0000000..f9c9180 --- /dev/null +++ b/derive/src/lib.rs | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | extern crate proc_macro; | ||
| 2 | use proc_macro::{TokenStream,TokenTree,Ident,Delimiter}; | ||
| 3 | |||
| 4 | #[proc_macro_derive(CrimtagMappable)] | ||
| 5 | pub fn derive_crimtag_mappable(s: TokenStream) -> TokenStream { | ||
| 6 | let mut i = s.into_iter(); | ||
| 7 | while let Some(t) = i.next() { | ||
| 8 | if let TokenTree::Ident(ident) = t && | ||
| 9 | ident.to_string() == "struct" { | ||
| 10 | break; | ||
| 11 | } | ||
| 12 | } | ||
| 13 | let name = if let Some(TokenTree::Ident(ident)) = i.next() { | ||
| 14 | ident.to_string() | ||
| 15 | } else { | ||
| 16 | panic!("No name found in struct."); | ||
| 17 | }; | ||
| 18 | |||
| 19 | // TODO: Expand this to include the generic parameters and the where? at | ||
| 20 | // least the parameters. | ||
| 21 | println!("struct name: {:?}", name); | ||
| 22 | |||
| 23 | let mut newfunc = format!( | ||
| 24 | r#"impl<'a> crimtag::MappedStructure<'a> for {} {{ | ||
| 25 | fn get_value(&'a self, id: &str) -> Option<MappedValue<'a>> {{ | ||
| 26 | match id {{ | ||
| 27 | "#, name); | ||
| 28 | |||
| 29 | // We're going to cheat. Every field name is followed directly by a colon, | ||
| 30 | // so all we really need to do is hunt for colons and then take the | ||
| 31 | // previous item. | ||
| 32 | |||
| 33 | while let Some(n) = i.next() { | ||
| 34 | if let TokenTree::Group(g) = &n && | ||
| 35 | g.delimiter() == Delimiter::Brace { | ||
| 36 | // Operate on the struct members. | ||
| 37 | let mut gi = g.stream().into_iter(); | ||
| 38 | let mut cur = 0usize; | ||
| 39 | let mut tok = [gi.next(), gi.next()]; | ||
| 40 | loop { | ||
| 41 | if let Some(TokenTree::Punct(ch)) = &tok[(cur+1)%2] && | ||
| 42 | ch.as_char() == ':' && | ||
| 43 | let Some(TokenTree::Ident(id)) = &tok[cur] { | ||
| 44 | println!("!!!!!!!!!! -> {}", id); | ||
| 45 | newfunc.push_str(&format!( | ||
| 46 | r#" "{}" => Some(self.{}.into()), | ||
| 47 | "#, id, id)); | ||
| 48 | } | ||
| 49 | println!(" - {:?} {:?}", tok[cur], tok[(cur+1)%2] ); | ||
| 50 | // Update next. | ||
| 51 | if let Some(ni) = gi.next() { | ||
| 52 | tok[cur].replace( ni ); | ||
| 53 | cur = (cur+1)%2; | ||
| 54 | } else { | ||
| 55 | break; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | newfunc.push_str( | ||
| 62 | r#" _ => None, | ||
| 63 | } | ||
| 64 | } | ||
| 65 | }"#); | ||
| 66 | |||
| 67 | println!(); | ||
| 68 | println!(); | ||
| 69 | println!("{}", newfunc); | ||
| 70 | println!(); | ||
| 71 | println!(); | ||
| 72 | |||
| 73 | newfunc.parse().unwrap() | ||
| 74 | } | ||
| 75 | |||
diff --git a/derive/tests/basic.rs b/derive/tests/basic.rs new file mode 100644 index 0000000..18fc366 --- /dev/null +++ b/derive/tests/basic.rs | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | use derive::*; | ||
| 2 | use crimtag::*; | ||
| 3 | |||
| 4 | #[derive(CrimtagMappable)] | ||
| 5 | pub struct Test { | ||
| 6 | number: i32, | ||
| 7 | something: f64, | ||
| 8 | inner_thing: String, | ||
| 9 | } | ||
| 10 | |||
| 11 | |||
| 12 | |||
| 13 | #[test] | ||
| 14 | fn it_works() { | ||
| 15 | } | ||
