Skip to content

Commit

Permalink
fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
crapStone authored and zonyitoo committed Jul 23, 2023
1 parent 371dc9a commit e505047
Showing 1 changed file with 54 additions and 47 deletions.
101 changes: 54 additions & 47 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,40 +88,35 @@ pub enum EscapePolicy {

impl EscapePolicy {
fn escape_basics(self) -> bool {
match self {
EscapePolicy::Nothing => false,
_ => true,
}
self != EscapePolicy::Nothing
}

fn escape_reserved(self) -> bool {
match self {
EscapePolicy::Reserved => true,
EscapePolicy::ReservedUnicode => true,
EscapePolicy::ReservedUnicodeExtended => true,
EscapePolicy::Everything => true,
_ => false,
}
matches!(
self,
EscapePolicy::Reserved
| EscapePolicy::ReservedUnicode
| EscapePolicy::ReservedUnicodeExtended
| EscapePolicy::Everything
)
}

fn escape_unicode(self) -> bool {
match self {
EscapePolicy::BasicsUnicode => true,
EscapePolicy::BasicsUnicodeExtended => true,
EscapePolicy::ReservedUnicode => true,
EscapePolicy::ReservedUnicodeExtended => true,
EscapePolicy::Everything => true,
_ => false,
}
matches!(
self,
EscapePolicy::BasicsUnicode
| EscapePolicy::BasicsUnicodeExtended
| EscapePolicy::ReservedUnicode
| EscapePolicy::ReservedUnicodeExtended
| EscapePolicy::Everything
)
}

fn escape_unicode_extended(self) -> bool {
match self {
EscapePolicy::BasicsUnicodeExtended => true,
EscapePolicy::ReservedUnicodeExtended => true,
EscapePolicy::Everything => true,
_ => false,
}
matches!(
self,
EscapePolicy::BasicsUnicodeExtended | EscapePolicy::ReservedUnicodeExtended | EscapePolicy::Everything
)
}

/// Given a character this returns true if it should be escaped as
Expand Down Expand Up @@ -450,7 +445,7 @@ impl Properties {
}

/// Remove the property with all values with the same key
pub fn remove_all<'a, S: AsRef<str>>(&'a mut self, s: S) -> impl DoubleEndedIterator<Item = String> + 'a {
pub fn remove_all<S: AsRef<str>>(&mut self, s: S) -> impl DoubleEndedIterator<Item = String> + '_ {
self.data.remove_all(property_get_key!(s.as_ref()))
}

Expand Down Expand Up @@ -813,8 +808,8 @@ impl Ini {

if let Some(props) = self.sections.get(&None) {
for (k, v) in props.iter() {
let k_str = escape_str(&k[..], opt.escape_policy);
let v_str = escape_str(&v[..], opt.escape_policy);
let k_str = escape_str(k, opt.escape_policy);
let v_str = escape_str(v, opt.escape_policy);
write!(writer, "{}={}{}", k_str, v_str, opt.line_separator)?;

firstline = false;
Expand All @@ -838,8 +833,8 @@ impl Ini {
)?;

for (k, v) in props.iter() {
let k_str = escape_str(&k[..], opt.escape_policy);
let v_str = escape_str(&v[..], opt.escape_policy);
let k_str = escape_str(k, opt.escape_policy);
let v_str = escape_str(v, opt.escape_policy);
write!(writer, "{}{}{}{}", k_str, opt.kv_separator, v_str, opt.line_separator)?;
}
}
Expand Down Expand Up @@ -928,10 +923,8 @@ impl Ini {
// Check if file starts with a BOM marker
// UTF-8: EF BB BF
let mut bom = [0u8; 3];
if let Ok(..) = reader.read_exact(&mut bom) {
if &bom == b"\xEF\xBB\xBF" {
with_bom = true;
}
if reader.read_exact(&mut bom).is_ok() && &bom == b"\xEF\xBB\xBF" {
with_bom = true;
}

if !with_bom {
Expand Down Expand Up @@ -1182,7 +1175,7 @@ impl<'a> Parser<'a> {
Err(e) => return Err(e),
},
'=' | ':' => {
if (&curkey[..]).is_empty() {
if (curkey[..]).is_empty() {
return self.error("missing key");
}
match self.parse_val() {
Expand Down Expand Up @@ -1571,7 +1564,7 @@ mod test {
..Default::default()
},
);
assert!(!ini.is_ok());
assert!(ini.is_err());

let err = ini.unwrap_err();
assert_eq!(err.line, 2);
Expand Down Expand Up @@ -2320,7 +2313,7 @@ c = d
let policy = EscapePolicy::Nothing;
assert_eq!(escape_str(test_str, policy), test_str);
}

#[test]
fn escape_str_basics() {
let test_backslash = r"\backslashes\";
Expand All @@ -2333,14 +2326,21 @@ c = d
assert_eq!(escape_str(test_controls, EscapePolicy::Nothing), test_controls);
assert_eq!(escape_str(test_whitespace, EscapePolicy::Nothing), test_whitespace);

for policy in vec![
EscapePolicy::Basics, EscapePolicy::BasicsUnicode, EscapePolicy::BasicsUnicodeExtended,
EscapePolicy::Reserved, EscapePolicy::ReservedUnicode, EscapePolicy::ReservedUnicodeExtended,
for policy in [
EscapePolicy::Basics,
EscapePolicy::BasicsUnicode,
EscapePolicy::BasicsUnicodeExtended,
EscapePolicy::Reserved,
EscapePolicy::ReservedUnicode,
EscapePolicy::ReservedUnicodeExtended,
EscapePolicy::Everything,
] {
assert_eq!(escape_str(test_backslash, policy), r"\\backslashes\\");
assert_eq!(escape_str(test_nul, policy), r"string with \0nulls\0 in it");
assert_eq!(escape_str(test_controls, policy), r"|\a| bell, |\b| backspace, |\x007f| delete, |\x001b| escape");
assert_eq!(
escape_str(test_controls, policy),
r"|\a| bell, |\b| backspace, |\x007f| delete, |\x001b| escape"
);
assert_eq!(escape_str(test_whitespace, policy), r"\t \r\n");
}
}
Expand All @@ -2353,17 +2353,21 @@ c = d
let test_punctuation = "!@$%^&*()-_+/?.>,<[]{}``";

// These policies should *not* escape reserved characters.
for policy in vec![
for policy in [
EscapePolicy::Nothing,
EscapePolicy::Basics, EscapePolicy::BasicsUnicode, EscapePolicy::BasicsUnicodeExtended,
EscapePolicy::Basics,
EscapePolicy::BasicsUnicode,
EscapePolicy::BasicsUnicodeExtended,
] {
assert_eq!(escape_str(test_reserved, policy), ":=;#");
assert_eq!(escape_str(test_punctuation, policy), test_punctuation);
}

// These should.
for policy in vec![
EscapePolicy::Reserved, EscapePolicy::ReservedUnicodeExtended, EscapePolicy::ReservedUnicode,
for policy in [
EscapePolicy::Reserved,
EscapePolicy::ReservedUnicodeExtended,
EscapePolicy::ReservedUnicode,
EscapePolicy::Everything,
] {
assert_eq!(escape_str(test_reserved, policy), r"\:\=\;\#");
Expand All @@ -2389,15 +2393,18 @@ c = d

// The "Unicode" policies should escape standard BMP unicode, but should *not* escape emoji or supplementary CJK codepoints.
// The Basics/Reserved policies should behave identically in this regard.
for policy in vec![EscapePolicy::BasicsUnicode, EscapePolicy::ReservedUnicode] {
for policy in [EscapePolicy::BasicsUnicode, EscapePolicy::ReservedUnicode] {
assert_eq!(escape_str(test_unicode, policy), r"\x00e9\x00a3\x2233\x5b57\x2728");
assert_eq!(escape_str(test_emoji, policy), test_emoji);
assert_eq!(escape_str(test_cjk, policy), test_cjk);
assert_eq!(escape_str(test_high_points, policy), test_high_points);
}

// UnicodeExtended policies should escape both BMP and supplementary plane characters.
for policy in vec![EscapePolicy::BasicsUnicodeExtended, EscapePolicy::ReservedUnicodeExtended] {
for policy in [
EscapePolicy::BasicsUnicodeExtended,
EscapePolicy::ReservedUnicodeExtended,
] {
assert_eq!(escape_str(test_unicode, policy), r"\x00e9\x00a3\x2233\x5b57\x2728");
assert_eq!(escape_str(test_emoji, policy), r"\x1f431\x1f609");
assert_eq!(escape_str(test_cjk, policy), r"\x2020c\x20547");
Expand Down

0 comments on commit e505047

Please sign in to comment.