use std::path::PathBuf;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AppendFsync {
Always,
EverySec,
No,
}
impl AppendFsync {
pub fn as_str(&self) -> &'static str {
match self {
Self::Always => "always",
Self::EverySec => "everysec",
Self::No => "no",
}
}
pub fn parse(s: &str) -> Option<Self> {
match s.to_ascii_lowercase().as_str() {
"always" => Some(Self::Always),
"everysec" => Some(Self::EverySec),
"no" => Some(Self::No),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EvictionPolicy {
NoEviction,
AllKeysLru,
AllKeysLfu,
AllKeysRandom,
VolatileLru,
VolatileLfu,
VolatileRandom,
VolatileTtl,
}
impl EvictionPolicy {
pub fn as_str(&self) -> &'static str {
match self {
Self::NoEviction => "noeviction",
Self::AllKeysLru => "allkeys-lru",
Self::AllKeysLfu => "allkeys-lfu",
Self::AllKeysRandom => "allkeys-random",
Self::VolatileLru => "volatile-lru",
Self::VolatileLfu => "volatile-lfu",
Self::VolatileRandom => "volatile-random",
Self::VolatileTtl => "volatile-ttl",
}
}
pub fn parse(s: &str) -> Option<Self> {
match s.to_ascii_lowercase().as_str() {
"noeviction" => Some(Self::NoEviction),
"allkeys-lru" => Some(Self::AllKeysLru),
"allkeys-lfu" => Some(Self::AllKeysLfu),
"allkeys-random" => Some(Self::AllKeysRandom),
"volatile-lru" => Some(Self::VolatileLru),
"volatile-lfu" => Some(Self::VolatileLfu),
"volatile-random" => Some(Self::VolatileRandom),
"volatile-ttl" => Some(Self::VolatileTtl),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogLevel {
Trace,
Debug,
Info,
Warn,
Error,
}
impl LogLevel {
pub fn as_str(&self) -> &'static str {
match self {
Self::Trace => "trace",
Self::Debug => "debug",
Self::Info => "info",
Self::Warn => "warning",
Self::Error => "error",
}
}
pub fn parse(s: &str) -> Option<Self> {
match s.to_ascii_lowercase().as_str() {
"trace" => Some(Self::Trace),
"debug" => Some(Self::Debug),
"info" => Some(Self::Info),
"warn" | "warning" => Some(Self::Warn),
"error" => Some(Self::Error),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LogOutput {
Stderr,
Stdout,
File(PathBuf),
}
impl LogOutput {
pub fn as_str(&self) -> std::borrow::Cow<'_, str> {
match self {
Self::Stderr => "stderr".into(),
Self::Stdout => "stdout".into(),
Self::File(p) => p.display().to_string().into(),
}
}
pub fn parse(s: &str) -> Self {
match s {
"stderr" => Self::Stderr,
"stdout" => Self::Stdout,
path => Self::File(PathBuf::from(path)),
}
}
}
#[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, Copy, PartialEq, Eq)]
pub struct AdvancedSection {
pub spin_limit: u32,
pub park_timeout_ms: u32,
pub tick_check_every: u32,
pub ring_capacity: usize,
}
impl Default for AdvancedSection {
fn default() -> Self {
Self {
spin_limit: 256,
park_timeout_ms: 50,
tick_check_every: 256,
ring_capacity: 1024,
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct NotificationSection {
pub notify_keyspace_events: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct NotificationFlags {
pub keyspace: bool,
pub keyevent: bool,
pub generic: bool,
pub string: bool,
pub list: bool,
pub set: bool,
pub hash: bool,
pub zset: bool,
pub stream: bool,
}
impl NotificationFlags {
pub fn is_empty(&self) -> bool {
!(self.keyspace || self.keyevent)
|| !(self.generic
|| self.string
|| self.list
|| self.set
|| self.hash
|| self.zset
|| self.stream)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SlowlogSection {
pub slower_than_micros: i64,
pub max_len: u32,
}
impl Default for SlowlogSection {
fn default() -> Self {
Self {
slower_than_micros: -1,
max_len: 128,
}
}
}
pub fn parse_notification_flags(s: &str) -> NotificationFlags {
let mut f = NotificationFlags::default();
for c in s.chars() {
match c {
'K' => f.keyspace = true,
'E' => f.keyevent = true,
'g' => f.generic = true,
'$' => f.string = true,
'l' => f.list = true,
's' => f.set = true,
'h' => f.hash = true,
'z' => f.zset = true,
't' => f.stream = true,
'A' => {
f.generic = true;
f.string = true;
f.list = true;
f.set = true;
f.hash = true;
f.zset = true;
f.stream = true;
}
_ => {} }
}
f
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct ClusterSection {
pub enabled: bool,
pub port_base: u16,
}
#[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 notification: NotificationSection,
pub advanced: AdvancedSection,
pub slowlog: SlowlogSection,
pub cluster: ClusterSection,
pub source_path: Option<PathBuf>,
}
pub use crate::error::ConfigError;