Move the rust codegen code into l10n
This commit is contained in:
parent
137a88ad8e
commit
4cfc6425e1
|
@ -23,7 +23,6 @@ members = [
|
||||||
"kifu/l10n",
|
"kifu/l10n",
|
||||||
"l10n",
|
"l10n",
|
||||||
"memorycache",
|
"memorycache",
|
||||||
"messages-codegen",
|
|
||||||
"nom-training",
|
"nom-training",
|
||||||
"result-extended",
|
"result-extended",
|
||||||
"screenplay",
|
"screenplay",
|
||||||
|
|
|
@ -130,6 +130,5 @@ fn main() {
|
||||||
for message in messages {
|
for message in messages {
|
||||||
println!();
|
println!();
|
||||||
println!("{}", message.rust_code());
|
println!("{}", message.rust_code());
|
||||||
// println!("{}", message.content());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
fn main() {
|
||||||
|
println!("msggen running");
|
||||||
|
}
|
|
@ -1,16 +0,0 @@
|
||||||
[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
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
proc-macro = true
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
convert_case = { version = "0.6" }
|
|
||||||
quote = { version = "*" }
|
|
||||||
syn = { version = "*" }
|
|
||||||
serde_yaml = { version = "*" }
|
|
||||||
serde = { version = "*", features = [ "derive" ] }
|
|
|
@ -1,87 +0,0 @@
|
||||||
/* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
use proc_macro::TokenStream;
|
|
||||||
use quote::quote;
|
|
||||||
use syn;
|
|
||||||
|
|
||||||
/* I want to write a macro that reads something like this:
|
|
||||||
* 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
|
|
||||||
* }",
|
|
||||||
*
|
|
||||||
* It generates an enumeration with all of the named values (Welcome, GamesInDatabase), it
|
|
||||||
* generates corresponding structures (Welcome{ name: String }, GamesInDatabase{ count: usize }),
|
|
||||||
* and it generates two implementations. One is an implementation that can be used to write all of
|
|
||||||
* the strings after => into a file. The other is an implementation that can be imported into a
|
|
||||||
* program, but which *does not* contain the strings. That would look more like this:
|
|
||||||
*
|
|
||||||
* messages.rs:
|
|
||||||
*
|
|
||||||
* enum Messages {
|
|
||||||
* Welcome(Welcome),
|
|
||||||
* GamesInDatabase(GamesInDatabase),
|
|
||||||
* }
|
|
||||||
*
|
|
||||||
* struct Welcome{ name: String }
|
|
||||||
* struct GamesInDatabase{ count: usize }
|
|
||||||
*
|
|
||||||
* message_strings.ftl:
|
|
||||||
*
|
|
||||||
* welcome = "Hello, ${name}",
|
|
||||||
* games-in-database = "{$count ->
|
|
||||||
* [one] There is one game in the database
|
|
||||||
* *[other] There are ${count} games in the database
|
|
||||||
* }",
|
|
||||||
*
|
|
||||||
* messages.rs can be imported into the resulting program, and the names of the strings are thus a
|
|
||||||
* part of the program, but the strings themselves are still data files.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
#[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)*
|
|
||||||
})+
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
#[proc_macro_derive(Messages)]
|
|
||||||
pub fn messages_macro(input: TokenStream) -> TokenStream {
|
|
||||||
let syn::DeriveInput { ident, .. } = syn::parse_macro_input! {input};
|
|
||||||
|
|
||||||
let gen = quote! {
|
|
||||||
impl Messages for #ident {
|
|
||||||
fn hello() {
|
|
||||||
println!("Hello from {}", stringify!(#ident));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
gen.into()
|
|
||||||
}
|
|
|
@ -1,14 +0,0 @@
|
||||||
welcome:
|
|
||||||
content: "Welcome to Kifu"
|
|
||||||
hello:
|
|
||||||
parameters:
|
|
||||||
name: string
|
|
||||||
content: "Hello, ${name}"
|
|
||||||
games-in-database:
|
|
||||||
parameters:
|
|
||||||
count: count
|
|
||||||
content: |
|
|
||||||
{$count ->
|
|
||||||
[one] There is one game in the database
|
|
||||||
*[other] There are ${count} games in the database
|
|
||||||
}
|
|
Loading…
Reference in New Issue