From f8c19b2d18e4f726967cbef17e35483ef9b298eb Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Tue, 5 Mar 2024 09:48:13 -0500 Subject: [PATCH] Create a codegen macro that generates ftl files from a struct --- Cargo.lock | 4 +++ Cargo.toml | 1 + messages-codegen/Cargo.toml | 8 ++++++ messages-codegen/src/lib.rs | 53 +++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 messages-codegen/Cargo.toml create mode 100644 messages-codegen/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index c6632ee..5d300d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2901,6 +2901,10 @@ dependencies = [ "tokio", ] +[[package]] +name = "messages-codegen" +version = "0.1.0" + [[package]] name = "mime" version = "0.2.6" diff --git a/Cargo.toml b/Cargo.toml index b33659e..d4565b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ members = [ "kifu/gtk", "l10n", "memorycache", + "messages-codegen", "nom-training", "result-extended", "screenplay", diff --git a/messages-codegen/Cargo.toml b/messages-codegen/Cargo.toml new file mode 100644 index 0000000..426f5d7 --- /dev/null +++ b/messages-codegen/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "messages-codegen" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/messages-codegen/src/lib.rs b/messages-codegen/src/lib.rs new file mode 100644 index 0000000..6e5422d --- /dev/null +++ b/messages-codegen/src/lib.rs @@ -0,0 +1,53 @@ +/* Given that fluent doesn't provide an ability to see all of the placeholders within a message, + * and that the placeholders may change depending on the values of others, I don't have a way to + * ensure that the translation code remains entirely connected to the source messages themselves. + * + * Given that I can't really enforce it, I can, perhaps, codegen it in a way that at least the two + * are right next to one another. A macro that generates the data structure and can write the + * source strings file. + * + * For overall file structure, I want the messages, L10N, and FluentErgo tied together. + */ + +#[macro_export] +macro_rules! messages { + ($($name:ident($($arg:ident: $argtype:ty)*) => $message:literal,)+) => { + pub enum Messages { + $($name($name)),+ + } + + impl Messages { + fn gen_strings() -> Vec { + vec![ + $(concat!(stringify!($name), " => ", $message).to_string(),)+ + ] + } + } + + $(pub struct $name { + $($arg: $argtype)* + })+ + } +} + +#[cfg(test)] +mod test { + use super::*; + + messages! { + Welcome(name: String) => "Hello, ${name}", + GamesInDatabase(count: usize) => "{$count -> + [one] There is one game in the database + *[other] There are ${count} games in the database +}", + } + + #[test] + fn it_can_generate_an_ftl() { + for s in Messages::gen_strings() { + println!("{}", s); + } + + assert!(false); + } +}