diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-03-29 22:21:26 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-05-19 12:13:36 +0000 |
| commit | 1b5c1ce77f7986ba59b05aba81479b41c2e260b0 (patch) | |
| tree | c4a7472fd534bf919ddc979d228369f42e60ed79 /android | |
| parent | ee319af925b16662bcb77240e5b705c8fcc77231 (diff) | |
| download | mullvadvpn-1b5c1ce77f7986ba59b05aba81479b41c2e260b0.tar.xz mullvadvpn-1b5c1ce77f7986ba59b05aba81479b41c2e260b0.zip | |
Move Android plural resources code into new module
Diffstat (limited to 'android')
| -rw-r--r-- | android/translations-converter/src/android/mod.rs | 153 | ||||
| -rw-r--r-- | android/translations-converter/src/android/plurals.rs | 148 |
2 files changed, 152 insertions, 149 deletions
diff --git a/android/translations-converter/src/android/mod.rs b/android/translations-converter/src/android/mod.rs index 0ddcad8508..b43277c509 100644 --- a/android/translations-converter/src/android/mod.rs +++ b/android/translations-converter/src/android/mod.rs @@ -1,153 +1,8 @@ +mod plurals; mod string_value; mod strings; -use self::string_value::StringValue; -use serde::{Deserialize, Serialize}; -use std::{ - fmt::{self, Display, Formatter}, - ops::{Deref, DerefMut}, +pub use self::{ + plurals::{PluralQuantity, PluralResource, PluralResources}, + strings::{StringResource, StringResources}, }; - -pub use self::strings::{StringResource, StringResources}; - -/// Contents of an Android plurals resources file. -/// -/// This type can be created directly deserializing the `plurals.xml` file. -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct PluralResources { - #[serde(rename = "plurals")] - entries: Vec<PluralResource>, -} - -/// An entry in an Android plurals resources file. -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct PluralResource { - /// The plural resource ID. - pub name: String, - - /// The items of the plural resource, one for each quantity variant. - #[serde(rename = "item")] - pub items: Vec<PluralVariant>, -} - -/// A string resource for a specific quantity. -/// -/// This is part of a plural resource. -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct PluralVariant { - /// The quantity for this variant to be used. - pub quantity: PluralQuantity, - - /// The string value - #[serde(rename = "$value")] - pub string: StringValue, -} - -/// A valid quantity for a plural variant. -#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] -#[serde(rename_all = "snake_case")] -pub enum PluralQuantity { - Zero, - One, - Few, - Many, - Other, -} - -impl Deref for PluralResources { - type Target = Vec<PluralResource>; - - fn deref(&self) -> &Self::Target { - &self.entries - } -} - -impl DerefMut for PluralResources { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.entries - } -} - -impl IntoIterator for PluralResources { - type Item = PluralResource; - type IntoIter = std::vec::IntoIter<Self::Item>; - - fn into_iter(self) -> Self::IntoIter { - self.entries.into_iter() - } -} - -impl PluralResources { - /// Create an empty list of plural resources. - pub fn new() -> Self { - PluralResources { - entries: Vec::new(), - } - } -} - -impl PluralResource { - /// Create a plural resource representation. - /// - /// The resource has a name, used as the identifier, and a list of items. Each item contains - /// the message and the quantity it should be used for. - pub fn new(name: String, values: impl Iterator<Item = (PluralQuantity, String)>) -> Self { - let items = values - .map(|(quantity, string)| PluralVariant { - quantity, - string: StringValue::from(&*string), - }) - .collect(); - - PluralResource { name, items } - } -} - -impl Display for PluralResources { - fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { - writeln!(formatter, r#"<?xml version="1.0" encoding="utf-8"?>"#)?; - writeln!(formatter, "<resources>")?; - - for entry in &self.entries { - write!(formatter, "{}", entry)?; - } - - writeln!(formatter, "</resources>") - } -} - -impl Display for PluralResource { - fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { - writeln!(formatter, r#" <plurals name="{}">"#, self.name)?; - - for item in &self.items { - writeln!(formatter, " {}", item)?; - } - - writeln!(formatter, " </plurals>") - } -} - -impl Display for PluralVariant { - fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { - write!( - formatter, - r#"<item quantity="{}">{}</item>"#, - self.quantity, self.string - ) - } -} - -impl Display for PluralQuantity { - fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { - let quantity = match self { - PluralQuantity::Zero => "zero", - PluralQuantity::One => "one", - PluralQuantity::Few => "few", - PluralQuantity::Many => "many", - PluralQuantity::Other => "other", - }; - - write!(formatter, "{}", quantity) - } -} diff --git a/android/translations-converter/src/android/plurals.rs b/android/translations-converter/src/android/plurals.rs new file mode 100644 index 0000000000..285add6e5b --- /dev/null +++ b/android/translations-converter/src/android/plurals.rs @@ -0,0 +1,148 @@ +use super::string_value::StringValue; +use serde::{Deserialize, Serialize}; +use std::{ + fmt::{self, Display, Formatter}, + ops::{Deref, DerefMut}, +}; + +/// Contents of an Android plurals resources file. +/// +/// This type can be created directly deserializing the `plurals.xml` file. +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct PluralResources { + #[serde(rename = "plurals")] + entries: Vec<PluralResource>, +} + +/// An entry in an Android plurals resources file. +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct PluralResource { + /// The plural resource ID. + pub name: String, + + /// The items of the plural resource, one for each quantity variant. + #[serde(rename = "item")] + pub items: Vec<PluralVariant>, +} + +/// A string resource for a specific quantity. +/// +/// This is part of a plural resource. +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct PluralVariant { + /// The quantity for this variant to be used. + pub quantity: PluralQuantity, + + /// The string value + #[serde(rename = "$value")] + pub string: StringValue, +} + +/// A valid quantity for a plural variant. +#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum PluralQuantity { + Zero, + One, + Few, + Many, + Other, +} + +impl Deref for PluralResources { + type Target = Vec<PluralResource>; + + fn deref(&self) -> &Self::Target { + &self.entries + } +} + +impl DerefMut for PluralResources { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.entries + } +} + +impl IntoIterator for PluralResources { + type Item = PluralResource; + type IntoIter = std::vec::IntoIter<Self::Item>; + + fn into_iter(self) -> Self::IntoIter { + self.entries.into_iter() + } +} + +impl PluralResources { + /// Create an empty list of plural resources. + pub fn new() -> Self { + PluralResources { + entries: Vec::new(), + } + } +} + +impl PluralResource { + /// Create a plural resource representation. + /// + /// The resource has a name, used as the identifier, and a list of items. Each item contains + /// the message and the quantity it should be used for. + pub fn new(name: String, values: impl Iterator<Item = (PluralQuantity, String)>) -> Self { + let items = values + .map(|(quantity, string)| PluralVariant { + quantity, + string: StringValue::from(&*string), + }) + .collect(); + + PluralResource { name, items } + } +} + +impl Display for PluralResources { + fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { + writeln!(formatter, r#"<?xml version="1.0" encoding="utf-8"?>"#)?; + writeln!(formatter, "<resources>")?; + + for entry in &self.entries { + write!(formatter, "{}", entry)?; + } + + writeln!(formatter, "</resources>") + } +} + +impl Display for PluralResource { + fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { + writeln!(formatter, r#" <plurals name="{}">"#, self.name)?; + + for item in &self.items { + writeln!(formatter, " {}", item)?; + } + + writeln!(formatter, " </plurals>") + } +} + +impl Display for PluralVariant { + fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { + write!( + formatter, + r#"<item quantity="{}">{}</item>"#, + self.quantity, self.string + ) + } +} + +impl Display for PluralQuantity { + fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { + let quantity = match self { + PluralQuantity::Zero => "zero", + PluralQuantity::One => "one", + PluralQuantity::Few => "few", + PluralQuantity::Many => "many", + PluralQuantity::Other => "other", + }; + + write!(formatter, "{}", quantity) + } +} |
