use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CorsaSessionKey {
tsconfig_path: PathBuf,
}
impl CorsaSessionKey {
pub fn new(tsconfig_path: impl AsRef<Path>) -> Self {
let path = tsconfig_path.as_ref().to_path_buf();
let canonical = std::fs::canonicalize(&path).unwrap_or(path);
Self {
tsconfig_path: canonical,
}
}
pub fn tsconfig_path(&self) -> &Path {
&self.tsconfig_path
}
}
impl Hash for CorsaSessionKey {
fn hash<H: Hasher>(&self, state: &mut H) {
self.tsconfig_path.hash(state);
}
}
#[cfg(test)]
mod tests {
use super::CorsaSessionKey;
#[test]
fn equal_keys_compare_equal() {
let a = CorsaSessionKey::new("./tsconfig.json");
let b = CorsaSessionKey::new("./tsconfig.json");
assert_eq!(a, b);
}
#[test]
fn different_paths_yield_different_keys() {
let a = CorsaSessionKey::new("./tsconfig.json");
let b = CorsaSessionKey::new("./tsconfig.app.json");
assert_ne!(a, b);
}
}