Skip to content

Commit

Permalink
fix: determinate mapping (#172)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun authored Feb 16, 2025
1 parent ad3ce3b commit 518ea91
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
18 changes: 17 additions & 1 deletion fmt/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl FeatureGate {
}
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone)]
pub enum Mapping {
Filename {
pattern: String,
Expand All @@ -120,6 +120,22 @@ pub enum Mapping {
},
}

impl PartialEq for Mapping {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Mapping::Filename { pattern: p1, .. }, Mapping::Filename { pattern: p2, .. }) => {
p1 == p2
}
(Mapping::Extension { pattern: p1, .. }, Mapping::Extension { pattern: p2, .. }) => {
p1 == p2
}
_ => false,
}
}
}

impl Eq for Mapping {}

impl Hash for Mapping {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write(match self {
Expand Down
29 changes: 21 additions & 8 deletions fmt/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fs;
use std::path::Path;
Expand Down Expand Up @@ -84,9 +85,11 @@ pub fn check_license_header<C: Callback>(
if config.use_default_mapping {
let default_mapping = default_mapping();
for m in default_mapping {
if !mapping.contains(&m) {
mapping.insert(m);
if let Some(o) = mapping.get(&m) {
log::warn!("default mapping {m:?} is override by {o:?}");
continue;
}
mapping.insert(m);
}
}
mapping
Expand All @@ -95,20 +98,30 @@ pub fn check_license_header<C: Callback>(
let definitions = {
let mut defs = HashMap::new();
for (k, v) in default_headers() {
if defs.contains_key(&k) {
anyhow::bail!("Header definition {k} is defined more than once");
match defs.entry(k) {
Entry::Occupied(mut ent) => {
log::warn!("Default header {} is override", ent.key());
ent.insert(v);
}
Entry::Vacant(ent) => {
ent.insert(v);
}
}
defs.insert(k, v);
}
for additional_header in &config.additional_headers {
let additional_defs = fs::read_to_string(additional_header)
.with_context(|| format!("cannot load header definitions: {additional_header}"))
.and_then(deserialize_header_definitions)?;
for (k, v) in additional_defs {
if defs.contains_key(&k) {
anyhow::bail!("Header definition {k} is defined more than once");
match defs.entry(k) {
Entry::Occupied(mut ent) => {
log::warn!("Additional header {} is override", ent.key());
ent.insert(v);
}
Entry::Vacant(ent) => {
ent.insert(v);
}
}
defs.insert(k, v);
}
}
defs
Expand Down

0 comments on commit 518ea91

Please sign in to comment.