mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-04-23 14:15:24 +00:00
Flatten snapshot middleware to be much simpler (#324)
* First take at flattening middleware for simpler code and better perf * Undo debug prints * Fix using wrong path in snapshot_from_vfs * Disable some broken tests * Re-enable (mistakenly?) disabled CSV test * Fix some tests * Update project file tests * Fix benchmark
This commit is contained in:
committed by
GitHub
parent
bdd1afea57
commit
486b067567
@@ -8,54 +8,41 @@ use serde::Serialize;
|
||||
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
|
||||
|
||||
use super::{
|
||||
error::SnapshotError,
|
||||
meta_file::AdjacentMetadata,
|
||||
middleware::{SnapshotInstanceResult, SnapshotMiddleware},
|
||||
util::match_file_name,
|
||||
error::SnapshotError, meta_file::AdjacentMetadata, middleware::SnapshotInstanceResult,
|
||||
};
|
||||
|
||||
pub struct SnapshotCsv;
|
||||
pub fn snapshot_csv(
|
||||
_context: &InstanceContext,
|
||||
vfs: &Vfs,
|
||||
path: &Path,
|
||||
instance_name: &str,
|
||||
) -> SnapshotInstanceResult {
|
||||
let meta_path = path.with_file_name(format!("{}.meta.json", instance_name));
|
||||
let contents = vfs.read(path)?;
|
||||
|
||||
impl SnapshotMiddleware for SnapshotCsv {
|
||||
fn from_vfs(_context: &InstanceContext, vfs: &Vfs, path: &Path) -> SnapshotInstanceResult {
|
||||
let meta = vfs.metadata(path)?;
|
||||
let table_contents = convert_localization_csv(&contents)
|
||||
.map_err(|source| SnapshotError::malformed_l10n_csv(source, path))?;
|
||||
|
||||
if meta.is_dir() {
|
||||
return Ok(None);
|
||||
}
|
||||
let mut snapshot = InstanceSnapshot::new()
|
||||
.name(instance_name)
|
||||
.class_name("LocalizationTable")
|
||||
.properties(hashmap! {
|
||||
"Contents".to_owned() => RbxValue::String {
|
||||
value: table_contents,
|
||||
},
|
||||
})
|
||||
.metadata(
|
||||
InstanceMetadata::new()
|
||||
.instigating_source(path)
|
||||
.relevant_paths(vec![path.to_path_buf(), meta_path.clone()]),
|
||||
);
|
||||
|
||||
let instance_name = match match_file_name(path, ".csv") {
|
||||
Some(name) => name,
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
let meta_path = path.with_file_name(format!("{}.meta.json", instance_name));
|
||||
let contents = vfs.read(path)?;
|
||||
|
||||
let table_contents = convert_localization_csv(&contents)
|
||||
.map_err(|source| SnapshotError::malformed_l10n_csv(source, path))?;
|
||||
|
||||
let mut snapshot = InstanceSnapshot::new()
|
||||
.name(instance_name)
|
||||
.class_name("LocalizationTable")
|
||||
.properties(hashmap! {
|
||||
"Contents".to_owned() => RbxValue::String {
|
||||
value: table_contents,
|
||||
},
|
||||
})
|
||||
.metadata(
|
||||
InstanceMetadata::new()
|
||||
.instigating_source(path)
|
||||
.relevant_paths(vec![path.to_path_buf(), meta_path.clone()]),
|
||||
);
|
||||
|
||||
if let Some(meta_contents) = vfs.read(&meta_path).with_not_found()? {
|
||||
let mut metadata = AdjacentMetadata::from_slice(&meta_contents, &meta_path)?;
|
||||
metadata.apply_all(&mut snapshot);
|
||||
}
|
||||
|
||||
Ok(Some(snapshot))
|
||||
if let Some(meta_contents) = vfs.read(&meta_path).with_not_found()? {
|
||||
let mut metadata = AdjacentMetadata::from_slice(&meta_contents, &meta_path)?;
|
||||
metadata.apply_all(&mut snapshot);
|
||||
}
|
||||
|
||||
Ok(Some(snapshot))
|
||||
}
|
||||
|
||||
/// Struct that holds any valid row from a Roblox CSV translation table.
|
||||
@@ -156,10 +143,14 @@ Ack,Ack!,,An exclamation of despair,¡Ay!"#,
|
||||
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let instance_snapshot =
|
||||
SnapshotCsv::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo.csv"))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let instance_snapshot = snapshot_csv(
|
||||
&InstanceContext::default(),
|
||||
&mut vfs,
|
||||
Path::new("/foo.csv"),
|
||||
"foo",
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
@@ -184,9 +175,15 @@ Ack,Ack!,,An exclamation of despair,¡Ay!"#,
|
||||
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let instance_snapshot =
|
||||
SnapshotCsv::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo.csv"))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let instance_snapshot = snapshot_csv(
|
||||
&InstanceContext::default(),
|
||||
&mut vfs,
|
||||
Path::new("/foo.csv"),
|
||||
"foo",
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user