WIP: refactor daemon API types to daemon/api.rs

This commit is contained in:
Qyriad 2026-03-22 17:15:04 +01:00
parent 420fac5f18
commit d4b40e8cc1
4 changed files with 92 additions and 73 deletions

View file

@ -1,6 +1,5 @@
use std::{
env, io,
ops::Deref,
os::fd::{AsFd, BorrowedFd, IntoRawFd, OwnedFd, RawFd},
sync::{
Arc, LazyLock,
@ -15,13 +14,16 @@ use mio::{Events, Interest, Poll, Token, net::UnixListener, unix::SourceFd};
use rustix::{buffer::spare_capacity, net::SocketFlags, process::Uid};
use serde::{Deserialize, Serialize};
use serde_json::StreamDeserializer;
use crate::prelude::*;
pub mod api;
use api::DaemonCmd;
use crate::{
SourceFile, SourceLine,
daemon_tokfd::{FdInfo, FdKind},
prelude::*,
};
use crate::{OwnedFdWithFlags, TokenFd};
@ -43,76 +45,6 @@ pub static TMPDIR: LazyLock<&'static Path> = LazyLock::new(|| {
Box::leak(dir)
});
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Deserialize, Serialize)]
#[serde(untagged)]
pub enum ConvenientAttrPath {
Dotted(Box<str>),
Split(Box<[Box<str>]>),
}
impl ConvenientAttrPath {
/// Not currently used, but here for completeness.
#[expect(dead_code)]
pub fn clone_from_dotted(s: &str) -> Self {
Self::Dotted(Box::from(s))
}
/// Not currently used, but here for completeness.
#[expect(dead_code)]
pub fn clone_from_split(s: &[&str]) -> Self {
Self::from_str_iter(s.iter().map(Deref::deref))
}
pub fn from_str_iter<'i, I>(iter: I) -> Self
where
I: Iterator<Item = &'i str>,
{
let boxed = iter.map(Box::from);
Self::Split(Box::from_iter(boxed))
}
pub fn to_nix_decl(&self) -> Box<str> {
use ConvenientAttrPath::*;
match self {
Dotted(s) => s.clone(),
Split(path) => {
// FIXME: quote if necessary
path.join(".").into_boxed_str()
},
}
}
}
impl<'i> FromIterator<&'i str> for ConvenientAttrPath {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = &'i str>,
{
Self::from_str_iter(iter.into_iter())
}
}
impl FromIterator<Box<str>> for ConvenientAttrPath {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = Box<str>>,
{
Self::Split(Box::from_iter(iter))
}
}
#[derive(Debug, Clone, PartialEq)]
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(tag = "action", content = "args", rename_all = "snake_case")]
// FIXME: rename to not confuse with the clap argument type.
pub enum DaemonCmd {
Append {
name: ConvenientAttrPath,
value: Box<str>,
},
}
const TIMEOUT_NEVER: Option<Duration> = None;
static NEXT_TOKEN_NUMBER: AtomicUsize = AtomicUsize::new(1);

59
src/daemon/api.rs Normal file
View file

@ -0,0 +1,59 @@
use std::ops::Deref;
use crate::prelude::*;
use serde::{Deserialize, Serialize};
mod impls;
/// Unfortunately, empty-string identifiers are valid Nix...
///
/// This type does not provide a [`Default`] impl, however.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Deserialize, Serialize)]
#[serde(untagged)]
pub enum ConvenientAttrPath {
Dotted(Box<str>),
Split(Box<[Box<str>]>),
}
impl ConvenientAttrPath {
pub fn clone_from_dotted(s: &str) -> Self {
Self::Dotted(Box::from(s))
}
pub fn clone_from_split(s: &[&str]) -> Self {
Self::from_str_iter(s.iter().map(Deref::deref))
}
pub fn from_str_iter<'i, I>(iter: I) -> Self
where
I: IntoIterator<Item = &'i str>,
{
let iter = iter.into_iter();
let boxed = iter.map(Box::from);
Self::Split(Box::from_iter(boxed))
}
pub fn to_nix_decl(&self) -> Box<str> {
use ConvenientAttrPath::*;
match self {
Dotted(s) => s.clone(),
Split(path) => {
// FIXME: quote if necessary
path.join(".").into_boxed_str()
},
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(tag = "action", content = "args", rename_all = "snake_case")]
// FIXME: rename to not confuse with the clap argument type.
pub enum DaemonCmd {
Append {
name: ConvenientAttrPath,
value: Box<str>,
},
}

27
src/daemon/api/impls.rs Normal file
View file

@ -0,0 +1,27 @@
use super::ConvenientAttrPath;
impl<'i> FromIterator<&'i str> for ConvenientAttrPath {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = &'i str>,
{
Self::from_str_iter(iter)
}
}
impl FromIterator<Box<str>> for ConvenientAttrPath {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = Box<str>>,
{
Self::Split(Box::from_iter(iter))
}
}
impl FromIterator<String> for ConvenientAttrPath {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = String>,
{
let iter = iter.into_iter();
Self::Split(Box::from_iter(iter.map(String::into_boxed_str)))
}
}

View file

@ -55,6 +55,7 @@ mod color;
pub use color::{_CLI_ENABLE_COLOR, SHOULD_COLOR};
mod daemon;
pub use daemon::Daemon;
pub use daemon::api as daemon_api;
mod daemon_io;
pub use daemon_io::OwnedFdWithFlags;
mod daemon_tokfd;