5 releases

0.1.5 Feb 22, 2020
0.1.4 Feb 13, 2020

#1204 in Algorithms

Download history 28/week @ 2024-03-11 15/week @ 2024-03-18 16/week @ 2024-03-25 53/week @ 2024-04-01 31/week @ 2024-04-08 22/week @ 2024-04-15 25/week @ 2024-04-22 23/week @ 2024-04-29 24/week @ 2024-05-06 47/week @ 2024-05-13 28/week @ 2024-05-20 23/week @ 2024-05-27 28/week @ 2024-06-03 21/week @ 2024-06-10 18/week @ 2024-06-17 29/week @ 2024-06-24

97 downloads per month
Used in 6 crates (2 directly)

MIT license

18KB
125 lines

detect-lang

Build Status Latest Version Docs License

This crate is a utility for identifying names of programming languages (and related files) from paths and file extensions.

This is not a crate for detecting natural languages.

Usage

Add this to your Cargo.toml:

[dependencies]
detect-lang = "0.1"

Releases

Release notes are available in the repo at CHANGELOG.md.

Paths and Extensions

Languages can be identified from paths using from_path or directly from extensions using from_extension.

use detect_lang::from_path;
assert_eq!(from_path("foo.rs").unwrap().name(), "Rust");
assert_eq!(from_path("foo.md").unwrap().name(), "Markdown");

use detect_lang::from_extension;
assert_eq!(from_extension("rs").unwrap().name(), "Rust");
assert_eq!(from_extension("md").unwrap().name(), "Markdown");

// The case is ignored
assert_eq!(from_path("foo.jSoN").unwrap().name(), "JSON");
assert_eq!(from_extension("jSoN").unwrap().name(), "JSON");

Language ID

In short, the language id is a lowercase version of name. However, it also replaces symbols making it usable as a URL slug.

For instance foo.hpp is identified as language name C++ and language ID cpp.

use detect_lang::from_path;
assert_eq!(from_path("foo.rs").unwrap().id(), "rust");
assert_eq!(from_path("foo.cpp").unwrap().id(), "cpp");
assert_eq!(from_path("foo.hpp").unwrap().id(), "cpp");

use detect_lang::from_extension;
assert_eq!(from_extension("rs").unwrap().id(), "rust");
assert_eq!(from_extension("cpp").unwrap().id(), "cpp");
assert_eq!(from_extension("hpp").unwrap().id(), "cpp");

// The case is ignored
assert_eq!(from_path("foo.jSoN").unwrap().id(), "json");
assert_eq!(from_extension("jSoN").unwrap().id(), "json");

Always Lowercase

If the extension is guaranteed to always be lowercase, then consider using from_lowercase_extension to avoid allocation and conversion to lowercase.

use detect_lang::{from_extension, from_lowercase_extension, Language};

assert_eq!(from_lowercase_extension("json"), Some(Language("JSON", "json")));
assert_eq!(from_lowercase_extension("jSoN"), None);

assert_eq!(from_extension("json"), Some(Language("JSON", "json")));
assert_eq!(from_extension("jSoN"), Some(Language("JSON", "json")));

Match Example

use std::path::Path;
use detect_lang::{from_path, Language};

let path = Path::new("foo.rs");
match from_path(path) {
    //   Language(name, id)
    Some(Language(_, "rust")) => println!("This is Rust"),
    Some(Language(..))        => println!("Well it's not Rust"),
    None                      => println!("Ehh, what?"),
}

No runtime deps