Skip to content

Commit

Permalink
Merge pull request #3 from SeriousBug/master
Browse files Browse the repository at this point in the history
implement `is_sanitized` function
  • Loading branch information
kardeiz authored May 7, 2022
2 parents f515874 + 7a42377 commit c3d7196
Showing 1 changed file with 107 additions and 1 deletion.
108 changes: 107 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,55 @@ pub fn sanitize_with_options<S: AsRef<str>>(name: S, options: Options) -> String

}

#[derive(Clone)]
pub struct OptionsForCheck {
pub windows: bool,
pub truncate: bool,
}

impl Default for OptionsForCheck {
fn default() -> Self {
OptionsForCheck {
windows: cfg!(windows),
truncate: true,
}
}
}

pub fn is_sanitized<S: AsRef<str>>(name: S) -> bool {
is_sanitized_with_options(name, OptionsForCheck::default())
}

pub fn is_sanitized_with_options<S: AsRef<str>>(name: S, options: OptionsForCheck) -> bool {

let OptionsForCheck { windows, truncate } = options;
let name = name.as_ref();

if ILLEGAL_RE.is_match(&name) {
return false;
}
if CONTROL_RE.is_match(&name) {
return false;
}
if RESERVED_RE.is_match(&name) {
return false;
}
if truncate && name.len() > 255 {
return false;
}
if windows {
if WINDOWS_RESERVED_RE.is_match(&name) {
return false;
}
if WINDOWS_TRAILING_RE.is_match(&name) {
return false;
}
}

return true;

}

#[cfg(test)]
mod tests {

Expand Down Expand Up @@ -164,9 +213,54 @@ mod tests {
""
];

static NAMES_IS_SANITIZED: &'static [bool] = &[
true,
true,
false,
false,
true,
true,
false,
false,
false,
false,
false,
false,
true,
false,
false,
true,
false,
true,
true,
true,
true,
false,
false,
false,
true,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false
];

#[test]
fn it_works() {

// sanitize
let options = super::Options {
windows: true,
truncate: true,
Expand All @@ -181,5 +275,17 @@ mod tests {
let shorter = ::std::iter::repeat('a').take(255).collect::<String>();
assert_eq!(super::sanitize_with_options(long, options.clone()), shorter);

// is_sanitized
let options = super::OptionsForCheck {
windows: true,
truncate: true,
};

for (idx, name) in NAMES.iter().enumerate() {
assert_eq!(super::is_sanitized_with_options(name, options.clone()), NAMES_IS_SANITIZED[idx]);
}

let long = ::std::iter::repeat('a').take(300).collect::<String>();
assert_eq!(super::is_sanitized_with_options(long, options.clone()), false);
}
}

0 comments on commit c3d7196

Please sign in to comment.