Skip to content

Commit

Permalink
Add a new Ruleset type, in preparation for #51
Browse files Browse the repository at this point in the history
  • Loading branch information
bradlarsen committed Oct 26, 2023
1 parent 81fbcf5 commit 0b72b06
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
3 changes: 3 additions & 0 deletions crates/noseyparker-rules/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
mod rule;
mod rules;
mod ruleset;
mod util;

pub use rule::Rule;
pub use rules::Rules;
pub use ruleset::Ruleset;

// -------------------------------------------------------------------------------------------------
// test
Expand Down
2 changes: 1 addition & 1 deletion crates/noseyparker-rules/src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use regex::Regex;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// A pattern-based rule as represented syntactically.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Rule {
/// The human-readable name of the rule
pub name: String,
Expand Down
13 changes: 4 additions & 9 deletions crates/noseyparker-rules/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ use anyhow::{bail, Context, Result};
use ignore::types::TypesBuilder;
use ignore::WalkBuilder;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::BufReader;
use std::path::Path;
use tracing::{debug, debug_span};

use crate::Rule;
use crate::{util, Rule};

// -------------------------------------------------------------------------------------------------
// Rules
Expand All @@ -22,7 +20,7 @@ impl Rules {
let mut rules = Rules { rules: Vec::new() };
for (path, contents) in iterable.into_iter() {
let rs: Self = serde_yaml::from_reader(contents)
.with_context(|| format!("Failed to load YAML from {}", path.display()))?;
.with_context(|| format!("Failed to load rules YAML from {}", path.display()))?;
rules.extend(rs);
}

Expand Down Expand Up @@ -57,11 +55,8 @@ impl Rules {
pub fn from_yaml_file<P: AsRef<Path>>(path: P) -> Result<Self> {
let path = path.as_ref();
let _span = debug_span!("Rules::from_yaml_file", "{}", path.display()).entered();
let infile =
File::open(path).with_context(|| format!("Failed to read rules from {}", path.display()))?;
let reader = BufReader::new(infile);
let rules: Self = serde_yaml::from_reader(reader)
.with_context(|| format!("Failed to load YAML from {}", path.display()))?;
let rules: Self = util::load_yaml_file(path)
.with_context(|| format!("Failed to load rules YAML from {}", path.display()))?;
debug!("Loaded {} rules from {}", rules.len(), path.display());
Ok(rules)
}
Expand Down
33 changes: 33 additions & 0 deletions crates/noseyparker-rules/src/ruleset.rs
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()
}
}
15 changes: 15 additions & 0 deletions crates/noseyparker-rules/src/util.rs
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)
}

0 comments on commit 0b72b06

Please sign in to comment.