knowledge_base_crud/
repository.rs1use crate::{read::ReadRepository, write::WriteRepository};
2use knowledge_base_validation::{Diagnostic, KnowledgeBaseValidator, validate_repository_with};
3use std::fmt;
4use std::path::{Path, PathBuf};
5use std::sync::Arc;
6
7#[derive(Clone)]
9pub struct KnowledgeBaseRepository {
10 root: PathBuf,
11 validators: Vec<Arc<dyn KnowledgeBaseValidator>>,
12}
13
14impl fmt::Debug for KnowledgeBaseRepository {
15 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
16 formatter
17 .debug_struct("KnowledgeBaseRepository")
18 .field("root", &self.root)
19 .field("validator_count", &self.validators.len())
20 .finish()
21 }
22}
23
24impl KnowledgeBaseRepository {
25 pub fn new(root: impl Into<PathBuf>) -> Self {
26 Self::with_validators(root, [])
27 }
28
29 pub fn with_validators(root: impl Into<PathBuf>, validators: impl IntoIterator<Item = Arc<dyn KnowledgeBaseValidator>>) -> Self {
30 Self {
31 root: root.into(),
32 validators: validators.into_iter().collect(),
33 }
34 }
35
36 pub fn root(&self) -> &Path {
37 &self.root
38 }
39
40 pub fn validate(&self) -> Vec<Diagnostic> {
42 validate_repository_with(&self.root, self.validators.iter().map(AsRef::as_ref))
43 }
44
45 pub fn read(&self) -> ReadRepository<'_> {
46 ReadRepository::new(self)
47 }
48
49 pub fn write(&self) -> WriteRepository<'_> {
50 WriteRepository::new(self)
51 }
52
53 pub(crate) fn validators(&self) -> &[Arc<dyn KnowledgeBaseValidator>] {
54 &self.validators
55 }
56}