airup_sdk/
lib.rs

1//! # The Airup SDK
2//! The Airup SDK provides interface to access Airup facilities, for example, interacting with the daemon, `airupd`.
3
4pub mod build;
5pub mod debug;
6pub mod error;
7pub mod extapi;
8pub mod extension;
9pub mod files;
10pub mod info;
11pub mod prelude;
12pub mod rpc;
13pub mod system;
14
15mod util;
16
17#[cfg(feature = "nonblocking")]
18pub mod nonblocking;
19
20#[cfg(feature = "blocking")]
21pub mod blocking;
22
23#[cfg(feature = "ffi")]
24pub mod ffi;
25
26pub use error::ApiError as Error;
27
28use serde::{Serialize, de::DeserializeOwned};
29use std::{
30    path::{Path, PathBuf},
31    sync::OnceLock,
32};
33
34/// Returns default path of Airup's IPC socket.
35///
36/// If environment `AIRUP_SOCK` was present, returns the value of `AIRUP_SOCK`. Otherwise it returns `$runtime_dir/airupd.sock`,
37/// which is related to the compile-time `build_manifest.json`.
38pub fn socket_path() -> &'static Path {
39    static SOCKET_PATH: OnceLock<&'static Path> = OnceLock::new();
40
41    let default_runtime_dir = build::try_manifest()
42        .map(|x| x.runtime_dir.as_path())
43        .unwrap_or(Path::new("/run/airup"));
44    SOCKET_PATH.get_or_init(|| {
45        Box::leak(
46            std::env::var("AIRUP_SOCK")
47                .map(PathBuf::from)
48                .unwrap_or_else(|_| default_runtime_dir.join("airupd.sock"))
49                .into(),
50        )
51    })
52}
53
54/// A trait that unifies `async` and `non-async` connections.
55pub trait Connection {
56    /// Return type of the [`Connection::invoke`] method.
57    type Invoke<'a, T: 'a>
58    where
59        Self: 'a;
60
61    /// Invokes specified method with given parameters on the connection, then wait for a response.
62    fn invoke<'a, P: Serialize + Send + 'a, T: DeserializeOwned + 'a>(
63        &'a mut self,
64        method: &'a str,
65        params: P,
66    ) -> Self::Invoke<'a, T>;
67}