use std::fmt;
use std::str::FromStr;
use std::time::Duration;
use surrealdb_cnf as cnf;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum LiveQueryEngine {
#[default]
Inline,
Router,
}
impl fmt::Display for LiveQueryEngine {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Inline => f.write_str("inline"),
Self::Router => f.write_str("router"),
}
}
}
impl FromStr for LiveQueryEngine {
type Err = String;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"inline" => Ok(Self::Inline),
"router" => Ok(Self::Router),
v => Err(format!("Invalid live query engine: '{v}'. Expected 'inline' or 'router'")),
}
}
}
#[derive(Clone, Debug)]
pub(crate) struct DatastoreConfig {
pub datastore_cache_size: usize,
pub export_batch_size: u32,
pub live_query_engine: LiveQueryEngine,
pub live_query_retention: Duration,
}
impl Default for DatastoreConfig {
fn default() -> Self {
Self {
datastore_cache_size: 1_000,
export_batch_size: 1000,
live_query_engine: LiveQueryEngine::Inline,
live_query_retention: Duration::from_secs(3600),
}
}
}
impl cnf::Config for DatastoreConfig {
fn parse(&mut self, map: &cnf::ConfigMap) {
map.parse_key("datastore_cache_size", &mut self.datastore_cache_size)
.parse_key("export_batch_size", &mut self.export_batch_size)
.parse_key("live_query_engine", &mut self.live_query_engine)
.parse_key_with("live_query_retention", &mut self.live_query_retention, |x| {
cnf::parse_duration(x).ok()
});
}
}