use crate::rules::RuleEntry;
use std::path::PathBuf;
pub struct InstructionDoc {
pub path: PathBuf,
pub content: String,
}
pub trait InstructionProvider: Send + Sync {
fn id(&self) -> &str;
fn documents(&self) -> Vec<InstructionDoc>;
}
pub struct InlineInstructions {
label: PathBuf,
text: String,
}
impl InlineInstructions {
pub fn new(label: impl Into<PathBuf>, text: impl Into<String>) -> Self {
Self {
label: label.into(),
text: text.into(),
}
}
}
impl InstructionProvider for InlineInstructions {
fn id(&self) -> &str {
"inline"
}
fn documents(&self) -> Vec<InstructionDoc> {
if self.text.trim().is_empty() {
return Vec::new();
}
vec![InstructionDoc {
path: self.label.clone(),
content: self.text.clone(),
}]
}
}
pub trait RuleProvider: Send + Sync {
fn id(&self) -> &str;
fn rules(&self) -> Vec<RuleEntry>;
}
pub struct StaticRules {
id: String,
entries: Vec<RuleEntry>,
}
impl StaticRules {
pub fn new(id: impl Into<String>, entries: Vec<RuleEntry>) -> Self {
Self {
id: id.into(),
entries,
}
}
}
impl RuleProvider for StaticRules {
fn id(&self) -> &str {
&self.id
}
fn rules(&self) -> Vec<RuleEntry> {
self.entries.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_host_can_supply_instructions_without_a_file() {
let docs = InlineInstructions::new("service://conventions", "Be brief.").documents();
assert_eq!(docs.len(), 1);
assert_eq!(docs[0].content, "Be brief.");
assert!(InlineInstructions::new("x", " ").documents().is_empty());
}
#[test]
fn a_host_can_supply_a_rule_index_it_built_itself() {
let entries = vec![RuleEntry {
name: "testing".into(),
description: "How we test".into(),
path: PathBuf::from("/srv/rules/testing.md"),
}];
let source = StaticRules::new("service", entries.clone());
assert_eq!(source.id(), "service");
assert_eq!(source.rules(), entries);
}
}