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

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)))
}
}