Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dir): Allow converting Path to InMemoryDir #303

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion crates/snapbox/src/dir/dir.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::DirSource;
use super::FileType;

/// Collection of files
Expand Down Expand Up @@ -94,9 +95,15 @@ impl Dir for std::ffi::OsString {
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InMemoryDir {
content: std::collections::BTreeMap<std::path::PathBuf, DirEntry>,
source: DirSource,
}

impl InMemoryDir {
fn with_source(mut self, source: impl Into<DirSource>) -> Self {
self.source = source.into();
self
}

/// Initialize a test fixture directory `root`
pub fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
for (relpath, entry) in &self.content {
Expand All @@ -107,6 +114,30 @@ impl InMemoryDir {
}
}

impl From<&'_ std::path::Path> for InMemoryDir {
fn from(root: &'_ std::path::Path) -> Self {
PathIter::infer_iter(root)
.collect::<Self>()
.with_source(DirSource::path(root))
}
}

impl From<&'_ std::path::PathBuf> for InMemoryDir {
fn from(root: &'_ std::path::PathBuf) -> Self {
PathIter::infer_iter(root.as_path())
.collect::<Self>()
.with_source(DirSource::path(root))
}
}

impl From<std::path::PathBuf> for InMemoryDir {
fn from(root: std::path::PathBuf) -> Self {
PathIter::infer_iter(root.as_path())
.collect::<Self>()
.with_source(DirSource::path(root))
}
}

pub type InMemoryDirIter = std::collections::btree_map::IntoIter<std::path::PathBuf, DirEntry>;

impl<P, E> FromIterator<(P, E)> for InMemoryDir
Expand Down Expand Up @@ -154,7 +185,10 @@ where
}
}
}
Self { content }
Self {
content,
source: DirSource::inmemory(),
}
}
}

Expand All @@ -167,6 +201,11 @@ pub struct PathIter {
}

impl PathIter {
fn infer_iter(root: &std::path::Path) -> Self {
let binary = false;
Self::iter_(root, binary)
}

fn binary_iter(root: &std::path::Path) -> Self {
let binary = true;
Self::iter_(root, binary)
Expand Down
2 changes: 2 additions & 0 deletions crates/snapbox/src/dir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod diff;
mod dir;
mod ops;
mod root;
mod source;
#[cfg(test)]
mod tests;

Expand All @@ -24,6 +25,7 @@ pub use dir::PathIter;
pub use ops::resolve_dir;
pub use ops::strip_trailing_slash;
pub use root::DirRoot;
pub use source::DirSource;

#[cfg(feature = "dir")]
pub(crate) use ops::canonicalize;
Expand Down
39 changes: 39 additions & 0 deletions crates/snapbox/src/dir/source.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DirSource {
inner: DirSourceInner,
}

#[derive(Clone, Debug, PartialEq, Eq)]
enum DirSourceInner {
InMemory,
Path(std::path::PathBuf),
}

impl DirSource {
pub(crate) fn inmemory() -> Self {
Self {
inner: DirSourceInner::InMemory,
}
}

pub fn is_inmemory(&self) -> bool {
matches!(self.inner, DirSourceInner::InMemory)
}

pub fn path(path: impl Into<std::path::PathBuf>) -> Self {
Self {
inner: DirSourceInner::Path(path.into()),
}
}

pub fn is_path(&self) -> bool {
self.as_path().is_some()
}

pub fn as_path(&self) -> Option<&std::path::Path> {
match &self.inner {
DirSourceInner::Path(value) => Some(value.as_ref()),
_ => None,
}
}
}
Loading