Skip to content

Commit 802e712

Browse files
committed
UPD: Refactoring
1 parent 9914b96 commit 802e712

File tree

1 file changed

+63
-49
lines changed

1 file changed

+63
-49
lines changed

wdx/crx_wdx/src/lib.rs

Lines changed: 63 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,28 @@ unsafe fn copy_rust_str_to_c_arr(s: &str, arr: *mut c_char, known_len: usize)
4141

4242
fn parse_locale(mut zip: ZipArchive<File>) -> io::Result<()>
4343
{
44+
let value: Option<&serde_json::Value>;
4445
unsafe {
45-
match JSON.get("default_locale")
46-
{
47-
Some(val) =>
46+
value = JSON.get("default_locale");
47+
}
48+
match value
49+
{
50+
Some(val) =>
51+
{
52+
match val.as_str()
4853
{
49-
match val.as_str()
50-
{
51-
Some(s) =>
52-
{
53-
let st = "_locales/".to_string().add(s).add("/messages.json");
54-
let file = zip.by_name(&st)?;
55-
LOCALES = serde_json::from_reader(file)?;
56-
},
57-
None => {}
58-
}
59-
},
60-
None => {}
61-
}
54+
Some(s) =>
55+
{
56+
let st = "_locales/".to_string().add(s).add("/messages.json");
57+
let file = zip.by_name(&st)?;
58+
unsafe {
59+
LOCALES = serde_json::from_reader(file)?;
60+
}
61+
},
62+
None => {}
63+
}
64+
},
65+
None => {}
6266
}
6367
Ok(())
6468
}
@@ -121,33 +125,43 @@ fn parse(file_name: &String) -> io::Result<()>
121125

122126
fn get_locale_str(str: String) -> String
123127
{
124-
unsafe {
125-
if str.starts_with("__MSG_") & str.ends_with("__") & (LOCALES != serde_json::Value::Null)
126-
{
127-
let mut ss: String = str.chars().skip(6).take(str.len() - 8).collect();
128-
129-
if ss.len() == 0
128+
if str.starts_with("__MSG_") & str.ends_with("__")
129+
{
130+
unsafe {
131+
if LOCALES == serde_json::Value::Null
130132
{
131133
return str;
132134
}
133-
ss.insert(0, '/');
134-
ss.push_str("/message");
135+
}
135136

136-
match LOCALES.pointer(&ss)
137-
{
138-
Some(val) =>
137+
let mut ss: String = str.chars().skip(6).take(str.len() - 8).collect();
138+
139+
if ss.len() == 0
140+
{
141+
return str;
142+
}
143+
ss.insert(0, '/');
144+
ss.push_str("/message");
145+
146+
let value: Option<&serde_json::Value>;
147+
unsafe {
148+
value = LOCALES.pointer(&ss);
149+
}
150+
151+
match value
152+
{
153+
Some(val) =>
154+
{
155+
match val.as_str()
139156
{
140-
match val.as_str()
141-
{
142-
Some(s) => s.to_string(),
143-
None => str
144-
}
145-
},
146-
None => str
147-
}
148-
} else {
149-
str
157+
Some(s) => s.to_string(),
158+
None => str
159+
}
160+
},
161+
None => str
150162
}
163+
} else {
164+
str
151165
}
152166
}
153167

@@ -169,9 +183,8 @@ pub unsafe extern "system" fn ContentGetSupportedField(field_index: i32, field_n
169183
#[no_mangle]
170184
pub unsafe extern "system" fn ContentGetValue(file_name: *const c_char, field_index: i32, _unit_index: i32, field_value: *mut c_char, maxlen: i32, _flags: i32) -> i32
171185
{
172-
173186
let index = field_index as usize;
174-
187+
175188
if (field_index < 0) || (index >= FIELD_ARRAY.len())
176189
{
177190
return FT_NOSUCHFIELD;
@@ -188,15 +201,17 @@ pub unsafe extern "system" fn ContentGetValue(file_name: *const c_char, field_in
188201
return FT_FILEERROR;
189202
}
190203
}
191-
204+
let result: Option<&serde_json::Value>;
192205
unsafe {
193-
match JSON.get(FIELD_ARRAY[index].1)
194-
{
195-
Some(val) => value =
206+
result = JSON.get(FIELD_ARRAY[index].1)
207+
}
208+
match result
209+
{
210+
Some(val) => value =
196211
{
197212
if val.is_array()
198213
{
199-
val.to_string()
214+
val.to_string()
200215
}
201216
else if val.is_u64()
202217
{
@@ -219,12 +234,11 @@ pub unsafe extern "system" fn ContentGetValue(file_name: *const c_char, field_in
219234
return FT_FIELDEMPTY;
220235
}
221236
},
222-
None => return FT_FIELDEMPTY
223-
}
224-
225-
copy_rust_str_to_c_arr(&value, field_value, maxlen as usize);
237+
None => return FT_FIELDEMPTY
226238
}
227-
239+
240+
copy_rust_str_to_c_arr(&value, field_value, maxlen as usize);
241+
228242
FIELD_ARRAY[index].2
229243
}
230244

0 commit comments

Comments
 (0)