-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
push: Add methods to get and insert push rules in a Ruleset
- Loading branch information
Showing
2 changed files
with
125 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use std::hash::Hash; | ||
|
||
use indexmap::{Equivalent, IndexSet}; | ||
use thiserror::Error; | ||
|
||
/// The error type returned when trying to move a push rule before or after a push rule with a given | ||
/// rule ID that could not be found. | ||
#[derive(Debug, Error)] | ||
#[non_exhaustive] | ||
#[error("Unknown rule ID")] | ||
pub struct UnknownRuleIdError; | ||
|
||
pub fn replace_and_move<T>( | ||
set: &mut IndexSet<T>, | ||
rule: T, | ||
move_rule_id: &str, | ||
move_after: bool, | ||
) -> Result<Option<T>, UnknownRuleIdError> | ||
where | ||
T: Hash + Eq, | ||
str: Equivalent<T>, | ||
{ | ||
let (from, replaced) = set.replace_full(rule); | ||
let mut to = set.get_index_of(move_rule_id).ok_or(UnknownRuleIdError)?; | ||
|
||
if move_after { | ||
to += 1; | ||
} | ||
|
||
set.move_index(from, to); | ||
|
||
Ok(replaced) | ||
} |