Skip to content

Commit

Permalink
refactor(InstanceContext): switch to constructor methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sasial-dev committed Sep 15, 2023
1 parent 3b71f63 commit 8cb9a7b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 39 deletions.
3 changes: 2 additions & 1 deletion src/serve_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ impl ServeSession {

let root_id = tree.get_root_id();

let instance_context = InstanceContext::from(root_project.emit_legacy_scripts);
let instance_context =
InstanceContext::with_emit_legacy_scripts(root_project.emit_legacy_scripts);

log::trace!("Generating snapshot of instances from VFS");
let snapshot = snapshot_from_vfs(&instance_context, &vfs, start_path)?;
Expand Down
31 changes: 9 additions & 22 deletions src/snapshot/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ impl InstanceContext {
}
}

pub fn with_emit_legacy_scripts(emit_legacy_scripts: Option<bool>) -> Self {
Self {
emit_legacy_scripts: emit_legacy_scripts
.unwrap_or_else(|| emit_legacy_scripts_default().unwrap()),
..Self::new()
}
}

/// Extend the list of ignore rules in the context with the given new rules.
pub fn add_path_ignore_rules<I>(&mut self, new_rules: I)
where
Expand All @@ -136,28 +144,7 @@ impl InstanceContext {
}

pub fn set_emit_legacy_scripts(&mut self, emit_legacy_scripts: bool) {
self.emit_legacy_scripts = emit_legacy_scripts
}
}

// serve_session always passes an option from the config file, but tests want it to be explict
#[cfg(test)]
impl From<bool> for InstanceContext {
fn from(emit_legacy_scripts: bool) -> Self {
Self {
emit_legacy_scripts,
..Self::new()
}
}
}

impl From<Option<bool>> for InstanceContext {
fn from(emit_legacy_scripts: Option<bool>) -> Self {
Self {
emit_legacy_scripts: emit_legacy_scripts
.unwrap_or_else(|| emit_legacy_scripts_default().unwrap()),
..Self::new()
}
self.emit_legacy_scripts = emit_legacy_scripts;
}
}

Expand Down
35 changes: 19 additions & 16 deletions src/snapshot_middleware/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ mod test {
let mut vfs = Vfs::new(imfs);

let instance_snapshot = snapshot_lua(
&InstanceContext::from(true),
&InstanceContext::with_emit_legacy_scripts(Some(true)),
&mut vfs,
Path::new("/foo.lua"),
)
Expand All @@ -169,7 +169,7 @@ mod test {
let mut vfs = Vfs::new(imfs);

let instance_snapshot = snapshot_lua(
&InstanceContext::from(false),
&InstanceContext::with_emit_legacy_scripts(Some(false)),
&mut vfs,
Path::new("/foo.lua"),
)
Expand All @@ -190,7 +190,7 @@ mod test {
let mut vfs = Vfs::new(imfs);

let instance_snapshot = snapshot_lua(
&InstanceContext::from(true),
&InstanceContext::with_emit_legacy_scripts(Some(true)),
&mut vfs,
Path::new("/foo.server.lua"),
)
Expand All @@ -211,7 +211,7 @@ mod test {
let mut vfs = Vfs::new(imfs);

let instance_snapshot = snapshot_lua(
&InstanceContext::from(false),
&InstanceContext::with_emit_legacy_scripts(Some(false)),
&mut vfs,
Path::new("/foo.server.lua"),
)
Expand All @@ -232,7 +232,7 @@ mod test {
let mut vfs = Vfs::new(imfs);

let instance_snapshot = snapshot_lua(
&InstanceContext::from(true),
&InstanceContext::with_emit_legacy_scripts(Some(true)),
&mut vfs,
Path::new("/foo.client.lua"),
)
Expand All @@ -253,7 +253,7 @@ mod test {
let mut vfs = Vfs::new(imfs);

let instance_snapshot = snapshot_lua(
&InstanceContext::from(false),
&InstanceContext::with_emit_legacy_scripts(Some(false)),
&mut vfs,
Path::new("/foo.client.lua"),
)
Expand All @@ -279,10 +279,13 @@ mod test {

let mut vfs = Vfs::new(imfs);

let instance_snapshot =
snapshot_lua(&InstanceContext::from(true), &mut vfs, Path::new("/root"))
.unwrap()
.unwrap();
let instance_snapshot = snapshot_lua(
&InstanceContext::with_emit_legacy_scripts(Some(true)),
&mut vfs,
Path::new("/root"),
)
.unwrap()
.unwrap();

insta::with_settings!({ sort_maps => true }, {
insta::assert_yaml_snapshot!(instance_snapshot);
Expand All @@ -309,7 +312,7 @@ mod test {
let mut vfs = Vfs::new(imfs);

let instance_snapshot = snapshot_lua(
&InstanceContext::from(true),
&InstanceContext::with_emit_legacy_scripts(Some(true)),
&mut vfs,
Path::new("/foo.lua"),
)
Expand Down Expand Up @@ -341,7 +344,7 @@ mod test {
let mut vfs = Vfs::new(imfs);

let instance_snapshot = snapshot_lua(
&InstanceContext::from(false),
&InstanceContext::with_emit_legacy_scripts(Some(false)),
&mut vfs,
Path::new("/foo.lua"),
)
Expand Down Expand Up @@ -373,7 +376,7 @@ mod test {
let mut vfs = Vfs::new(imfs);

let instance_snapshot = snapshot_lua(
&InstanceContext::from(true),
&InstanceContext::with_emit_legacy_scripts(Some(true)),
&mut vfs,
Path::new("/foo.server.lua"),
)
Expand Down Expand Up @@ -405,7 +408,7 @@ mod test {
let mut vfs = Vfs::new(imfs);

let instance_snapshot = snapshot_lua(
&InstanceContext::from(false),
&InstanceContext::with_emit_legacy_scripts(Some(false)),
&mut vfs,
Path::new("/foo.server.lua"),
)
Expand Down Expand Up @@ -439,7 +442,7 @@ mod test {
let mut vfs = Vfs::new(imfs);

let instance_snapshot = snapshot_lua(
&InstanceContext::from(true),
&InstanceContext::with_emit_legacy_scripts(Some(true)),
&mut vfs,
Path::new("/bar.server.lua"),
)
Expand Down Expand Up @@ -473,7 +476,7 @@ mod test {
let mut vfs = Vfs::new(imfs);

let instance_snapshot = snapshot_lua(
&InstanceContext::from(false),
&InstanceContext::with_emit_legacy_scripts(Some(false)),
&mut vfs,
Path::new("/bar.server.lua"),
)
Expand Down

0 comments on commit 8cb9a7b

Please sign in to comment.