mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-04-23 06:05:24 +00:00
Add utility for working with file names, port JSON model to use it
This commit is contained in:
@@ -10,8 +10,8 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
error::SnapshotError,
|
|
||||||
middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware},
|
middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware},
|
||||||
|
util::match_file_name,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct SnapshotJsonModel;
|
pub struct SnapshotJsonModel;
|
||||||
@@ -25,15 +25,8 @@ impl SnapshotMiddleware for SnapshotJsonModel {
|
|||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
let file_name = entry
|
let instance_name = match match_file_name(entry.path(), ".model.json") {
|
||||||
.path()
|
Some(name) => name,
|
||||||
.file_name()
|
|
||||||
.unwrap()
|
|
||||||
.to_str()
|
|
||||||
.ok_or_else(|| SnapshotError::file_name_bad_unicode(entry.path()))?;
|
|
||||||
|
|
||||||
let instance_name = match match_trailing(&file_name, ".model.json") {
|
|
||||||
Some(name) => name.to_owned(),
|
|
||||||
None => return Ok(None),
|
None => return Ok(None),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -58,7 +51,7 @@ impl SnapshotMiddleware for SnapshotJsonModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut snapshot = instance.core.into_snapshot(instance_name);
|
let mut snapshot = instance.core.into_snapshot(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()];
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ mod rbxlx;
|
|||||||
mod rbxm;
|
mod rbxm;
|
||||||
mod rbxmx;
|
mod rbxmx;
|
||||||
mod txt;
|
mod txt;
|
||||||
|
mod util;
|
||||||
|
|
||||||
pub use self::error::*;
|
pub use self::error::*;
|
||||||
|
|
||||||
|
|||||||
20
src/snapshot_middleware/util.rs
Normal file
20
src/snapshot_middleware/util.rs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
/// If the given string ends up with the given suffix, returns the portion of
|
||||||
|
/// the string before the suffix.
|
||||||
|
pub fn match_trailing<'a>(input: &'a str, suffix: &str) -> Option<&'a str> {
|
||||||
|
if input.ends_with(suffix) {
|
||||||
|
let end = input.len().saturating_sub(suffix.len());
|
||||||
|
Some(&input[..end])
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// If the given path has a file name, and that file name ends with the given
|
||||||
|
/// suffix, returns the portion of the file name before the given suffix.
|
||||||
|
pub fn match_file_name<'a>(path: &'a Path, suffix: &str) -> Option<&'a str> {
|
||||||
|
let file_name = path.file_name()?.to_str()?;
|
||||||
|
|
||||||
|
match_trailing(&file_name, suffix)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user