Simplify snapshot code using match_file_name utility

This commit is contained in:
Lucien Greathouse
2019-10-09 18:27:52 -07:00
parent 6b620ddcef
commit e46f9fd94f
6 changed files with 27 additions and 104 deletions

View File

@@ -10,9 +10,9 @@ use crate::{
}; };
use super::{ use super::{
error::SnapshotError,
meta_file::AdjacentMetadata, meta_file::AdjacentMetadata,
middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware}, middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware},
util::match_file_name,
}; };
pub struct SnapshotCsv; pub struct SnapshotCsv;
@@ -26,25 +26,11 @@ impl SnapshotMiddleware for SnapshotCsv {
return Ok(None); return Ok(None);
} }
let extension = match entry.path().extension() { let instance_name = match match_file_name(entry.path(), ".csv") {
Some(x) => x Some(name) => name,
.to_str()
.ok_or_else(|| SnapshotError::file_name_bad_unicode(entry.path()))?,
None => return Ok(None), None => return Ok(None),
}; };
if extension != "csv" {
return Ok(None);
}
let instance_name = entry
.path()
.file_stem()
.expect("Could not extract file stem")
.to_str()
.ok_or_else(|| SnapshotError::file_name_bad_unicode(entry.path()))?
.to_string();
let meta_path = entry let meta_path = entry
.path() .path()
.with_file_name(format!("{}.meta.json", instance_name)); .with_file_name(format!("{}.meta.json", instance_name));
@@ -58,7 +44,7 @@ impl SnapshotMiddleware for SnapshotCsv {
relevant_paths: vec![entry.path().to_path_buf(), meta_path.clone()], relevant_paths: vec![entry.path().to_path_buf(), meta_path.clone()],
..Default::default() ..Default::default()
}, },
name: Cow::Owned(instance_name), name: Cow::Owned(instance_name.to_owned()),
class_name: Cow::Borrowed("LocalizationTable"), class_name: Cow::Borrowed("LocalizationTable"),
properties: hashmap! { properties: hashmap! {
"Contents".to_owned() => RbxValue::String { "Contents".to_owned() => RbxValue::String {

View File

@@ -12,6 +12,7 @@ use super::{
dir::SnapshotDir, dir::SnapshotDir,
meta_file::AdjacentMetadata, meta_file::AdjacentMetadata,
middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware}, middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware},
util::match_trailing,
}; };
pub struct SnapshotLua; pub struct SnapshotLua;
@@ -146,15 +147,6 @@ fn snapshot_init<F: ImfsFetcher>(
Ok(None) Ok(None)
} }
fn match_trailing<'a>(input: &'a str, trailer: &str) -> Option<&'a str> {
if input.ends_with(trailer) {
let end = input.len().saturating_sub(trailer.len());
Some(&input[..end])
} else {
None
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View File

@@ -8,8 +8,8 @@ use crate::{
}; };
use super::{ use super::{
error::SnapshotError,
middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware}, middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware},
util::match_file_name,
}; };
pub struct SnapshotRbxlx; pub struct SnapshotRbxlx;
@@ -23,24 +23,10 @@ impl SnapshotMiddleware for SnapshotRbxlx {
return Ok(None); return Ok(None);
} }
let file_name = entry let instance_name = match match_file_name(entry.path(), ".rbxlx") {
.path() Some(name) => name,
.file_name() None => return Ok(None),
.unwrap() };
.to_str()
.ok_or_else(|| SnapshotError::file_name_bad_unicode(entry.path()))?;
if !file_name.ends_with(".rbxlx") {
return Ok(None);
}
let instance_name = entry
.path()
.file_stem()
.expect("Could not extract file stem")
.to_str()
.ok_or_else(|| SnapshotError::file_name_bad_unicode(entry.path()))?
.to_string();
let options = rbx_xml::DecodeOptions::new() let options = rbx_xml::DecodeOptions::new()
.property_behavior(rbx_xml::DecodePropertyBehavior::ReadUnknown); .property_behavior(rbx_xml::DecodePropertyBehavior::ReadUnknown);
@@ -51,7 +37,7 @@ impl SnapshotMiddleware for SnapshotRbxlx {
let root_id = temp_tree.get_root_id(); let root_id = temp_tree.get_root_id();
let mut snapshot = InstanceSnapshot::from_tree(&temp_tree, root_id); let mut snapshot = InstanceSnapshot::from_tree(&temp_tree, root_id);
snapshot.name = Cow::Owned(instance_name); snapshot.name = Cow::Owned(instance_name.to_owned());
snapshot.metadata.instigating_source = Some(entry.path().to_path_buf().into()); snapshot.metadata.instigating_source = Some(entry.path().to_path_buf().into());
snapshot.metadata.relevant_paths = vec![entry.path().to_path_buf()]; snapshot.metadata.relevant_paths = vec![entry.path().to_path_buf()];

View File

@@ -8,8 +8,8 @@ use crate::{
}; };
use super::{ use super::{
error::SnapshotError,
middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware}, middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware},
util::match_file_name,
}; };
pub struct SnapshotRbxm; pub struct SnapshotRbxm;
@@ -23,24 +23,10 @@ impl SnapshotMiddleware for SnapshotRbxm {
return Ok(None); return Ok(None);
} }
let file_name = entry let instance_name = match match_file_name(entry.path(), ".rbxm") {
.path() Some(name) => name,
.file_name() None => return Ok(None),
.unwrap() };
.to_str()
.ok_or_else(|| SnapshotError::file_name_bad_unicode(entry.path()))?;
if !file_name.ends_with(".rbxm") {
return Ok(None);
}
let instance_name = entry
.path()
.file_stem()
.expect("Could not extract file stem")
.to_str()
.ok_or_else(|| SnapshotError::file_name_bad_unicode(entry.path()))?
.to_string();
let mut temp_tree = RbxTree::new(RbxInstanceProperties { let mut temp_tree = RbxTree::new(RbxInstanceProperties {
name: "DataModel".to_owned(), name: "DataModel".to_owned(),
@@ -57,7 +43,7 @@ impl SnapshotMiddleware for SnapshotRbxm {
if children.len() == 1 { if children.len() == 1 {
let mut snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0]); let mut snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0]);
snapshot.name = Cow::Owned(instance_name); snapshot.name = Cow::Owned(instance_name.to_owned());
snapshot.metadata.instigating_source = Some(entry.path().to_path_buf().into()); snapshot.metadata.instigating_source = Some(entry.path().to_path_buf().into());
snapshot.metadata.relevant_paths = vec![entry.path().to_path_buf()]; snapshot.metadata.relevant_paths = vec![entry.path().to_path_buf()];

View File

@@ -8,8 +8,8 @@ use crate::{
}; };
use super::{ use super::{
error::SnapshotError,
middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware}, middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware},
util::match_file_name,
}; };
pub struct SnapshotRbxmx; pub struct SnapshotRbxmx;
@@ -23,24 +23,10 @@ impl SnapshotMiddleware for SnapshotRbxmx {
return Ok(None); return Ok(None);
} }
let file_name = entry let instance_name = match match_file_name(entry.path(), ".rbxmx") {
.path() Some(name) => name,
.file_name() None => return Ok(None),
.unwrap() };
.to_str()
.ok_or_else(|| SnapshotError::file_name_bad_unicode(entry.path()))?;
if !file_name.ends_with(".rbxmx") {
return Ok(None);
}
let instance_name = entry
.path()
.file_stem()
.expect("Could not extract file stem")
.to_str()
.ok_or_else(|| SnapshotError::file_name_bad_unicode(entry.path()))?
.to_string();
let options = rbx_xml::DecodeOptions::new() let options = rbx_xml::DecodeOptions::new()
.property_behavior(rbx_xml::DecodePropertyBehavior::ReadUnknown); .property_behavior(rbx_xml::DecodePropertyBehavior::ReadUnknown);
@@ -53,7 +39,7 @@ impl SnapshotMiddleware for SnapshotRbxmx {
if children.len() == 1 { if children.len() == 1 {
let mut snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0]); let mut snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0]);
snapshot.name = Cow::Owned(instance_name); snapshot.name = Cow::Owned(instance_name.to_owned());
snapshot.metadata.instigating_source = Some(entry.path().to_path_buf().into()); snapshot.metadata.instigating_source = Some(entry.path().to_path_buf().into());
snapshot.metadata.relevant_paths = vec![entry.path().to_path_buf()]; snapshot.metadata.relevant_paths = vec![entry.path().to_path_buf()];

View File

@@ -12,6 +12,7 @@ use super::{
error::SnapshotError, error::SnapshotError,
meta_file::AdjacentMetadata, meta_file::AdjacentMetadata,
middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware}, middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware},
util::match_file_name,
}; };
pub struct SnapshotTxt; pub struct SnapshotTxt;
@@ -25,25 +26,11 @@ impl SnapshotMiddleware for SnapshotTxt {
return Ok(None); return Ok(None);
} }
let extension = match entry.path().extension() { let instance_name = match match_file_name(entry.path(), ".txt") {
Some(x) => x Some(name) => name,
.to_str()
.ok_or_else(|| SnapshotError::file_name_bad_unicode(entry.path()))?,
None => return Ok(None), None => return Ok(None),
}; };
if extension != "txt" {
return Ok(None);
}
let instance_name = entry
.path()
.file_stem()
.expect("Could not extract file stem")
.to_str()
.ok_or_else(|| SnapshotError::file_name_bad_unicode(entry.path()))?
.to_string();
let contents = entry.contents(imfs)?; let contents = entry.contents(imfs)?;
let contents_str = str::from_utf8(contents) let contents_str = str::from_utf8(contents)
.map_err(|err| SnapshotError::file_contents_bad_unicode(err, entry.path()))? .map_err(|err| SnapshotError::file_contents_bad_unicode(err, entry.path()))?
@@ -66,7 +53,7 @@ impl SnapshotMiddleware for SnapshotTxt {
relevant_paths: vec![entry.path().to_path_buf(), meta_path.clone()], relevant_paths: vec![entry.path().to_path_buf(), meta_path.clone()],
..Default::default() ..Default::default()
}, },
name: Cow::Owned(instance_name), name: Cow::Owned(instance_name.to_owned()),
class_name: Cow::Borrowed("StringValue"), class_name: Cow::Borrowed("StringValue"),
properties, properties,
children: Vec::new(), children: Vec::new(),