use crate::pp::SourceFiles;
pub const ROOT: &str = "<hexicon>";
pub const SYSTEM: &str = "<hexicon>/system";
pub(crate) static FILES: &[(&str, &[u8])] =
include!(concat!(env!("OUT_DIR"), "/built_in_files.rs"));
fn in_root(path: &str) -> Option<String> {
let path = path.replace('\\', "/");
path.strip_prefix(ROOT)?
.strip_prefix('/')
.map(str::to_owned)
}
pub fn file(path: &str) -> Option<&'static [u8]> {
let name = in_root(path)?;
FILES
.iter()
.find(|(have, _)| have.eq_ignore_ascii_case(&name))
.map(|(_, data)| *data)
}
pub(crate) fn system_header(name: &str) -> Option<&'static [u8]> {
file(&format!("{SYSTEM}/{name}"))
}
pub fn files_in(folder: &str) -> Vec<String> {
let Some(name) = in_root(folder) else {
return Vec::new();
};
let name = name.trim_end_matches('/');
let mut found: Vec<String> = FILES
.iter()
.filter_map(|(have, _)| {
let rest = have.get(name.len()..)?.strip_prefix('/')?;
(have.get(..name.len())?.eq_ignore_ascii_case(name) && !rest.contains('/'))
.then(|| format!("{ROOT}/{have}"))
})
.collect();
found.sort();
found
}
pub(crate) fn is_built_in(path: &str) -> bool {
path.replace('\\', "/").starts_with(ROOT)
}
fn is_folder(path: &str) -> bool {
match in_root(path) {
Some(name) => {
let name = name.trim_end_matches('/');
FILES.iter().any(|(have, _)| {
have.len() > name.len()
&& have[..name.len()].eq_ignore_ascii_case(name)
&& have.as_bytes()[name.len()] == b'/'
})
}
None => path.replace('\\', "/") == ROOT && !FILES.is_empty(),
}
}
pub(crate) struct WithBuiltIns<'f> {
files: &'f mut dyn SourceFiles,
english: Vec<String>,
translated: Option<std::collections::HashMap<String, Vec<u8>>>,
}
impl<'f> WithBuiltIns<'f> {
pub(crate) fn new(files: &'f mut dyn SourceFiles) -> Self {
Self::for_sources(files, &[])
}
pub(crate) fn for_sources(files: &'f mut dyn SourceFiles, sources: &[&str]) -> Self {
Self {
files,
english: sources
.iter()
.filter(|name| crate::english::is_english(name))
.map(|name| (*name).to_owned())
.collect(),
translated: None,
}
}
fn english(&mut self, name: &str) -> Option<Vec<u8>> {
if let Some(have) = self.translated.as_ref().and_then(|t| t.get(name)) {
return Some(have.clone());
}
let names: Vec<String> = if self.english.iter().any(|n| n == name) {
self.english.clone()
} else {
vec![name.to_owned()]
};
let mut texts = Vec::new();
for n in &names {
match self.files.read(n) {
Some(bytes) => texts.push(String::from_utf8_lossy(&bytes).into_owned()),
None if n == name => return None,
None => texts.push(String::new()),
}
}
let sources: Vec<(&str, &str)> = names
.iter()
.zip(&texts)
.map(|(n, text)| (n.as_str(), text.as_str()))
.collect();
let shown: Vec<&str> = names.iter().map(String::as_str).collect();
let translated = self.translated.get_or_insert_with(Default::default);
for (i, translation) in crate::english::translate_all(&sources)
.into_iter()
.enumerate()
{
translated.insert(names[i].clone(), translation.source(&shown, i).into_bytes());
}
translated.get(name).cloned()
}
}
impl SourceFiles for WithBuiltIns<'_> {
fn read(&mut self, name: &str) -> Option<Vec<u8>> {
if is_built_in(name) {
return file(name).map(<[u8]>::to_vec);
}
if crate::english::is_english(name) {
return self.english(name);
}
self.files.read(name)
}
fn exists(&mut self, name: &str) -> bool {
if is_built_in(name) {
return file(name).is_some() || is_folder(name);
}
self.files.exists(name)
}
fn decode(&mut self, charset: &str, bytes: &[u8]) -> Option<Vec<u8>> {
self.files.decode(charset, bytes)
}
}