Skip to content

Commit

Permalink
utils: introduce ContainingPath trait
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspar030 committed Dec 22, 2023
1 parent 25cf214 commit f431bf6
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

use camino::Utf8PathBuf;
use indexmap::IndexMap;

pub(crate) fn calculate_hash<T: Hash>(t: &T) -> u64 {
let mut s = DefaultHasher::new();
t.hash(&mut s);
Expand All @@ -20,3 +23,17 @@ pub enum StringOrMapString {
String(String),
Map(std::collections::HashMap<String, String>),
}

pub(crate) trait ContainingPath<T: AsRef<std::path::Path>> {
fn get_containing_path(&self, path: &T) -> Option<&T>;
}

impl ContainingPath<Utf8PathBuf> for IndexMap<&Utf8PathBuf, Utf8PathBuf> {
fn get_containing_path(&self, path: &Utf8PathBuf) -> Option<&Utf8PathBuf> {
self.get(path).or_else(|| {
self.iter()
.find(|(k, _)| path.starts_with(k))
.map(|(_, v)| v)
})
}
}

0 comments on commit f431bf6

Please sign in to comment.