pub mod custom;
pub mod feature_grouped;
pub mod free_form;
pub mod kind_grouped;
pub mod schema_mirror;
use std::path::Path;
pub use custom::{Assertion, CustomProfile, PathPattern};
use super::finding::Finding;
use super::source_tree::SourceTree;
#[derive(Debug, Clone)]
pub enum Profile {
SchemaMirror,
KindGrouped,
FeatureGrouped,
FreeForm,
Custom(CustomProfile),
}
impl Profile {
pub fn from_name(name: &str) -> Result<Self, ProfileLoadError> {
match name {
"schema-mirror" => Ok(Self::SchemaMirror),
"kind-grouped" => Ok(Self::KindGrouped),
"feature-grouped" => Ok(Self::FeatureGrouped),
"free-form" => Ok(Self::FreeForm),
other => {
let path = Path::new(other);
let content = std::fs::read_to_string(path)
.map_err(|e| ProfileLoadError::Io(path.to_path_buf(), e.to_string()))?;
let custom: CustomProfile = toml::from_str(&content)
.map_err(|e| ProfileLoadError::Parse(path.to_path_buf(), e.to_string()))?;
Ok(Self::Custom(custom))
}
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum ProfileLoadError {
#[error("reading custom profile {0}: {1}")]
Io(std::path::PathBuf, String),
#[error("parsing custom profile {0}: {1}")]
Parse(std::path::PathBuf, String),
}
pub fn check_profile(profile: &Profile, tree: &SourceTree, schema_dir: &Path) -> Vec<Finding> {
match profile {
Profile::SchemaMirror => schema_mirror::check(tree, schema_dir),
Profile::KindGrouped => kind_grouped::check(tree, schema_dir),
Profile::FeatureGrouped => feature_grouped::check(tree, schema_dir),
Profile::FreeForm => free_form::check(tree, schema_dir),
Profile::Custom(c) => custom::check(c, tree, schema_dir),
}
}