Skip to content

Commit

Permalink
automatic_edits: Suggest parent scopes for allow_api
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlattimore committed Sep 19, 2023
1 parent c437be0 commit 7a7567a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
50 changes: 28 additions & 22 deletions src/config/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,39 +276,25 @@ impl PermSel {
}

pub(crate) fn parent(&self) -> Option<PermSel> {
Some(self.clone_with_scope(self.parent_scope()?))
}

pub(crate) fn parent_scope(&self) -> Option<PermissionScope> {
match self.scope {
PermissionScope::All => None,
PermissionScope::Build => Some(PermissionScope::FromBuild),
PermissionScope::Test => Some(PermissionScope::FromTest),
PermissionScope::FromBuild => Some(PermissionScope::All),
PermissionScope::FromTest => Some(PermissionScope::All),
}
}

fn child_scopes(&self) -> &'static [PermissionScope] {
match self.scope {
PermissionScope::All => &[PermissionScope::FromBuild, PermissionScope::FromTest],
PermissionScope::Build => &[],
PermissionScope::Test => &[],
PermissionScope::FromBuild => &[PermissionScope::Build],
PermissionScope::FromTest => &[PermissionScope::Test],
}
Some(self.clone_with_scope(self.scope.parent_scope()?))
}

/// Returns all selectors that inherit from this one.
pub(crate) fn descendants(&self) -> Vec<PermSel> {
let mut scopes: Vec<PermSel> = self
.scope
.child_scopes()
.iter()
.map(|s| self.clone_with_scope(*s))
.collect();
let mut next_level: Vec<PermSel> = scopes
.iter()
.flat_map(|sel| sel.child_scopes().iter().map(|s| self.clone_with_scope(*s)))
.flat_map(|sel| {
sel.scope
.child_scopes()
.iter()
.map(|s| self.clone_with_scope(*s))
})
.collect();
scopes.append(&mut next_level);
scopes
Expand Down Expand Up @@ -352,6 +338,26 @@ impl PermissionScope {
}
}
}

pub(crate) fn parent_scope(self) -> Option<PermissionScope> {
match self {
PermissionScope::All => None,
PermissionScope::Build => Some(PermissionScope::FromBuild),
PermissionScope::Test => Some(PermissionScope::FromTest),
PermissionScope::FromBuild => Some(PermissionScope::All),
PermissionScope::FromTest => Some(PermissionScope::All),
}
}

fn child_scopes(self) -> &'static [PermissionScope] {
match self {
PermissionScope::All => &[PermissionScope::FromBuild, PermissionScope::FromTest],
PermissionScope::Build => &[],
PermissionScope::Test => &[],
PermissionScope::FromBuild => &[PermissionScope::Build],
PermissionScope::FromTest => &[PermissionScope::Test],
}
}
}

/// A manual implementation of Serialize for PermSel so that we can use it as keys in a hashmap that
Expand Down
24 changes: 18 additions & 6 deletions src/config_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ pub(crate) fn fixes_for_problem(problem: &Problem, config: &Config) -> Vec<Box<d
edits.push(Box::new(IgnoreApi(available.clone())));
}
Problem::DisallowedApiUsage(usage) => {
edits.push(Box::new(AllowApiUsage {
usage: usage.clone(),
}));
usage.add_allow_api_fixes(&mut edits);
let _ = usage.add_exclude_fixes(&mut edits, config);
}
Problem::IsProcMacro(pkg_id) => {
Expand Down Expand Up @@ -130,9 +128,7 @@ pub(crate) fn fixes_for_problem(problem: &Problem, config: &Config) -> Vec<Box<d
// have shown up elsewhere and it seems nicer to just degrade to not show those edits.
let _ = info.usages.add_include_fixes(&mut edits, config);
let _ = info.usages.add_exclude_fixes(&mut edits, config);
edits.push(Box::new(AllowApiUsage {
usage: info.usages.clone(),
}))
info.usages.add_allow_api_fixes(&mut edits);
}
_ => {}
}
Expand Down Expand Up @@ -342,6 +338,22 @@ impl ApiUsages {
}
Ok(())
}

fn add_allow_api_fixes(&self, edits: &mut Vec<Box<dyn Edit>>) {
edits.push(Box::new(AllowApiUsage {
usage: self.clone(),
}));
let mut scope = self.scope;
while let Some(parent_scope) = scope.parent_scope() {
edits.push(Box::new(AllowApiUsage {
usage: ApiUsages {
scope: parent_scope,
..self.clone()
},
}));
scope = parent_scope;
}
}
}

fn pkg_path(perm_sel: &PermSel) -> impl Iterator<Item = &str> + Clone {
Expand Down

0 comments on commit 7a7567a

Please sign in to comment.