From f25b8789962157f7fa2af55b139a75e2ee1a09af Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Fri, 6 Jun 2025 22:33:30 +0200 Subject: 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. --- server/common/src/grit.rs | 54 +++++++++++++++++++++++++++++----------------- server/common/src/tests.rs | 44 +++++++++++++++++++++---------------- 2 files changed, 60 insertions(+), 38 deletions(-) (limited to 'server/common/src') 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 }, + If { expr: String, output: Vec }, } #[derive(Debug, PartialEq)] @@ -52,7 +52,7 @@ pub struct File { pub enum IfFile { File(File), - If { expr: String, file: Vec }, + If { expr: String, file: Vec }, } #[derive(Debug, PartialEq)] @@ -79,17 +79,10 @@ pub enum IfMessagePart { If { expr: String, - message: Vec, + message: Vec, }, } -#[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 }, + If { + expr: String, + message: Vec, + }, } #[derive(Debug, PartialEq)] @@ -305,7 +301,7 @@ fn parse_if_output_element( reader: &mut EventReader, ) -> anyhow::Result { let expr = get_attribute(attributes, "expr")?; - let mut output = Vec::::new(); + let mut output = Vec::::new(); loop { let event = reader.next()?; @@ -316,7 +312,10 @@ fn parse_if_output_element( 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( reader: &mut EventReader, ) -> anyhow::Result { let expr = get_attribute(attributes, "expr")?; - let mut file = Vec::::new(); + let mut file = Vec::::new(); loop { let event = reader.next()?; @@ -460,7 +459,10 @@ fn parse_if_file_element( 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( ) -> anyhow::Result { let expr = get_attribute(attributes, "expr")?; - let mut message = Vec::::new(); + let mut message = Vec::::new(); loop { let event = reader.next()?; @@ -706,13 +708,19 @@ fn parse_if_message_part_element( 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( ) -> anyhow::Result { let expr = get_attribute(attributes, "expr")?; - let mut message = Vec::::new(); + let mut message = Vec::::new(); loop { let event = reader.next()?; @@ -763,7 +771,13 @@ fn parse_if_message_element( 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(), }, -- cgit v1.2.3-70-g09d2