tawasuyu

tawasuyu/brahman

Rust 405TOML 156Markdown 41JSON 175449 Definiciones

⎇ d3649bfd1a80153652c2a028315369bb4b184024 crates/core/brahman-admin/src/client.rs Rust · 48 líneas · 1.5 KB Historia Quién tocó qué crudo

feat(nouser): yahweh widget — nouser-explorer panel GPUI 5c41ef920 · Sergio ·

1//! Cliente admin: lee un `StatusSnapshot` desde un socket admin.
2
3use std::path::Path;
4
5use thiserror::Error;
6use tokio::io::{AsyncBufReadExt, BufReader};
7use tokio::net::UnixStream;
8
9use crate::snapshot::StatusSnapshot;
10
11#[derive(Debug, Error)]
12pub enum AdminError {
13    #[error("E/S: {0}")]
14    Io(#[from] std::io::Error),
15    #[error("respuesta vacía")]
16    Empty,
17    #[error("JSON inválido: {0}")]
18    Json(#[from] serde_json::Error),
19}
20
21/// Conecta al socket admin, lee la línea JSON y deserializa.
22pub async fn query(path: impl AsRef<Path>) -> Result<StatusSnapshot, AdminError> {
23    let stream = UnixStream::connect(path).await?;
24    let mut reader = BufReader::new(stream);
25    let mut line = String::new();
26    let n = reader.read_line(&mut line).await?;
27    if n == 0 {
28        return Err(AdminError::Empty);
29    }
30    let snapshot = serde_json::from_str(&line)?;
31    Ok(snapshot)
32}
33
34/// Variante sync de [`query`] para callers que no tienen runtime tokio
35/// (típicamente: GUIs con su propio executor, como GPUI).
36pub fn query_blocking(path: impl AsRef<Path>) -> Result<StatusSnapshot, AdminError> {
37    use std::io::{BufRead, BufReader as StdBufReader};
38    use std::os::unix::net::UnixStream as StdUnixStream;
39    let stream = StdUnixStream::connect(path)?;
40    let mut reader = StdBufReader::new(stream);
41    let mut line = String::new();
42    let n = reader.read_line(&mut line)?;
43    if n == 0 {
44        return Err(AdminError::Empty);
45    }
46    let snapshot = serde_json::from_str(&line)?;
47    Ok(snapshot)
48}