From d4b40e8cc17f861bdd1ccb011c58b459707c3b6a Mon Sep 17 00:00:00 2001 From: Qyriad Date: Sun, 22 Mar 2026 17:15:04 +0100 Subject: [PATCH] WIP: refactor daemon API types to daemon/api.rs --- src/daemon.rs | 78 +++-------------------------------------- src/daemon/api.rs | 59 +++++++++++++++++++++++++++++++ src/daemon/api/impls.rs | 27 ++++++++++++++ src/lib.rs | 1 + 4 files changed, 92 insertions(+), 73 deletions(-) create mode 100644 src/daemon/api.rs create mode 100644 src/daemon/api/impls.rs diff --git a/src/daemon.rs b/src/daemon.rs index 4272486..7fe7898 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -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), - Split(Box<[Box]>), -} - -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, - { - let boxed = iter.map(Box::from); - Self::Split(Box::from_iter(boxed)) - } - - pub fn to_nix_decl(&self) -> Box { - 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(iter: I) -> Self - where - I: IntoIterator, - { - Self::from_str_iter(iter.into_iter()) - } -} - -impl FromIterator> for ConvenientAttrPath { - fn from_iter(iter: I) -> Self - where - I: IntoIterator>, - { - 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, - }, -} - const TIMEOUT_NEVER: Option = None; static NEXT_TOKEN_NUMBER: AtomicUsize = AtomicUsize::new(1); diff --git a/src/daemon/api.rs b/src/daemon/api.rs new file mode 100644 index 0000000..077ce0f --- /dev/null +++ b/src/daemon/api.rs @@ -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), + Split(Box<[Box]>), +} + +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, + { + let iter = iter.into_iter(); + let boxed = iter.map(Box::from); + Self::Split(Box::from_iter(boxed)) + } + + pub fn to_nix_decl(&self) -> Box { + 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, + }, +} diff --git a/src/daemon/api/impls.rs b/src/daemon/api/impls.rs new file mode 100644 index 0000000..41d64b9 --- /dev/null +++ b/src/daemon/api/impls.rs @@ -0,0 +1,27 @@ +use super::ConvenientAttrPath; + +impl<'i> FromIterator<&'i str> for ConvenientAttrPath { + fn from_iter(iter: I) -> Self + where + I: IntoIterator, + { + Self::from_str_iter(iter) + } +} +impl FromIterator> for ConvenientAttrPath { + fn from_iter(iter: I) -> Self + where + I: IntoIterator>, + { + Self::Split(Box::from_iter(iter)) + } +} +impl FromIterator for ConvenientAttrPath { + fn from_iter(iter: I) -> Self + where + I: IntoIterator, + { + let iter = iter.into_iter(); + Self::Split(Box::from_iter(iter.map(String::into_boxed_str))) + } +} diff --git a/src/lib.rs b/src/lib.rs index 40f983b..b2b597a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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;