Create a codegen macro that generates ftl files from a struct

This commit is contained in:
Savanni D'Gerinel 2024-03-05 09:48:13 -05:00
parent 35c034b04b
commit f8c19b2d18
4 changed files with 66 additions and 0 deletions

4
Cargo.lock generated
View File

@ -2901,6 +2901,10 @@ dependencies = [
"tokio",
]
[[package]]
name = "messages-codegen"
version = "0.1.0"
[[package]]
name = "mime"
version = "0.2.6"

View File

@ -22,6 +22,7 @@ members = [
"kifu/gtk",
"l10n",
"memorycache",
"messages-codegen",
"nom-training",
"result-extended",
"screenplay",

View File

@ -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]

View File

@ -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<String> {
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);
}
}