Skip to content

Commit

Permalink
Merge pull request #28 from ebkalderon/add-x-shellscript-support
Browse files Browse the repository at this point in the history
Add support for text/x-shellscript
  • Loading branch information
bojand committed Dec 1, 2020
2 parents 5e85472 + 7791f61 commit c4e825f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,5 +465,11 @@ matcher_map!(
"html",
matchers::text::is_html
),
(MatcherType::TEXT, "text/xml", "xml", matchers::text::is_xml)
(MatcherType::TEXT, "text/xml", "xml", matchers::text::is_xml),
(
MatcherType::TEXT,
"text/x-shellscript",
"sh",
matchers::text::is_shellscript
)
);
12 changes: 11 additions & 1 deletion src/matchers/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@ fn starts_with_ignore_ascii_case(buf: &[u8], needle: &[u8]) -> bool {
buf.len() >= needle.len() && buf[..needle.len()].eq_ignore_ascii_case(needle)
}

/// Returns whether a buffer is a shell script.
pub fn is_shellscript(buf: &[u8]) -> bool {
buf.len() > 2 && &buf[..2] == b"#!"
}

#[cfg(test)]
mod tests {
use super::{is_html, trim_start_whitespaces};
use super::{is_html, is_shellscript, trim_start_whitespaces};

#[test]
fn trim_whitespaces() {
Expand All @@ -88,4 +93,9 @@ mod tests {
assert_eq!(is_html(b"<HTML "), true);
assert_eq!(is_html(b" <BODY>"), true);
}

#[test]
fn shellscript() {
assert_eq!(is_shellscript(b"#!"), false);
}
}
1 change: 1 addition & 0 deletions testdata/sample.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#!/bin/bash
2 changes: 2 additions & 0 deletions tests/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ mod common;
test_format!(TEXT, "text/html", "html", html, "sample.html");

test_format!(TEXT, "text/xml", "xml", xml, "sample.xml");

test_format!(TEXT, "text/x-shellscript", "sh", sh, "sample.sh");

0 comments on commit c4e825f

Please sign in to comment.