Make Imfs ordering deterministic by using BTreeSet

This commit is contained in:
Lucien Greathouse 2019-03-12 16:23:21 -07:00
parent 684a33c418
commit 7ca3670338
2 changed files with 38 additions and 13 deletions

View file

@ -1,9 +1,10 @@
use std::{ use std::{
collections::{HashMap, HashSet}, cmp::Ordering,
path::{self, Path, PathBuf}, collections::{HashMap, HashSet, BTreeSet},
fmt, fmt,
fs, fs,
io, io,
path::{self, Path, PathBuf},
}; };
use failure::Fail; use failure::Fail;
@ -237,7 +238,7 @@ impl Imfs {
} else if metadata.is_dir() { } else if metadata.is_dir() {
let item = ImfsItem::Directory(ImfsDirectory { let item = ImfsItem::Directory(ImfsDirectory {
path: path.to_path_buf(), path: path.to_path_buf(),
children: HashSet::new(), children: BTreeSet::new(),
}); });
self.items.insert(path.to_path_buf(), item); self.items.insert(path.to_path_buf(), item);
@ -285,19 +286,43 @@ impl Imfs {
} }
} }
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ImfsFile { pub struct ImfsFile {
pub path: PathBuf, pub path: PathBuf,
pub contents: Vec<u8>, pub contents: Vec<u8>,
} }
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] impl PartialOrd for ImfsFile {
pub struct ImfsDirectory { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
pub path: PathBuf, Some(self.cmp(other))
pub children: HashSet<PathBuf>, }
} }
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] impl Ord for ImfsFile {
fn cmp(&self, other: &Self) -> Ordering {
self.path.cmp(&other.path)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ImfsDirectory {
pub path: PathBuf,
pub children: BTreeSet<PathBuf>,
}
impl PartialOrd for ImfsDirectory {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for ImfsDirectory {
fn cmp(&self, other: &Self) -> Ordering {
self.path.cmp(&other.path)
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum ImfsItem { pub enum ImfsItem {
File(ImfsFile), File(ImfsFile),
Directory(ImfsDirectory), Directory(ImfsDirectory),

View file

@ -1,5 +1,5 @@
use std::{ use std::{
collections::{HashMap, HashSet}, collections::{HashMap, HashSet, BTreeSet},
fs, fs,
path::PathBuf, path::PathBuf,
}; };
@ -80,7 +80,7 @@ fn base_tree() -> Result<(TempDir, Imfs, ExpectedImfs, TestResources), Error> {
expected_roots.insert(root.path().to_path_buf()); expected_roots.insert(root.path().to_path_buf());
let root_item = { let root_item = {
let mut children = HashSet::new(); let mut children = BTreeSet::new();
children.insert(foo_path.clone()); children.insert(foo_path.clone());
children.insert(bar_path.clone()); children.insert(bar_path.clone());
@ -91,7 +91,7 @@ fn base_tree() -> Result<(TempDir, Imfs, ExpectedImfs, TestResources), Error> {
}; };
let foo_item = { let foo_item = {
let mut children = HashSet::new(); let mut children = BTreeSet::new();
children.insert(baz_path.clone()); children.insert(baz_path.clone());
ImfsItem::Directory(ImfsDirectory { ImfsItem::Directory(ImfsDirectory {
@ -199,7 +199,7 @@ fn adding_folder() -> Result<(), Error> {
} }
let folder_item = { let folder_item = {
let mut children = HashSet::new(); let mut children = BTreeSet::new();
children.insert(file1_path.clone()); children.insert(file1_path.clone());
children.insert(file2_path.clone()); children.insert(file2_path.clone());