a7n_platform/
local_socket.rs1use std::path::{Path, PathBuf};
6
7pub use interprocess::local_socket::tokio::{Listener, Stream};
8
9#[cfg(unix)]
10#[path = "local_socket/unix.rs"]
11mod imp;
12#[cfg(windows)]
13#[path = "local_socket/windows.rs"]
14mod imp;
15
16#[derive(Debug, Clone, PartialEq, Eq)]
18pub struct SocketLocation(PathBuf);
19
20impl SocketLocation {
21 #[must_use]
23 pub fn new(location: impl Into<PathBuf>) -> Self {
24 Self(location.into())
25 }
26
27 #[must_use]
28 pub fn as_path(&self) -> &Path {
29 &self.0
30 }
31}
32
33#[derive(Debug, thiserror::Error)]
35pub enum SocketError {
36 #[error("no per-user runtime/cache directory or username is available")]
37 NoUserLocation,
38 #[error("socket path must be absolute and at most {max_bytes} bytes: {path}")]
39 InvalidPath { path: PathBuf, max_bytes: usize },
40 #[error("socket directory must be a real directory owned by this user with mode 0700: {0}")]
41 InsecureDirectory(PathBuf),
42 #[error("refusing to replace a socket path that is not a socket owned by this user: {0}")]
43 InsecureSocket(PathBuf),
44 #[error("local socket operation timed out")]
45 Timeout,
46 #[error(transparent)]
47 Io(#[from] std::io::Error),
48}
49
50pub fn default_location() -> Result<SocketLocation, SocketError> {
55 imp::default_location()
56}
57
58pub async fn bind_user_only(location: &SocketLocation) -> Result<Listener, SocketError> {
63 imp::bind_user_only(location).await
64}
65
66pub async fn connect(location: &SocketLocation) -> Result<Stream, SocketError> {
71 imp::connect(location).await
72}