diff options
| author | mat <github@matdoes.dev> | 2022-05-08 18:38:22 -0500 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-05-08 18:38:22 -0500 |
| commit | 7c742347a7e16ac28fcc88e3bab30e68695763e4 (patch) | |
| tree | ac33cff59db9eb9bf3c2f33125e53eb0df0e073c /azalea-chat/src | |
| parent | a9a8834b3aa78b1b85b16e738be8bd9105ef23c0 (diff) | |
| download | azalea-drasl-7c742347a7e16ac28fcc88e3bab30e68695763e4.tar.xz | |
azalea-chat now checks language files
Diffstat (limited to 'azalea-chat/src')
| -rwxr-xr-x | azalea-chat/src/component.rs | 16 | ||||
| -rwxr-xr-x | azalea-chat/src/translatable_component.rs | 155 |
2 files changed, 166 insertions, 5 deletions
diff --git a/azalea-chat/src/component.rs b/azalea-chat/src/component.rs index 9f065c47..36709cc0 100755 --- a/azalea-chat/src/component.rs +++ b/azalea-chat/src/component.rs @@ -63,14 +63,15 @@ impl Component { for component in self.clone().into_iter() { let component_text = match &component { - Self::Text(c) => &c.text, - Self::Translatable(c) => &c.key, + Self::Text(c) => c.text.to_string(), + Self::Translatable(c) => c.to_string(), }; + let component_style = &component.get_base().style; let ansi_text = running_style.compare_ansi(component_style, default_style); built_string.push_str(&ansi_text); - built_string.push_str(component_text); + built_string.push_str(&component_text); running_style.apply(component_style); } @@ -123,7 +124,12 @@ impl<'de> Deserialize<'de> for Component { let text = json.get("text").unwrap().as_str().unwrap_or("").to_string(); component = Component::Text(TextComponent::new(text)); } else if json.get("translate").is_some() { - let translate = json.get("translate").unwrap().to_string(); + let translate = json + .get("translate") + .unwrap() + .as_str() + .ok_or_else(|| de::Error::custom("\"translate\" must be a string"))? + .into(); if json.get("with").is_some() { let with = json.get("with").unwrap().as_array().unwrap(); let mut with_array = Vec::with_capacity(with.len()); @@ -136,7 +142,7 @@ impl<'de> Deserialize<'de> for Component { && text_component.base.style.is_empty() { with_array.push(StringOrComponent::String(text_component.text)); - break; + continue; } } with_array.push(StringOrComponent::Component( diff --git a/azalea-chat/src/translatable_component.rs b/azalea-chat/src/translatable_component.rs index 0709f7bf..fdef6465 100755 --- a/azalea-chat/src/translatable_component.rs +++ b/azalea-chat/src/translatable_component.rs @@ -1,3 +1,5 @@ +use std::fmt::{self, Formatter}; + use crate::{base_component::BaseComponent, component::Component}; #[derive(Clone, Debug)] @@ -21,4 +23,157 @@ impl TranslatableComponent { args, } } + + pub fn read(&self) -> Result<String, fmt::Error> { + let template = azalea_language::get(&self.key).unwrap_or_else(|| &self.key); + // decode the % things + + let mut result = String::new(); + let mut i = 0; + let mut matched = 0; + + // this code is ugly but it works + + while i < template.len() { + if template.chars().nth(i).unwrap() == '%' { + let char_after = match template.chars().nth(i + 1) { + Some(c) => c, + None => { + result.push(template.chars().nth(i).unwrap()); + break; + } + }; + i += 1; + match char_after { + '%' => { + result.push('%'); + } + 's' => { + result.push_str( + &self + .args + .get(matched) + .unwrap_or(&StringOrComponent::String("".to_string())) + .to_string(), + ); + matched += 1; + } + _ => { + // check if the char is a number + if let Some(d) = char_after.to_digit(10) { + // make sure the next two chars are $s + if let Some('$') = template.chars().nth(i + 1) { + if let Some('s') = template.chars().nth(i + 2) { + i += 2; + result.push_str( + &self + .args + .get((d - 1) as usize) + .unwrap_or(&StringOrComponent::String("".to_string())) + .to_string(), + ); + } else { + return Err(fmt::Error); + } + } else { + return Err(fmt::Error); + } + } else { + i -= 1; + result.push('%'); + } + } + } + } else { + result.push(template.chars().nth(i).unwrap()); + } + + i += 1 + } + + Ok(result.to_string()) + } +} + +impl fmt::Display for TranslatableComponent { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> { + write!(f, "{}", self.read()?) + } +} + +impl fmt::Display for StringOrComponent { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> { + match self { + StringOrComponent::String(s) => write!(f, "{}", s), + StringOrComponent::Component(c) => write!(f, "{}", c.to_ansi(None)), + } + } +} + +// tests +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_none() { + let c = TranslatableComponent::new("translation.test.none".to_string(), vec![]); + assert_eq!(c.read(), Ok("Hello, world!".to_string())); + } + #[test] + fn test_complex() { + let c = TranslatableComponent::new( + "translation.test.complex".to_string(), + vec![ + StringOrComponent::String("a".to_string()), + StringOrComponent::String("b".to_string()), + StringOrComponent::String("c".to_string()), + StringOrComponent::String("d".to_string()), + ], + ); + // so true mojang + assert_eq!( + c.read(), + Ok("Prefix, ab again b and a lastly c and also a again!".to_string()) + ); + } + #[test] + fn test_escape() { + let c = TranslatableComponent::new( + "translation.test.escape".to_string(), + vec![ + StringOrComponent::String("a".to_string()), + StringOrComponent::String("b".to_string()), + StringOrComponent::String("c".to_string()), + StringOrComponent::String("d".to_string()), + ], + ); + assert_eq!(c.read(), Ok("%s %a %%s %%b".to_string())); + } + #[test] + fn test_invalid() { + let c = TranslatableComponent::new( + "translation.test.invalid".to_string(), + vec![ + StringOrComponent::String("a".to_string()), + StringOrComponent::String("b".to_string()), + StringOrComponent::String("c".to_string()), + StringOrComponent::String("d".to_string()), + ], + ); + assert_eq!(c.read(), Ok("hi %".to_string())); + } + #[test] + fn test_invalid2() { + let c = TranslatableComponent::new( + "translation.test.invalid2".to_string(), + vec![ + StringOrComponent::String("a".to_string()), + StringOrComponent::String("b".to_string()), + StringOrComponent::String("c".to_string()), + StringOrComponent::String("d".to_string()), + ], + ); + assert_eq!(c.read(), Ok("hi % s".to_string())); + } } |
