use std::path::{Path, PathBuf};
pub use interprocess::local_socket::tokio::{Listener, Stream};
#[cfg(unix)]
#[path = "local_socket/unix.rs"]
mod imp;
#[cfg(windows)]
#[path = "local_socket/windows.rs"]
mod imp;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SocketLocation(PathBuf);
impl SocketLocation {
#[must_use]
pub fn new(location: impl Into<PathBuf>) -> Self {
Self(location.into())
}
#[must_use]
pub fn as_path(&self) -> &Path {
&self.0
}
}
#[derive(Debug, thiserror::Error)]
pub enum SocketError {
#[error("no per-user runtime/cache directory or username is available")]
NoUserLocation,
#[error("socket path must be absolute and at most {max_bytes} bytes: {path}")]
InvalidPath { path: PathBuf, max_bytes: usize },
#[error("socket directory must be a real directory owned by this user with mode 0700: {0}")]
InsecureDirectory(PathBuf),
#[error("refusing to replace a socket path that is not a socket owned by this user: {0}")]
InsecureSocket(PathBuf),
#[error("local socket operation timed out")]
Timeout,
#[error(transparent)]
Io(#[from] std::io::Error),
}
pub fn default_location() -> Result<SocketLocation, SocketError> {
imp::default_location()
}
pub async fn bind_user_only(location: &SocketLocation) -> Result<Listener, SocketError> {
imp::bind_user_only(location).await
}
pub async fn connect(location: &SocketLocation) -> Result<Stream, SocketError> {
imp::connect(location).await
}