summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2025-06-06 22:33:30 +0200
committerJoel Klinghed <the_jk@spawned.biz>2025-06-06 22:35:15 +0200
commitf25b8789962157f7fa2af55b139a75e2ee1a09af (patch)
tree3cac053fa23bec31045eabd3f0e9147a8faf6b0a
parentb0942ad84aaa25443b57812bcea50c2bde03e03b (diff)
grit: Allow if's to be recursive
Not often used in grit files, if at all, but needed when we want to add support for expanding grit-part in grit structure.
-rw-r--r--server/common/src/grit.rs54
-rw-r--r--server/common/src/tests.rs44
2 files changed, 60 insertions, 38 deletions
diff --git a/server/common/src/grit.rs b/server/common/src/grit.rs
index 273da8a..fab1130 100644
--- a/server/common/src/grit.rs
+++ b/server/common/src/grit.rs
@@ -34,7 +34,7 @@ pub struct Output {
pub enum IfOutput {
Output(Output),
- If { expr: String, output: Vec<Output> },
+ If { expr: String, output: Vec<IfOutput> },
}
#[derive(Debug, PartialEq)]
@@ -52,7 +52,7 @@ pub struct File {
pub enum IfFile {
File(File),
- If { expr: String, file: Vec<File> },
+ If { expr: String, file: Vec<IfFile> },
}
#[derive(Debug, PartialEq)]
@@ -79,18 +79,11 @@ pub enum IfMessagePart {
If {
expr: String,
- message: Vec<MessagePart>,
+ message: Vec<IfMessagePart>,
},
}
#[derive(Debug, PartialEq)]
-pub enum MessagePart {
- Message(Message),
-
- Part(PartRef),
-}
-
-#[derive(Debug, PartialEq)]
pub struct PartRef {
pub file: String,
}
@@ -119,7 +112,10 @@ pub enum TextPlaceholder {
pub enum IfMessage {
Message(Message),
- If { expr: String, message: Vec<Message> },
+ If {
+ expr: String,
+ message: Vec<IfMessage>,
+ },
}
#[derive(Debug, PartialEq)]
@@ -305,7 +301,7 @@ fn parse_if_output_element<R: Read>(
reader: &mut EventReader<R>,
) -> anyhow::Result<IfOutput> {
let expr = get_attribute(attributes, "expr")?;
- let mut output = Vec::<Output>::new();
+ let mut output = Vec::<IfOutput>::new();
loop {
let event = reader.next()?;
@@ -316,7 +312,10 @@ fn parse_if_output_element<R: Read>(
namespace: _,
} => match name.local_name.as_str() {
"output" => {
- output.push(parse_output_element(&attributes, reader)?);
+ output.push(IfOutput::Output(parse_output_element(&attributes, reader)?));
+ }
+ "if" => {
+ output.push(parse_if_output_element(&attributes, reader)?);
}
_ => {
return Err(Error::msg(format!(
@@ -449,7 +448,7 @@ fn parse_if_file_element<R: Read>(
reader: &mut EventReader<R>,
) -> anyhow::Result<IfFile> {
let expr = get_attribute(attributes, "expr")?;
- let mut file = Vec::<File>::new();
+ let mut file = Vec::<IfFile>::new();
loop {
let event = reader.next()?;
@@ -460,7 +459,10 @@ fn parse_if_file_element<R: Read>(
namespace: _,
} => match name.local_name.as_str() {
"file" => {
- file.push(parse_file_element(&attributes, reader)?);
+ file.push(IfFile::File(parse_file_element(&attributes, reader)?));
+ }
+ "if" => {
+ file.push(parse_if_file_element(&attributes, reader)?);
}
_ => {
return Err(Error::msg(format!(
@@ -695,7 +697,7 @@ fn parse_if_message_part_element<R: Read>(
) -> anyhow::Result<IfMessagePart> {
let expr = get_attribute(attributes, "expr")?;
- let mut message = Vec::<MessagePart>::new();
+ let mut message = Vec::<IfMessagePart>::new();
loop {
let event = reader.next()?;
@@ -706,13 +708,19 @@ fn parse_if_message_part_element<R: Read>(
namespace: _,
} => match name.local_name.as_str() {
"message" => {
- message.push(MessagePart::Message(parse_message_element(
+ message.push(IfMessagePart::Message(parse_message_element(
&attributes,
reader,
)?));
}
"part" => {
- message.push(MessagePart::Part(parse_part_element(&attributes, reader)?));
+ message.push(IfMessagePart::Part(parse_part_element(
+ &attributes,
+ reader,
+ )?));
+ }
+ "if" => {
+ message.push(parse_if_message_part_element(&attributes, reader)?);
}
_ => {
return Err(Error::msg(format!(
@@ -752,7 +760,7 @@ fn parse_if_message_element<R: Read>(
) -> anyhow::Result<IfMessage> {
let expr = get_attribute(attributes, "expr")?;
- let mut message = Vec::<Message>::new();
+ let mut message = Vec::<IfMessage>::new();
loop {
let event = reader.next()?;
@@ -763,7 +771,13 @@ fn parse_if_message_element<R: Read>(
namespace: _,
} => match name.local_name.as_str() {
"message" => {
- message.push(parse_message_element(&attributes, reader)?);
+ message.push(IfMessage::Message(parse_message_element(
+ &attributes,
+ reader,
+ )?));
+ }
+ "if" => {
+ message.push(parse_if_message_element(&attributes, reader)?);
}
_ => {
return Err(Error::msg(format!(
diff --git a/server/common/src/tests.rs b/server/common/src/tests.rs
index fc4d7aa..f336f67 100644
--- a/server/common/src/tests.rs
+++ b/server/common/src/tests.rs
@@ -322,11 +322,13 @@ async fn test_grit_parse_base() {
grit::IfOutput::If {
expr: "not pp_if('zawgyi_encoding')".to_string(),
output: vec![
- grit::Output {
- filename: "values-my-rZG/strings.xml".to_string(),
- output_type: "android".to_string(),
- lang: "my-ZG".to_string(),
- },
+ grit::IfOutput::Output(
+ grit::Output {
+ filename: "values-my-rZG/strings.xml".to_string(),
+ output_type: "android".to_string(),
+ lang: "my-ZG".to_string(),
+ },
+ ),
],
},
grit::IfOutput::Output(
@@ -349,23 +351,29 @@ async fn test_grit_parse_base() {
grit::IfFile::If {
expr: "pp_if('zawgyi_encoding')".to_string(),
file: vec![
- grit::File {
- path: "translations/base_my-Zawgyi.xlf".to_string(),
- lang: "my".to_string(),
- },
+ grit::IfFile::File(
+ grit::File {
+ path: "translations/base_my-Zawgyi.xlf".to_string(),
+ lang: "my".to_string(),
+ },
+ ),
],
},
grit::IfFile::If {
expr: "not pp_if('zawgyi_encoding')".to_string(),
file: vec![
- grit::File {
- path: "translations/base_my.xlf".to_string(),
- lang: "my".to_string(),
- },
- grit::File {
- path: "translations/base_my-Zawgyi.xlf".to_string(),
- lang: "my-ZG".to_string(),
- },
+ grit::IfFile::File(
+ grit::File {
+ path: "translations/base_my.xlf".to_string(),
+ lang: "my".to_string(),
+ },
+ ),
+ grit::IfFile::File(
+ grit::File {
+ path: "translations/base_my-Zawgyi.xlf".to_string(),
+ lang: "my-ZG".to_string(),
+ },
+ ),
],
},
grit::IfFile::File(
@@ -385,7 +393,7 @@ async fn test_grit_parse_base() {
grit::IfMessagePart::If {
expr: "pp_ifdef('include_extra')".to_string(),
message: vec![
- grit::MessagePart::Part(
+ grit::IfMessagePart::Part(
grit::PartRef {
file: "extra.grdp".to_string(),
},