1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
use std::fmt::{self, Formatter};
use crate::{base_component::BaseComponent, component::Component};
#[derive(Clone, Debug)]
pub enum StringOrComponent {
String(String),
Component(Component),
}
#[derive(Clone, Debug)]
pub struct TranslatableComponent {
pub base: BaseComponent,
pub key: String,
pub args: Vec<StringOrComponent>,
}
impl TranslatableComponent {
pub fn new(key: String, args: Vec<StringOrComponent>) -> Self {
Self {
base: BaseComponent::new(),
key,
args,
}
}
pub fn read(&self) -> Result<String, fmt::Error> {
let template = azalea_language::get(&self.key).unwrap_or(&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()));
}
}
|