{"id":16856409,"url":"https://github.com/vallentin/detect-lang","last_synced_at":"2025-04-11T07:38:44.926Z","repository":{"id":44948545,"uuid":"239164495","full_name":"vallentin/detect-lang","owner":"vallentin","description":"Detect the programming language of paths and extensions","archived":false,"fork":false,"pushed_at":"2022-01-15T22:38:57.000Z","size":20,"stargazers_count":7,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-25T05:11:09.660Z","etag":null,"topics":["detect-language","language","language-detection","languages","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vallentin.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-08T16:30:48.000Z","updated_at":"2024-07-17T14:32:43.000Z","dependencies_parsed_at":"2022-09-26T21:41:08.030Z","dependency_job_id":null,"html_url":"https://github.com/vallentin/detect-lang","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vallentin%2Fdetect-lang","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vallentin%2Fdetect-lang/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vallentin%2Fdetect-lang/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vallentin%2Fdetect-lang/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vallentin","download_url":"https://codeload.github.com/vallentin/detect-lang/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247867361,"owners_count":21009240,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["detect-language","language","language-detection","languages","rust"],"created_at":"2024-10-13T14:04:09.309Z","updated_at":"2025-04-11T07:38:44.894Z","avatar_url":"https://github.com/vallentin.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# detect-lang\n\n[![Build Status](https://github.com/vallentin/detect-lang/workflows/Rust/badge.svg)](https://github.com/vallentin/detect-lang/actions?query=workflow%3ARust)\n[![Build Status](https://travis-ci.org/vallentin/detect-lang.svg?branch=master)](https://travis-ci.org/vallentin/detect-lang)\n[![Latest Version](https://img.shields.io/crates/v/detect-lang.svg)](https://crates.io/crates/detect-lang)\n[![Docs](https://docs.rs/detect-lang/badge.svg)](https://docs.rs/detect-lang)\n[![License](https://img.shields.io/github/license/vallentin/detect-lang.svg)](https://github.com/vallentin/detect-lang)\n\nThis crate is a utility for identifying names of programming languages (and related files) from paths and file extensions.\n\nThis is **not** a crate for detecting **natural** languages.\n\n## Usage\n\nAdd this to your `Cargo.toml`:\n\n```toml\n[dependencies]\ndetect-lang = \"0.1\"\n```\n\n## Releases\n\nRelease notes are available in the repo at [CHANGELOG.md].\n\n[CHANGELOG.md]: CHANGELOG.md\n\n## Paths and Extensions\n\nLanguages can be identified from paths using [`from_path`]\nor directly from extensions using [`from_extension`].\n\n[`from_path`]: https://docs.rs/detect-lang/*/detect_lang/fn.from_path.html\n[`from_extension`]: https://docs.rs/detect-lang/*/detect_lang/fn.from_extension.html\n\n```rust\nuse detect_lang::from_path;\nassert_eq!(from_path(\"foo.rs\").unwrap().name(), \"Rust\");\nassert_eq!(from_path(\"foo.md\").unwrap().name(), \"Markdown\");\n\nuse detect_lang::from_extension;\nassert_eq!(from_extension(\"rs\").unwrap().name(), \"Rust\");\nassert_eq!(from_extension(\"md\").unwrap().name(), \"Markdown\");\n\n// The case is ignored\nassert_eq!(from_path(\"foo.jSoN\").unwrap().name(), \"JSON\");\nassert_eq!(from_extension(\"jSoN\").unwrap().name(), \"JSON\");\n```\n\n## Language ID\n\nIn short, the language [`id`] is a lowercase version of [`name`].\nHowever, it also replaces symbols making it usable as a [URL slug].\n\nFor instance `foo.hpp` is identified as language name `C++` and\nlanguage ID `cpp`.\n\n[`id`]: https://docs.rs/detect-lang/*/detect_lang/struct.Language.html#method.id\n[`name`]: https://docs.rs/detect-lang/*/detect_lang/struct.Language.html#method.name\n[URL slug]: https://en.wikipedia.org/wiki/Clean_URL#Slug\n\n```rust\nuse detect_lang::from_path;\nassert_eq!(from_path(\"foo.rs\").unwrap().id(), \"rust\");\nassert_eq!(from_path(\"foo.cpp\").unwrap().id(), \"cpp\");\nassert_eq!(from_path(\"foo.hpp\").unwrap().id(), \"cpp\");\n\nuse detect_lang::from_extension;\nassert_eq!(from_extension(\"rs\").unwrap().id(), \"rust\");\nassert_eq!(from_extension(\"cpp\").unwrap().id(), \"cpp\");\nassert_eq!(from_extension(\"hpp\").unwrap().id(), \"cpp\");\n\n// The case is ignored\nassert_eq!(from_path(\"foo.jSoN\").unwrap().id(), \"json\");\nassert_eq!(from_extension(\"jSoN\").unwrap().id(), \"json\");\n```\n\n## Always Lowercase\n\nIf the extension is guaranteed to always be lowercase,\nthen consider using [`from_lowercase_extension`] to avoid\nallocation and conversion to lowercase.\n\n[`from_lowercase_extension`]: https://docs.rs/detect-lang/*/detect_lang/fn.from_lowercase_extension.html\n\n```rust\nuse detect_lang::{from_extension, from_lowercase_extension, Language};\n\nassert_eq!(from_lowercase_extension(\"json\"), Some(Language(\"JSON\", \"json\")));\nassert_eq!(from_lowercase_extension(\"jSoN\"), None);\n\nassert_eq!(from_extension(\"json\"), Some(Language(\"JSON\", \"json\")));\nassert_eq!(from_extension(\"jSoN\"), Some(Language(\"JSON\", \"json\")));\n```\n\n## Match Example\n\n```rust\nuse std::path::Path;\nuse detect_lang::{from_path, Language};\n\nlet path = Path::new(\"foo.rs\");\nmatch from_path(path) {\n    //   Language(name, id)\n    Some(Language(_, \"rust\")) =\u003e println!(\"This is Rust\"),\n    Some(Language(..))        =\u003e println!(\"Well it's not Rust\"),\n    None                      =\u003e println!(\"Ehh, what?\"),\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvallentin%2Fdetect-lang","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvallentin%2Fdetect-lang","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvallentin%2Fdetect-lang/lists"}