use std::path::PathBuf;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AppendFsync {
Always,
EverySec,
No,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EvictionPolicy {
NoEviction,
AllKeysLru,
AllKeysLfu,
AllKeysRandom,
VolatileLru,
VolatileLfu,
VolatileRandom,
VolatileTtl,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogLevel {
Trace,
Debug,
Info,
Warn,
Error,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LogOutput {
Stderr,
Stdout,
File(PathBuf),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ServerSection {
pub bind: [u8; 4],
pub port: u16,
pub threads: usize,
pub data_dir: PathBuf,
}
impl Default for ServerSection {
fn default() -> Self {
Self {
bind: [127, 0, 0, 1],
port: 6004,
threads: 0,
data_dir: PathBuf::from("."),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PersistenceSection {
pub aof: bool,
pub appendfsync: AppendFsync,
pub auto_aof_rewrite_percentage: u32,
pub auto_aof_rewrite_min_size: u64,
}
impl Default for PersistenceSection {
fn default() -> Self {
Self {
aof: true,
appendfsync: AppendFsync::EverySec,
auto_aof_rewrite_percentage: 100,
auto_aof_rewrite_min_size: 64 * 1024 * 1024,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MemorySection {
pub maxmemory: u64,
pub maxmemory_policy: EvictionPolicy,
}
impl Default for MemorySection {
fn default() -> Self {
Self {
maxmemory: 0,
maxmemory_policy: EvictionPolicy::NoEviction,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ExpirySection {
pub hz: u32,
pub sample: u32,
}
impl Default for ExpirySection {
fn default() -> Self {
Self { hz: 10, sample: 20 }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LogSection {
pub level: LogLevel,
pub output: LogOutput,
}
impl Default for LogSection {
fn default() -> Self {
Self {
level: LogLevel::Info,
output: LogOutput::Stderr,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Config {
pub server: ServerSection,
pub persistence: PersistenceSection,
pub memory: MemorySection,
pub expiry: ExpirySection,
pub log: LogSection,
pub source_path: Option<PathBuf>,
}
#[derive(Debug)]
pub enum ConfigError {
IoOpen {
path: PathBuf,
err: String,
},
Parse {
line: usize,
col: usize,
msg: String,
},
Schema {
line: usize,
field: String,
msg: String,
},
}
impl std::fmt::Display for ConfigError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::IoOpen { path, err } => {
write!(f, "kevy-config: cannot read {}: {err}", path.display())
}
Self::Parse { line, col, msg } => {
write!(f, "kevy-config: parse error at line {line} col {col}: {msg}")
}
Self::Schema { line, field, msg } => {
write!(f, "kevy-config: schema error at line {line} on {field}: {msg}")
}
}
}
}
impl std::error::Error for ConfigError {}