Skip to content

Commit

Permalink
fix(config): incomplete wasm support (#146)
Browse files Browse the repository at this point in the history
The implementation of the custom filesystem at [#142] was incomplete.
  • Loading branch information
pythonbrad authored Feb 1, 2024
1 parent 4867553 commit c64c8d5
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl Config {
match value {
Data::File(DataFile { path }) => {
let filepath = config_path.join(path);
let conf = Config::from_file(&filepath)?;
let conf = Config::from_filesystem(&filepath, fs)?;
data.extend(conf.data.unwrap_or_default());
}
Data::Simple(value) => {
Expand Down Expand Up @@ -149,7 +149,7 @@ impl Config {
match value {
Data::File(DataFile { path }) => {
let filepath = config_path.join(path);
let conf = Config::from_file(&filepath)?;
let conf = Config::from_filesystem(&filepath, fs)?;
translators.extend(conf.translators.unwrap_or_default());
}
Data::Simple(value) => {
Expand All @@ -172,7 +172,7 @@ impl Config {
match value {
Data::File(DataFile { path }) => {
let filepath = config_path.join(path);
let conf = Config::from_file(&filepath)?;
let conf = Config::from_filesystem(&filepath, fs)?;
translation.extend(conf.translation.unwrap_or_default());
}
Data::Simple(_) | Data::Multi(_) => {
Expand Down Expand Up @@ -363,4 +363,33 @@ mod tests {
let translation = conf.extract_translation();
assert_eq!(translation.keys().len(), 0);
}

#[test]
fn from_filesystem() {
use crate::FileSystem;
use std::{error::Error, fs};

#[derive(Default)]
struct FilterFileSystem;

impl FileSystem for FilterFileSystem {
fn read_to_string(&self, filepath: &Path) -> Result<String, Box<dyn Error>> {
let file_stem = filepath.file_stem().unwrap();

Ok(if file_stem == "data_sample" {
fs::read_to_string(filepath)?
} else {
String::new()
})
}
}

let fs = FilterFileSystem {};
let filepath = Path::new("./data/data_sample.toml");
let conf = Config::from_filesystem(&filepath, &fs).unwrap();

assert_eq!(conf.extract_data().keys().len(), 13);
assert_eq!(conf.extract_translators().unwrap().keys().len(), 0);
assert_eq!(conf.extract_translation().keys().len(), 0);
}
}

0 comments on commit c64c8d5

Please sign in to comment.