use std::ffi::{OsStr, OsString};
#[cfg(test)]
pub(crate) mod tests;
#[cfg(unix)]
pub fn make_local_socket_name(unique_id: &str) -> OsString {
let mut base = if let Some(runtime_dir) = std::env::var_os("XDG_RUNTIME_DIR") {
std::path::PathBuf::from(runtime_dir)
} else {
std::env::temp_dir()
};
let socket_name = format!("nu.{}.{}.sock", std::process::id(), unique_id);
base.push(socket_name);
base.into()
}
#[cfg(unix)]
pub fn interpret_local_socket_name(
name: &OsStr,
) -> Result<interprocess::local_socket::Name<'_>, std::io::Error> {
use interprocess::local_socket::{GenericFilePath, ToFsName};
name.to_fs_name::<GenericFilePath>()
}
#[cfg(windows)]
pub fn make_local_socket_name(unique_id: &str) -> OsString {
format!("nu.{}.{}", std::process::id(), unique_id).into()
}
#[cfg(windows)]
pub fn interpret_local_socket_name(
name: &OsStr,
) -> Result<interprocess::local_socket::Name<'_>, std::io::Error> {
use interprocess::local_socket::{GenericNamespaced, ToNsName};
name.to_ns_name::<GenericNamespaced>()
}
#[cfg(not(windows))]
pub fn is_would_block_err(err: &std::io::Error) -> bool {
err.kind() == std::io::ErrorKind::WouldBlock
}
#[cfg(windows)]
pub fn is_would_block_err(err: &std::io::Error) -> bool {
err.kind() == std::io::ErrorKind::WouldBlock
|| err.raw_os_error().is_some_and(|e| {
e as i64 == windows::Win32::Foundation::ERROR_PIPE_LISTENING.0 as i64
})
}