Skip to main content

walrus_client/
lib.rs

1//! Walrus client library — Unix domain socket client for connecting to a
2//! walrus-gateway. Used by walrus-cli and other platform clients.
3
4pub use connection::Connection;
5use std::path::PathBuf;
6
7pub mod connection;
8
9/// Client configuration for connecting to a walrus-gateway.
10#[derive(Debug, Clone)]
11pub struct ClientConfig {
12    /// Gateway Unix domain socket path.
13    pub socket_path: PathBuf,
14}
15
16impl Default for ClientConfig {
17    fn default() -> Self {
18        Self {
19            socket_path: default_socket_path(),
20        }
21    }
22}
23
24/// Default socket path: `~/.walrus/walrus.sock`.
25fn default_socket_path() -> PathBuf {
26    dirs::home_dir()
27        .expect("no home directory")
28        .join(".walrus")
29        .join("walrus.sock")
30}
31
32/// Unix domain socket client for the walrus-gateway.
33///
34/// Holds configuration. Call [`WalrusClient::connect`] to establish a
35/// connection.
36pub struct WalrusClient {
37    config: ClientConfig,
38}
39
40impl WalrusClient {
41    /// Create a new client with the given configuration.
42    pub fn new(config: ClientConfig) -> Self {
43        Self { config }
44    }
45
46    /// Access the client configuration.
47    pub fn config(&self) -> &ClientConfig {
48        &self.config
49    }
50
51    /// Set the gateway socket path.
52    pub fn socket_path(mut self, path: impl Into<PathBuf>) -> Self {
53        self.config.socket_path = path.into();
54        self
55    }
56
57    /// Connect to the gateway and return a [`Connection`].
58    pub async fn connect(&self) -> anyhow::Result<Connection> {
59        Connection::connect(&self.config.socket_path).await
60    }
61}