Skip to content

Commit

Permalink
Prevent the Profile::specs invariant from being broken
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Oct 22, 2023
1 parent 8a89086 commit a056cad
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ pub(crate) struct LabelMaker<'a, R: rand::Rng> {
impl<'a, R: rand::Rng> LabelMaker<'a, R> {
pub(crate) async fn make(&mut self, profile: &Profile) -> Result<(), LabelMakerError> {
let mut res = Vec::new();
for s in &profile.specs {
for s in profile.specs() {
res.extend(self.labels.resolve(s)?);
}
for r in res {
Expand Down
16 changes: 14 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,20 @@ impl Config {

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct Profile {
pub(crate) name: String,
pub(crate) specs: Vec<LabelSpec>,
name: String,
// Invariant (enforced on creation by Config::get_profile): The various
// LabelSpecs do not step on each others' toes
specs: Vec<LabelSpec>,
}

impl Profile {
pub(crate) fn name(&self) -> &str {
&self.name
}

pub(crate) fn specs(&self) -> &[LabelSpec] {
&self.specs
}
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
Expand Down
5 changes: 3 additions & 2 deletions src/labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,9 @@ impl<'a> fmt::Display for LabelOperationMessage<'a> {
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct LabelSpec {
pub(crate) name: LabelName,
// Invariant: rename_from contains neither duplicates (*modulo* name
// casing) nor the same string as `name` (*modulo* case)
// Invariant (enforced on creation by Config::get_profile): rename_from
// contains neither duplicates (*modulo* name casing) nor the same string
// as `name` (*modulo* case)
pub(crate) rename_from: Vec<LabelName>,
pub(crate) options: LabelOptions,
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl Command {
};
let mut rng = rand::thread_rng();
for r in repos {
log::info!("Applying profile {:?} to repository {}", profile.name, r);
log::info!("Applying profile {:?} to repository {}", profile.name(), r);
let mut maker = client.get_label_maker(r, &mut rng, dry_run).await?;
maker.make(&profile).await?;
}
Expand Down

0 comments on commit a056cad

Please sign in to comment.