Initial pile-config
This commit is contained in:
104
crates/pile-config/src/lib.rs
Normal file
104
crates/pile-config/src/lib.rs
Normal file
@@ -0,0 +1,104 @@
|
||||
use serde::Deserialize;
|
||||
use std::{collections::HashMap, fmt::Debug, path::PathBuf, slice};
|
||||
|
||||
pub static INIT_DB_TOML: &str = include_str!("./config.toml");
|
||||
|
||||
mod post;
|
||||
pub use post::*;
|
||||
|
||||
#[test]
|
||||
fn init_db_toml_valid() {
|
||||
toml::from_str::<ConfigToml>(INIT_DB_TOML).unwrap();
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum OneOrMany<T: Debug + Clone> {
|
||||
One(T),
|
||||
Many(Vec<T>),
|
||||
}
|
||||
|
||||
impl<T: Debug + Clone> OneOrMany<T> {
|
||||
pub fn to_vec(self) -> Vec<T> {
|
||||
match self {
|
||||
Self::One(x) => vec![x],
|
||||
Self::Many(x) => x,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_slice(&self) -> &[T] {
|
||||
match self {
|
||||
Self::One(x) => slice::from_ref(&x),
|
||||
Self::Many(x) => &x[..],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct ConfigToml {
|
||||
pub dataset: DatasetConfig,
|
||||
pub schema: HashMap<String, FieldSpec>,
|
||||
pub fts: Option<DatasetFts>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct DatasetConfig {
|
||||
/// Must be unique
|
||||
pub name: String,
|
||||
|
||||
/// Root dir for indices
|
||||
pub working_dir: Option<PathBuf>,
|
||||
|
||||
/// Where to find this field
|
||||
pub source: HashMap<String, Source>,
|
||||
|
||||
/// How to post-process this field
|
||||
#[serde(default)]
|
||||
pub post: Vec<FieldSpecPost>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Source {
|
||||
/// A directory of FLAC files
|
||||
Flac { path: OneOrMany<PathBuf> },
|
||||
}
|
||||
|
||||
//
|
||||
// MARK: schema
|
||||
//
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct FieldSpec {
|
||||
/// The type of this field
|
||||
pub r#type: FieldType,
|
||||
|
||||
/// How to find this field in a data entry
|
||||
pub path: OneOrMany<String>,
|
||||
|
||||
/// How to post-process this field
|
||||
#[serde(default)]
|
||||
pub post: Vec<FieldSpecPost>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Deserialize, PartialEq, Eq)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum FieldType {
|
||||
Text,
|
||||
}
|
||||
|
||||
//
|
||||
// MARK: fts
|
||||
//
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Default)]
|
||||
pub struct DatasetFts {
|
||||
#[serde(alias = "field")]
|
||||
pub fields: HashMap<String, FtsIndexField>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct FtsIndexField {
|
||||
pub tokenize: bool,
|
||||
}
|
||||
Reference in New Issue
Block a user