feat: Grundgerüst mit Konfiguration, SQLite-Schema und Selbsttest (M2.1)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
11
crates/nexus-core/Cargo.toml
Normal file
11
crates/nexus-core/Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "nexus-core"
|
||||
description = "Nexus: gemeinsame Typen, Fehler und Konfiguration"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
serde.workspace = true
|
||||
thiserror.workspace = true
|
||||
toml.workspace = true
|
||||
10
crates/nexus-core/README.md
Normal file
10
crates/nexus-core/README.md
Normal file
@ -0,0 +1,10 @@
|
||||
# nexus-core
|
||||
|
||||
Gemeinsame Typen, Fehler und Konfiguration. Keine Abhängigkeit zu Datenbank oder Netzwerk.
|
||||
|
||||
**Version:** wie der Workspace (`0.1.0-dev`). **Schnittstelle:**
|
||||
- `Config` (`from_toml`, `load`, `validate`, `db_path`): Felder `listen` und `data_dir`, Standardwerte, unbekannte Felder werden abgelehnt, keine Geheimnisse.
|
||||
- `Error` (`Config`, `Io`, `Db`) und `Result`.
|
||||
- `VERSION`.
|
||||
|
||||
**Tests:** `cargo test -p nexus-core` (Einheitentests für Standardwerte, Fehlerfälle, Datei lesen).
|
||||
105
crates/nexus-core/src/config.rs
Normal file
105
crates/nexus-core/src/config.rs
Normal file
@ -0,0 +1,105 @@
|
||||
use std::net::SocketAddr;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::{Error, Result};
|
||||
|
||||
/// Konfiguration des Gateways. Enthält keine Geheimnisse.
|
||||
///
|
||||
/// Alle Felder haben Standardwerte; unbekannte Felder werden abgelehnt,
|
||||
/// damit Tippfehler auffallen.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
pub struct Config {
|
||||
/// Adresse, auf der das Gateway lauscht, zum Beispiel `127.0.0.1:8080`.
|
||||
pub listen: String,
|
||||
/// Ordner für Datenbank, Logs und Ergebnisse.
|
||||
pub data_dir: PathBuf,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
listen: "127.0.0.1:8080".to_string(),
|
||||
data_dir: PathBuf::from("data"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
/// Liest die Konfiguration aus TOML-Text und prüft sie.
|
||||
pub fn from_toml(text: &str) -> Result<Self> {
|
||||
let config: Config = toml::from_str(text).map_err(|e| Error::Config(e.to_string()))?;
|
||||
config.validate()?;
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
/// Liest die Konfiguration aus einer Datei.
|
||||
pub fn load(path: &Path) -> Result<Self> {
|
||||
let text = std::fs::read_to_string(path).map_err(|source| Error::Io {
|
||||
path: path.to_path_buf(),
|
||||
source,
|
||||
})?;
|
||||
Self::from_toml(&text)
|
||||
}
|
||||
|
||||
/// Prüft die Werte, die sich ohne Zugriff auf das System prüfen lassen.
|
||||
pub fn validate(&self) -> Result<()> {
|
||||
self.listen.parse::<SocketAddr>().map_err(|e| {
|
||||
Error::Config(format!("listen \"{}\" ist keine Adresse: {e}", self.listen))
|
||||
})?;
|
||||
if self.data_dir.as_os_str().is_empty() {
|
||||
return Err(Error::Config("data_dir darf nicht leer sein".to_string()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Pfad der SQLite-Datenbank.
|
||||
pub fn db_path(&self) -> PathBuf {
|
||||
self.data_dir.join("nexus.db")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn leere_konfiguration_nutzt_standardwerte() {
|
||||
let c = Config::from_toml("").unwrap();
|
||||
assert_eq!(c, Config::default());
|
||||
assert_eq!(c.listen, "127.0.0.1:8080");
|
||||
assert_eq!(c.db_path(), PathBuf::from("data/nexus.db"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn werte_werden_uebernommen() {
|
||||
let c = Config::from_toml("listen = \"0.0.0.0:9000\"\ndata_dir = \"/srv/nexus\"").unwrap();
|
||||
assert_eq!(c.listen, "0.0.0.0:9000");
|
||||
assert_eq!(c.db_path(), PathBuf::from("/srv/nexus/nexus.db"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unbekanntes_feld_wird_abgelehnt() {
|
||||
let err = Config::from_toml("lisen = \"127.0.0.1:1\"").unwrap_err();
|
||||
assert!(matches!(err, Error::Config(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ungueltige_adresse_wird_abgelehnt() {
|
||||
let err = Config::from_toml("listen = \"nirgendwo\"").unwrap_err();
|
||||
assert!(err.to_string().contains("keine Adresse"), "{err}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leeres_datenverzeichnis_wird_abgelehnt() {
|
||||
assert!(Config::from_toml("data_dir = \"\"").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fehlende_datei_ist_ein_io_fehler() {
|
||||
let err = Config::load(Path::new("/nicht/vorhanden/config.toml")).unwrap_err();
|
||||
assert!(matches!(err, Error::Io { .. }));
|
||||
}
|
||||
}
|
||||
23
crates/nexus-core/src/error.rs
Normal file
23
crates/nexus-core/src/error.rs
Normal file
@ -0,0 +1,23 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Fehler, die mehrere Nexus-Crates gemeinsam verwenden.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
/// Die Konfiguration fehlt, ist unlesbar oder ungültig.
|
||||
#[error("Konfiguration: {0}")]
|
||||
Config(String),
|
||||
|
||||
/// Ein Dateizugriff ist fehlgeschlagen.
|
||||
#[error("Dateizugriff auf {path}: {source}")]
|
||||
Io {
|
||||
path: PathBuf,
|
||||
#[source]
|
||||
source: std::io::Error,
|
||||
},
|
||||
|
||||
/// Ein Datenbankfehler (Text, damit dieses Crate ohne Datenbank auskommt).
|
||||
#[error("Datenbank: {0}")]
|
||||
Db(String),
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
12
crates/nexus-core/src/lib.rs
Normal file
12
crates/nexus-core/src/lib.rs
Normal file
@ -0,0 +1,12 @@
|
||||
//! Gemeinsame Typen, Fehler und Konfiguration für alle Nexus-Crates.
|
||||
//!
|
||||
//! Dieses Crate hat keine Abhängigkeit zu Datenbank oder Netzwerk.
|
||||
|
||||
mod config;
|
||||
mod error;
|
||||
|
||||
pub use config::Config;
|
||||
pub use error::{Error, Result};
|
||||
|
||||
/// Version des Nexus-Workspaces.
|
||||
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
Reference in New Issue
Block a user