use std::path::{Path, PathBuf};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Endpoint {
NamedPipe(String),
UnixSocket(PathBuf),
}
#[derive(Debug, Clone)]
pub struct ResolvedEndpoint {
data_dir: PathBuf,
target: Endpoint,
}
impl ResolvedEndpoint {
pub fn data_dir(&self) -> &Path {
&self.data_dir
}
pub fn target(&self) -> &Endpoint {
&self.target
}
pub fn socket_name(&self) -> String {
match &self.target {
Endpoint::NamedPipe(name) => name.clone(),
Endpoint::UnixSocket(path) => path.to_string_lossy().into_owned(),
}
}
}
pub fn endpoint_for(data_dir: &Path) -> ResolvedEndpoint {
#[cfg(windows)]
let target = {
use sha2::{Digest, Sha256};
let resolved = data_dir.to_string_lossy().to_lowercase();
let digest = Sha256::digest(resolved.as_bytes());
let suffix: String = digest[..12]
.iter()
.map(|byte| format!("{byte:02x}"))
.collect();
Endpoint::NamedPipe(format!(r"\\.\pipe\openbutler-{suffix}"))
};
#[cfg(not(windows))]
let target = Endpoint::UnixSocket(data_dir.join("daemon.sock"));
ResolvedEndpoint {
data_dir: data_dir.to_path_buf(),
target,
}
}