Add InstanceMetadata builder (#269)

* Add InstanceMetadata builder, with context field for globbing

* Revert snapshot changes

* Port snapshot functions to InstanceMetadata builder-ish pattern

* Remove IgnoreGlob struct

* Elide lifetime
This commit is contained in:
Lucien Greathouse
2019-12-03 14:49:40 -08:00
committed by GitHub
parent da6c7b4d7a
commit a48c238ed7
8 changed files with 75 additions and 39 deletions

View File

@@ -1,4 +1,7 @@
use std::{fmt, path::PathBuf};
use std::{
fmt,
path::{Path, PathBuf},
};
use serde::{Deserialize, Serialize};
@@ -42,14 +45,41 @@ pub struct InstanceMetadata {
pub relevant_paths: Vec<PathBuf>,
}
impl Default for InstanceMetadata {
fn default() -> Self {
InstanceMetadata {
impl InstanceMetadata {
pub fn new() -> Self {
Self {
ignore_unknown_instances: false,
instigating_source: None,
relevant_paths: Vec::new(),
}
}
pub fn ignore_unknown_instances(self, ignore_unknown_instances: bool) -> Self {
Self {
ignore_unknown_instances,
..self
}
}
pub fn instigating_source(self, instigating_source: impl Into<InstigatingSource>) -> Self {
Self {
instigating_source: Some(instigating_source.into()),
..self
}
}
pub fn relevant_paths(self, relevant_paths: Vec<PathBuf>) -> Self {
Self {
relevant_paths,
..self
}
}
}
impl Default for InstanceMetadata {
fn default() -> Self {
Self::new()
}
}
#[derive(Clone, PartialEq, Serialize, Deserialize)]
@@ -74,3 +104,9 @@ impl From<PathBuf> for InstigatingSource {
InstigatingSource::Path(path)
}
}
impl From<&Path> for InstigatingSource {
fn from(path: &Path) -> Self {
InstigatingSource::Path(path.to_path_buf())
}
}

View File

@@ -47,11 +47,11 @@ impl SnapshotMiddleware for SnapshotCsv {
value: table_contents,
},
})
.metadata(InstanceMetadata {
instigating_source: Some(entry.path().to_path_buf().into()),
relevant_paths: vec![entry.path().to_path_buf(), meta_path.clone()],
..Default::default()
});
.metadata(
InstanceMetadata::new()
.instigating_source(entry.path())
.relevant_paths(vec![entry.path().to_path_buf(), meta_path.clone()]),
);
if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? {
let meta_contents = meta_entry.contents(vfs)?;

View File

@@ -62,11 +62,11 @@ impl SnapshotMiddleware for SnapshotDir {
.name(instance_name)
.class_name("Folder")
.children(snapshot_children)
.metadata(InstanceMetadata {
instigating_source: Some(entry.path().to_path_buf().into()),
relevant_paths,
..Default::default()
});
.metadata(
InstanceMetadata::new()
.instigating_source(entry.path())
.relevant_paths(relevant_paths),
);
if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? {
let meta_contents = meta_entry.contents(vfs)?;

View File

@@ -87,11 +87,11 @@ fn snapshot_lua_file<F: VfsFetcher>(vfs: &Vfs<F>, entry: &VfsEntry) -> SnapshotI
value: contents_str,
},
})
.metadata(InstanceMetadata {
instigating_source: Some(entry.path().to_path_buf().into()),
relevant_paths: vec![entry.path().to_path_buf(), meta_path.clone()],
..Default::default()
});
.metadata(
InstanceMetadata::new()
.instigating_source(entry.path())
.relevant_paths(vec![entry.path().to_path_buf(), meta_path.clone()]),
);
if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? {
let meta_contents = meta_entry.contents(vfs)?;

View File

@@ -36,11 +36,11 @@ impl SnapshotMiddleware for SnapshotRbxlx {
let snapshot = InstanceSnapshot::from_tree(&temp_tree, root_id)
.name(instance_name)
.metadata(InstanceMetadata {
instigating_source: Some(entry.path().to_path_buf().into()),
relevant_paths: vec![entry.path().to_path_buf()],
..Default::default()
});
.metadata(
InstanceMetadata::new()
.instigating_source(entry.path())
.relevant_paths(vec![entry.path().to_path_buf()]),
);
Ok(Some(snapshot))
}

View File

@@ -46,11 +46,11 @@ impl SnapshotMiddleware for SnapshotRbxm {
if children.len() == 1 {
let snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0])
.name(instance_name)
.metadata(InstanceMetadata {
instigating_source: Some(entry.path().to_path_buf().into()),
relevant_paths: vec![entry.path().to_path_buf()],
..Default::default()
});
.metadata(
InstanceMetadata::new()
.instigating_source(entry.path())
.relevant_paths(vec![entry.path().to_path_buf()]),
);
Ok(Some(snapshot))
} else {

View File

@@ -38,11 +38,11 @@ impl SnapshotMiddleware for SnapshotRbxmx {
if children.len() == 1 {
let snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0])
.name(instance_name)
.metadata(InstanceMetadata {
instigating_source: Some(entry.path().to_path_buf().into()),
relevant_paths: vec![entry.path().to_path_buf()],
..Default::default()
});
.metadata(
InstanceMetadata::new()
.instigating_source(entry.path())
.relevant_paths(vec![entry.path().to_path_buf()]),
);
Ok(Some(snapshot))
} else {

View File

@@ -52,11 +52,11 @@ impl SnapshotMiddleware for SnapshotTxt {
.name(instance_name)
.class_name("StringValue")
.properties(properties)
.metadata(InstanceMetadata {
instigating_source: Some(entry.path().to_path_buf().into()),
relevant_paths: vec![entry.path().to_path_buf(), meta_path.clone()],
..Default::default()
});
.metadata(
InstanceMetadata::new()
.instigating_source(entry.path())
.relevant_paths(vec![entry.path().to_path_buf(), meta_path.clone()]),
);
if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? {
let meta_contents = meta_entry.contents(vfs)?;