Skip to content

Commit

Permalink
Support argument renaming during deriving on domtrans rules
Browse files Browse the repository at this point in the history
  • Loading branch information
dburgener committed Jan 6, 2023
1 parent c9dc744 commit f06bd61
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions src/internal_rep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,18 @@ pub fn argument_to_typeinfo_vec<'a>(
Ok(ret)
}

fn rename_cow<'a>(
cow_str: &CascadeString,
renames: &BTreeMap<String, String>,
) -> Cow<'a, CascadeString> {
Cow::Owned(CascadeString::from(
renames
.get::<str>(cow_str.as_ref())
.unwrap_or(&cow_str.to_string())
.clone(),
))
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum AvRuleFlavor {
Allow,
Expand All @@ -792,18 +804,6 @@ pub struct AvRule<'a> {

impl AvRule<'_> {
fn get_renamed_statement(&self, renames: &BTreeMap<String, String>) -> Self {
fn rename_cow<'a>(
cow_str: &CascadeString,
renames: &BTreeMap<String, String>,
) -> Cow<'a, CascadeString> {
Cow::Owned(CascadeString::from(
renames
.get::<str>(cow_str.as_ref())
.unwrap_or(&cow_str.to_string())
.clone(),
))
}

AvRule {
av_rule_flavor: self.av_rule_flavor,
source: rename_cow(&self.source, renames),
Expand Down Expand Up @@ -890,6 +890,8 @@ impl<'a> Context<'a> {
}

fn get_renamed_context(&self, renames: &BTreeMap<String, String>) -> Self {
// The global rename_cow works on CascadeStrings. In this local case we work on &strs
// instead
fn rename_cow<'a>(cow_str: &str, renames: &BTreeMap<String, String>) -> Cow<'a, str> {
let new_str: &str = cow_str.borrow();
Cow::Owned(renames.get(new_str).unwrap_or(&new_str.to_string()).clone())
Expand Down Expand Up @@ -1562,6 +1564,16 @@ impl From<&DomtransRule<'_>> for sexp::Sexp {
}
}

impl DomtransRule<'_> {
fn get_renamed_statement(&self, renames: &BTreeMap<String, String>) -> Self {
DomtransRule {
source: rename_cow(&self.source, renames),
target: rename_cow(&self.target, renames),
executable: rename_cow(&self.executable, renames),
}
}
}

fn call_to_domain_transition<'a>(
c: &'a FuncCall,
types: &'a TypeMap,
Expand Down Expand Up @@ -2275,9 +2287,9 @@ impl<'a> ValidatedStatement<'a> {
ValidatedStatement::FcRule(f) => {
ValidatedStatement::FcRule(f.get_renamed_statement(renames))
}
// DomtransRule is probably broken on derive anyways. It uses TypeInfos directly rather
// than strings. This probably means that deriving a DomTrans using "this" is broken
ValidatedStatement::DomtransRule(_) => self.clone(),
ValidatedStatement::DomtransRule(d) => {
ValidatedStatement::DomtransRule(d.get_renamed_statement(renames))
}
}
}
}
Expand Down Expand Up @@ -3388,7 +3400,13 @@ mod tests {
context: Context::new(false, None, None, Cow::Borrowed("old_name"), None, None),
});

for statement in [statement1, statement2, statement3] {
let statement4 = ValidatedStatement::DomtransRule(DomtransRule {
source: Cow::Owned(CascadeString::from("old_name")),
target: Cow::Owned(CascadeString::from("old_name")),
executable: Cow::Owned(CascadeString::from("old_name")),
});

for statement in [statement1, statement2, statement3, statement4] {
let mut renames = BTreeMap::new();
renames.insert("old_name".to_string(), "new_name".to_string());
let renamed_statement = statement.get_renamed_statement(&renames);
Expand Down

0 comments on commit f06bd61

Please sign in to comment.