-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new
Ruleset
type, in preparation for #51
- Loading branch information
1 parent
81fbcf5
commit 0b72b06
Showing
5 changed files
with
56 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use anyhow::{Context, Result}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::path::Path; | ||
use tracing::{debug, debug_span}; | ||
|
||
use crate::util; | ||
|
||
/// A syntactic representation describing a set of Nosey Parker rules. | ||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
pub struct Ruleset { | ||
/// A description of this ruleset | ||
pub description: String, | ||
|
||
/// A list of rule IDs to include in this set | ||
pub include_ids: Vec<String>, | ||
} | ||
|
||
impl Ruleset { | ||
/// Load a ruleset from the given YAML file. | ||
pub fn from_yaml_file<P: AsRef<Path>>(path: P) -> Result<Self> { | ||
let path = path.as_ref(); | ||
let _span = debug_span!("Ruleset::from_yaml_file", "{}", path.display()).entered(); | ||
let ruleset: Self = util::load_yaml_file(path) | ||
.with_context(|| format!("Failed to load ruleset YAML from {}", path.display()))?; | ||
debug!("Loaded ruleset of {} rules from {}", ruleset.num_rules(), path.display()); | ||
Ok(ruleset) | ||
} | ||
|
||
/// How many rules are listed in this ruleset? | ||
pub fn num_rules(&self) -> usize { | ||
self.include_ids.len() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use anyhow::Result; | ||
use serde::de::DeserializeOwned; | ||
use serde_yaml; | ||
use std::fs::File; | ||
use std::io::BufReader; | ||
use std::path::Path; | ||
|
||
/// Load a value from a YAML file. | ||
pub fn load_yaml_file<T: DeserializeOwned, P: AsRef<Path>>(path: P) -> Result<T> { | ||
let path = path.as_ref(); | ||
let infile = File::open(path)?; | ||
let reader = BufReader::new(infile); | ||
let result = serde_yaml::from_reader(reader)?; | ||
Ok(result) | ||
} |