openbutler-protocol 0.0.1

The wire contract between the OpenButler daemon and its clients: frame encoding, action envelopes, and the shared state types.
Documentation
//! Endpoint discovery — the address a client dials for a data directory.
//!
//! The two sides of discovery live here so client and daemon can never
//! derive different names: the data directory hashes into a Windows named
//! pipe (every client on the machine derives the same name) and maps to a
//! unix socket file on POSIX. Ported from the daemon's
//! `server/socket/endpoint.rs` (read 2026-11-22), which stays as a
//! re-export of this module.
//!
//! Data-directory *resolution* (the `OPENBUTLER_DATA_DIR` rung) stays with
//! each side's own config module: this module takes the directory as
//! input and stays pure path math.

use std::path::{Path, PathBuf};

/// Where the daemon listens.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Endpoint {
    /// Windows named pipe: the full `\\.\pipe\...` name.
    NamedPipe(String),
    /// POSIX unix socket file.
    UnixSocket(PathBuf),
}

/// A resolved endpoint: target plus the data directory it belongs to.
#[derive(Debug, Clone)]
pub struct ResolvedEndpoint {
    data_dir: PathBuf,
    target: Endpoint,
}

impl ResolvedEndpoint {
    /// The data directory this endpoint serves (dispatch reads state here).
    pub fn data_dir(&self) -> &Path {
        &self.data_dir
    }

    /// The transport target (for tests and diagnostics).
    pub fn target(&self) -> &Endpoint {
        &self.target
    }

    /// The connect target as an owned string (lossy only for non-UTF-8 POSIX
    /// paths, which the product never creates).
    pub fn socket_name(&self) -> String {
        match &self.target {
            Endpoint::NamedPipe(name) => name.clone(),
            Endpoint::UnixSocket(path) => path.to_string_lossy().into_owned(),
        }
    }
}

/// Derives the endpoint for a data directory (`endpoint_for`).
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,
    }
}