Skip to content

Commit

Permalink
Fix #14: Language support for Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddieM4 committed Jun 18, 2024
1 parent 1722a32 commit c2ca043
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ docs in Obsidian most of the time.
* Bash
* C
* JavaScript
* Rust

### Contributing

Expand Down
22 changes: 22 additions & 0 deletions src/filetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum FileType {
Bash,
C,
JavaScript,
Rust,
Unknown,
}

Expand All @@ -29,6 +30,8 @@ impl From<&str> for FileType {
"c" => FileType::C,
"javascript" => FileType::JavaScript,
"js" => FileType::JavaScript,
"rust" => FileType::Rust,
"rs" => FileType::Rust,
_ => FileType::Unknown,
}
}
Expand All @@ -41,6 +44,7 @@ impl From<FileType> for String {
FileType::Bash => "bash",
FileType::C => "c",
FileType::JavaScript => "javascript",
FileType::Rust => "rust",
FileType::Unknown => "unknown",
}.into()
}
Expand Down Expand Up @@ -87,6 +91,7 @@ pub fn detect_path(ft: FileType, lines: &Vec<&str>) -> Option<PathDetection> {
FileType::Bash => r"#\s*(\w.*\.sh\b)",
FileType::C => r"//\s*(\w.*\.(c|h)\b)",
FileType::JavaScript => r"//\s*(\w.*\.js\b)",
FileType::Rust => r"//\s*(\w.*\.rs\b)",
FileType::Unknown => return None,
};
let re = Regex::new(pat).expect("Failed to compile regex");
Expand Down Expand Up @@ -185,6 +190,23 @@ mod test {
]);
}

#[test]
fn test_rust() {
let ft = FileType::Rust;
check_none(ft, vec![
"fn main() {",
" println!('Hello world');",
"}",
]);
check_some(ft, 0, "foo.rs", vec![
"// foo.rs",
"",
"fn main() {",
" println!('Hello world');",
"}",
]);
}

#[test]
fn test_js() {
let ft = FileType::JavaScript;
Expand Down

0 comments on commit c2ca043

Please sign in to comment.