Files
boxmaker/crates/memoryd/src/config.rs
T
kyle b84b9af540 memoryd: the crate, memoryd.toml, and listening on a socket
Implemented-By: OpenCode session (model recorded in docs/implementer-log.md)
2026-09-25 16:41:44 -07:00

141 lines
4.0 KiB
Rust

//! `memoryd` configuration: read `memoryd.toml` into a typed `Config` (M5a spec, section 11).
//!
//! Unknown keys are errors in every table, as in the other roles: a misspelt key that silently
//! fell back to its default would be a setting the owner believes is set and is not.
use std::path::{Path, PathBuf};
use proto::DataClass;
use serde::Deserialize;
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields, default)]
pub struct Paths {
pub home: PathBuf,
}
impl Default for Paths {
fn default() -> Self {
Self {
home: std::env::var_os("BOXMAKER_HOME")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("/var/lib/boxmaker")),
}
}
}
/// `[memory]`: the starter `soul.md`, and whether identity writes wait for the owner.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields, default)]
pub struct Memory {
/// A relative path is resolved next to the config file by `Config::load`.
pub soul_starter: PathBuf,
pub review: bool,
}
impl Default for Memory {
fn default() -> Self {
Self {
soul_starter: PathBuf::from("soul.md"),
review: false,
}
}
}
/// `[index]`: records above `max_class` are not indexed.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields, default)]
pub struct Index {
pub max_class: DataClass,
}
impl Default for Index {
fn default() -> Self {
Self {
max_class: DataClass::Private,
}
}
}
/// An empty path means "the default under `home`".
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Default)]
#[serde(deny_unknown_fields, default)]
pub struct Sockets {
#[serde(rename = "loop")]
pub loopd: PathBuf,
pub broker: PathBuf,
pub admin: PathBuf,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Default)]
#[serde(deny_unknown_fields, default)]
pub struct Config {
pub paths: Paths,
pub memory: Memory,
pub index: Index,
pub sockets: Sockets,
}
#[derive(Debug)]
pub enum ConfigError {
Read(PathBuf, std::io::Error),
Parse(PathBuf, toml::de::Error),
}
impl std::fmt::Display for ConfigError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
ConfigError::Read(path, err) => write!(f, "{}: {err}", path.display()),
ConfigError::Parse(path, err) => write!(f, "{}: {err}", path.display()),
}
}
}
impl std::error::Error for ConfigError {}
impl Config {
pub fn parse(text: &str) -> Result<Config, toml::de::Error> {
toml::from_str(text)
}
/// Read and parse `path`; a relative `soul_starter` is taken to be next to it.
pub fn load(path: &Path) -> Result<Config, ConfigError> {
let text =
std::fs::read_to_string(path).map_err(|e| ConfigError::Read(path.to_path_buf(), e))?;
let mut config =
Config::parse(&text).map_err(|e| ConfigError::Parse(path.to_path_buf(), e))?;
if config.memory.soul_starter.is_relative() {
let base = path.parent().unwrap_or_else(|| Path::new("."));
config.memory.soul_starter = base.join(&config.memory.soul_starter);
}
Ok(config)
}
fn socket(&self, set: &Path, default: &str) -> PathBuf {
if set.as_os_str().is_empty() {
self.paths.home.join(default)
} else {
set.to_path_buf()
}
}
pub fn loop_socket(&self) -> PathBuf {
self.socket(&self.sockets.loopd, "run/loop-memory/memory.sock")
}
pub fn broker_socket(&self) -> PathBuf {
self.socket(&self.sockets.broker, "run/broker-memory/memory.sock")
}
pub fn admin_socket(&self) -> PathBuf {
self.socket(&self.sockets.admin, "run/owner-memory/admin.sock")
}
pub fn memory_dir(&self) -> PathBuf {
self.paths.home.join("memory")
}
pub fn index_path(&self) -> PathBuf {
self.paths.home.join("index/memory.sqlite3")
}
pub fn sessions_dir(&self) -> PathBuf {
self.paths.home.join("sessions")
}
}