use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use serde::Serialize;
#[derive(Debug, Clone, Serialize)]
pub struct Rule {
pub pattern: String,
pub handler: String,
pub priority: i32,
#[serde(default, skip_serializing_if = "is_false")]
pub case_insensitive: bool,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
pub options: HashMap<String, String>,
}
fn is_false(b: &bool) -> bool {
!*b
}
#[derive(Debug, Clone)]
pub struct PackEntry {
pub relative_path: PathBuf,
pub absolute_path: PathBuf,
pub is_dir: bool,
pub gate_failure: Option<GateFailure>,
}
#[derive(Debug, Clone)]
pub struct GateFailure {
pub label: String,
pub predicate: String,
pub host: String,
}
#[derive(Debug, Clone, Serialize)]
pub struct RuleMatch {
pub relative_path: PathBuf,
pub absolute_path: PathBuf,
pub pack: String,
pub handler: String,
pub is_dir: bool,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
pub options: HashMap<String, String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub preprocessor_source: Option<PathBuf>,
#[serde(skip)]
pub rendered_bytes: Option<Arc<[u8]>>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rule_match_serializes() {
let m = RuleMatch {
relative_path: "vimrc".into(),
absolute_path: "/dots/vim/vimrc".into(),
pack: "vim".into(),
handler: "symlink".into(),
is_dir: false,
options: HashMap::new(),
preprocessor_source: None,
rendered_bytes: None,
};
let json = serde_json::to_string(&m).unwrap();
assert!(json.contains("vimrc"));
assert!(json.contains("symlink"));
assert!(!json.contains("options"));
}
}