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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#![allow(clippy::needless_return)]
use std::path::{Path, PathBuf};
pub static ZIPPED_RULE_FILES: &[u8] = include_bytes!(concat!(env!("OUT_DIR"),"/rules.zip"));
cfg_if! {
if #[cfg(target_family = "wasm")] {
use std::cell::RefCell;
fn file_system_type_from(path: &Path) -> Option<&str> {
use sxd_document::dom::*;
use std::path::Component;
use crate::interface::get_element;
use crate::canonicalize::name;
return DIRECTORY_TREE.with(|files| {
let files = files.borrow();
let files = get_element(&*files);
let mut children = vec![ChildOfElement::Element(files)];
let mut components = path.components();
let mut next_component = components.next();
let mut matched_dir = None;
while let Some(component) = next_component {
if let Component::Normal(os_str) = component {
let component_name = os_str.to_str().unwrap();
matched_dir = None;
for child in &children {
if let ChildOfElement::Element(child) = child {
if child.attribute_value("name").unwrap() == component_name {
if name(&child) == "dir" {
matched_dir = Some("dir");
children = child.children();
break;
} else { return if components.next().is_none() {Some("file")} else {None};
}
}
}
};
if matched_dir.is_none() {
return matched_dir;
}
} else {
error!("Expected Component::Normal, found {:?}", component);
return None;
};
next_component = components.next();
};
return matched_dir;
});
}
pub fn is_file_shim(path: &Path) -> bool {
let fs = file_system_type_from(path);
return match fs {
None => false,
Some(fs) => fs == "file",
};
}
pub fn is_dir_shim(path: &Path) -> bool {
let fs = file_system_type_from(path);
return match fs {
None => false,
Some(fs) => fs == "dir",
};
}
pub fn canonicalize_shim(path: &Path) -> std::io::Result<PathBuf> {
return Ok( path.to_path_buf() );
}
pub fn read_to_string_shim(path: &Path) -> Result<String, crate::errors::Error> {
use std::io::Cursor;
use std::io::Read;
let file_name = path.to_str().unwrap().replace("/", "\\");
if let Some(contents) = OVERRIDE_FILE_NAME.with(|override_name| {
if file_name.as_str() == override_name.borrow().as_str() {
debug!("override read_to_string_shim: {}",file_name);
return OVERRIDE_FILE_CONTENTS.with(|contents| return Some(contents.borrow().clone()));
} else {
return None;
}
}) {
return Ok(contents);
};
debug!("read_to_string_shim: {}",file_name);
let buf_reader = Cursor::new(ZIPPED_RULE_FILES);
let mut archive = zip::ZipArchive::new(buf_reader).unwrap();
let mut file = match archive.by_name(&file_name) {
Ok(file) => file,
Err(..) => {
panic!("Didn't find {} in zip archive", file_name);
}
};
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
return Ok(contents);
}
thread_local! {
static OVERRIDE_FILE_NAME: RefCell<String> = RefCell::new("".to_string());
static OVERRIDE_FILE_CONTENTS: RefCell<String> = RefCell::new("".to_string());
}
pub fn override_file_for_debugging_rules(file_name: &str, file_contents: &str) {
OVERRIDE_FILE_NAME.with(|name| *name.borrow_mut() = file_name.to_string().replace("/", "\\"));
OVERRIDE_FILE_CONTENTS.with(|contents| *contents.borrow_mut() = file_contents.to_string());
crate::speech::NAVIGATION_RULES.with(|nav_rules|
nav_rules.borrow_mut().invalidate(
crate::prefs::FilesChanged{
speech_rules: true, speech_unicode_short: false, speech_unicode_full: false,
braille_rules: true, braille_unicode_short: false, braille_unicode_full: false,
intent: false, defs: false }
));
}
use sxd_document::parser;
use sxd_document::Package;
thread_local! {
static DIRECTORY_TREE: RefCell<Package> = RefCell::new(
parser::parse(r"
<dir name='Rules'>
<file name='definitions.yaml'/>
<file name='intent.yaml'/>
<file name='prefs.yaml'/>
<dir name='Intent'>
<file name='general.yaml'/>
<file name='geometry.yaml'/>
<file name='linear-algebra.yaml'/>
</dir>
<dir name='Braille'>
<dir name='Nemeth'>
<file name='Nemeth_Rules.yaml'/>
<file name='unicode.yaml'/>
<file name='unicode-full.yaml'/>
</dir>
<dir name='UEB'>
<file name='UEB_Rules.yaml'/>
<file name='unicode.yaml'/>
<file name='unicode-full.yaml'/>
</dir>
</dir>
<dir name='Languages'>
<dir name='en'>
<dir name='SharedRules'>
<file name='default.yaml'/>
<file name='general.yaml'/>
<file name='geometry.yaml'/>
<file name='linear-algebra.yaml'/>
<file name='menclose.yaml'/>
</dir>
<file name='ClearSpeak_Rules.yaml'/>
<file name='definitions.yaml'/>
<file name='navigate.yaml'/>
<file name='overview.yaml'/>
<file name='SimpleSpeak_Rules.yaml'/>
<file name='unicode.yaml'/>
<file name='unicode-full.yaml'/>
</dir>
</dir>
</dir>")
.expect("Internal error in creating web assembly files: didn't parse initializer string")
);
}
} else {
use crate::errors::*;
pub fn is_file_shim(path: &Path) -> bool {
return path.is_file();
}
pub fn is_dir_shim(path: &Path) -> bool {
return path.is_dir();
}
pub fn canonicalize_shim(path: &Path) -> std::io::Result<PathBuf> {
return path.canonicalize();
}
pub fn read_to_string_shim(path: &Path) -> Result<String> {
debug!("Reading file '{}'", path.to_str().unwrap());
return std::fs::read_to_string(path).chain_err(|| format!("while trying to read {}", path.to_str().unwrap()));
}
}
}