unic-langid-0.9.0/.cargo_vcs_info.json0000644000000001121365450470400133340ustar00{ "git": { "sha1": "0432f0414e443c3c6cda5da584205e41ac41ff2c" } } unic-langid-0.9.0/Cargo.lock0000644000000047221365450470400113220ustar00# This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] name = "proc-macro-hack" version = "0.5.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d659fe7c6d27f25e9d80a1a094c223f5246f6a6596453e09d7229bf42750b63" [[package]] name = "proc-macro2" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8872cf6f48eee44265156c111456a700ab3483686b3f96df4cf5481c89157319" dependencies = [ "unicode-xid", ] [[package]] name = "quote" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c1f4b0efa5fc5e8ceb705136bfee52cfdb6a4e3509f770b478cd6ed434232a7" dependencies = [ "proc-macro2", ] [[package]] name = "serde" version = "1.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "36df6ac6412072f67cf767ebbde4133a5b2e88e76dc6187fa7104cd16f783399" [[package]] name = "syn" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "410a7488c0a728c7ceb4ad59b9567eb4053d02e8cc7f5c0e0eeeb39518369213" dependencies = [ "proc-macro2", "quote", "unicode-xid", ] [[package]] name = "tinystr" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4bac79c4b51eda1b090b1edebfb667821bbb51f713855164dc7cec2cb8ac2ba3" [[package]] name = "unic-langid" version = "0.9.0" dependencies = [ "unic-langid-impl", "unic-langid-macros", ] [[package]] name = "unic-langid-impl" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a4a8eeaf0494862c1404c95ec2f4c33a2acff5076f64314b465e3ddae1b934d" dependencies = [ "serde", "tinystr", ] [[package]] name = "unic-langid-macros" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "18f980d6d87e8805f2836d64b4138cc95aa7986fa63b1f51f67d5fbff64dd6e5" dependencies = [ "proc-macro-hack", "tinystr", "unic-langid-impl", "unic-langid-macros-impl", ] [[package]] name = "unic-langid-macros-impl" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29396ffd97e27574c3e01368b1a64267d3064969e4848e2e130ff668be9daa9f" dependencies = [ "proc-macro-hack", "quote", "syn", "unic-langid-impl", ] [[package]] name = "unicode-xid" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" unic-langid-0.9.0/Cargo.toml0000644000000023111365450470400113350ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "unic-langid" version = "0.9.0" authors = ["Zibi Braniecki "] include = ["src/**/*", "benches/*.rs", "Cargo.toml", "README.md"] description = "API for managing Unicode Language Identifiers" readme = "README.md" categories = ["internationalization"] license = "MIT/Apache-2.0" repository = "https://github.com/zbraniecki/unic-locale" [dependencies.unic-langid-impl] version = "0.9" [dependencies.unic-langid-macros] version = "0.9" optional = true [dev-dependencies.unic-langid-macros] version = "0.9" [features] default = [] likelysubtags = ["unic-langid-impl/likelysubtags"] macros = ["unic-langid-macros"] serde = ["unic-langid-impl/serde"] unic-langid-0.9.0/Cargo.toml.orig010064400017500001731000000014701365450346500150360ustar0000000000000000[package] name = "unic-langid" description = "API for managing Unicode Language Identifiers" version = "0.9.0" authors = ["Zibi Braniecki "] edition = "2018" readme = "README.md" repository = "https://github.com/zbraniecki/unic-locale" license = "MIT/Apache-2.0" categories = ["internationalization"] include = [ "src/**/*", "benches/*.rs", "Cargo.toml", "README.md" ] [dependencies] unic-langid-impl = { version = "0.9", path = "../unic-langid-impl" } unic-langid-macros = { version = "0.9", path = "../unic-langid-macros", optional = true } [dev-dependencies] unic-langid-macros = { version = "0.9", path = "../unic-langid-macros" } [features] default = [] serde = ["unic-langid-impl/serde"] # Provide macros. macros = ["unic-langid-macros"] likelysubtags = ["unic-langid-impl/likelysubtags"] unic-langid-0.9.0/README.md010064400017500001731000000036671360777407400134440ustar0000000000000000# unic-langid [![Build Status](https://travis-ci.org/zbraniecki/unic-locale.svg?branch=master)](https://travis-ci.org/zbraniecki/unic-locale) [![Coverage Status](https://coveralls.io/repos/github/zbraniecki/unic-locale/badge.svg?branch=master)](https://coveralls.io/github/zbraniecki/unic-locale?branch=master) `unic-langid` is an API for managing [Unicode Language Identifiers](http://unicode.org/reports/tr35/#Unicode_language_identifier). The crate provides a way to create a struct from a string, manipulate its fields, canonicalize it, and serialize into a string. Usage ----- ```rust use unic_langid::LanguageIdentifier; let loc: LanguageIdentifier = "en-US".parse().expect("Parsing failed."); assert_eq!(loc.language(), "en"); assert_eq!(loc.script(), None); assert_eq!(loc.region(), Some("US")); loc.set_script(Some("latn")); assert_eq!(&loc.to_string(), "en-Latn-US"); ``` ```rust use unic_langid::LanguageIdentifier; let langid = LanguageIdentifier::from_parts( Some("en"), None, None, Some(&["nedis", "macos"]) ).expect("Parsing failed."); assert_eq!(&langid.to_string(), "en-macos-nedis") ``` Macros ------ `unic-langid` can be also compiled with `features = ["macros"]` which enables `langid!` macro: ```rust use unic_langid::langid; // The `const` assignment will currently work only // if the langid doesn't contain any variants. const EN_US: LanguageIdentifier = langid!("en-US"); fn main() { let de_at = langid!("de-AT"); assert_eq!(&de_at.to_string(), "de-AT") assert_eq!(&EN_US.to_string(), "en-US") } ``` The macro allows for compile-time parsing and validation of literal language identifiers. Status ------ The crate is mostly complete on the `well-formed` level. The next batch of work will go into API maturity. Get Involved ------------ `unic-langid` is open-source, licensed under the Apache License, Version 2.0. We encourage everyone to take a look at our code and we'll listen to your feedback. unic-langid-0.9.0/src/lib.rs010064400017500001731000000100371365450333000140400ustar0000000000000000//! `unic-langid` is a core API for parsing, manipulating, and serializing Unicode Language //! Identifiers. //! //! The crate provides algorithms for parsing a string into a well-formed language identifier //! as defined by [`UTS #35: Unicode LDML 3.1 Unicode Language Identifier`]. //! //! # Examples //! //! ``` //! use unic_langid::LanguageIdentifier; //! use unic_langid::subtags::{Language, Script, Region}; //! //! let mut li: LanguageIdentifier = "en-US".parse() //! .expect("Parsing failed."); //! //! let lang: Language = "en".parse().expect("Parsing failed."); //! let region: Region = "US".parse().expect("Parsing failed."); //! assert_eq!(li.language, lang); //! assert_eq!(li.script, None); //! assert_eq!(li.region, Some(region)); //! assert_eq!(li.variants().len(), 0); //! //! let region: Region = "GB".parse().expect("Parsing failed."); //! li.region = Some(region); //! //! assert_eq!(li.to_string(), "en-GB"); //! ``` //! //! For more details, see [`LanguageIdentifier`]. //! //! # Optional features //! //! ## `langid!` and `langids!` macros //! //! If `feature = "macros"` is selected, the crate provides a procedural macro //! which allows to construct build-time well-formed language identifiers with zero-cost at runtime. //! //! ``` ignore //! use unic_langid::{langid, langids, lang, region, script, variant}; //! use unic_langid::subtags::{Language, Script, Region, Variant}; //! use std::str::FromStr; //! //! let es_ar = langid!("es-AR"); //! let en_us = langid!("en-US"); //! //! assert_eq!(&es_ar.to_string(), "es-AR"); //! assert_eq!(&en_us.to_string(), "en-US"); //! //! let lang_ids = langids!("es-AR", "en-US", "de"); //! //! assert_eq!(lang_ids[0], "es-AR"); //! assert_eq!(lang_ids[1], "en-US"); //! assert_eq!(lang_ids[2], "de"); //! //! assert_eq!(lang!("pl"), "pl"); //! assert_eq!(lang!("pl"), Language::from_str("pl").unwrap()); //! //! assert_eq!(script!("latn"), "Latn"); //! assert_eq!(script!("latn"), Script::from_str("Latn").unwrap()); //! //! assert_eq!(region!("us"), "US"); //! assert_eq!(region!("us"), Region::from_str("us").unwrap()); //! //! assert_eq!(variant!("macos"), "macos"); //! assert_eq!(variant!("macos"), Variant::from_str("macos").unwrap()); //! ``` //! //! The macros produce instances of `LanguageIdentifier` the same way as parsing from `&str` does, //! but since the parsing is performed at build time, it doesn't need a `Result`. //! //! At the moment `langid!` can also be used for const variables, but only if no variants are used. //! //! The macros are optional to reduce the dependency chain and compilation time of `unic-langid`. //! //! ## Likely Subtags //! //! If `feature = "likelysubtags"` is selected, the `LanguageIdentifier` gains two more methods: //! //! * add_likely_subtags //! * remove_likely_subtags //! //! Both of them operate in place updating the existing `LanguageIdentifier` by either extending //! subtags to most likely values, or removing the subtags that are not needed. //! //! Both methods return a `bool` that indicates if the identifier has been modified. //! //! ``` ignore //! use unic_langid::LanuageIdentifier; //! //! let mut li: LanguageIdentifier = "fr-FR".parse() //! .expect("Parsing failed."); //! //! assert_eq!(li.add_likely_subtags(), true); //! assert_eq!(li, "fr-Latn-FR"); //! //! assert_eq!(li.remove_likely_subtags(), true); //! assert_eq!(li, "fr"); //! ``` //! //! The feature is optional because it increases the binary size of the library by including //! a data table for CLDR likelySubtags. //! //! [`UTS #35: Unicode LDML 3.1 Unicode Language Identifier`]: https://unicode.org/reports/tr35/tr35.html#Unicode_language_identifier //! [`LanguageIdentifier`]: ./struct.LanguageIdentifier.html pub use unic_langid_impl::*; #[cfg(feature = "unic-langid-macros")] pub use unic_langid_macros::{lang, langid, region, script, variant}; #[cfg(feature = "unic-langid-macros")] #[macro_export] macro_rules! langids { ( $($langid:expr),* ) => { { let mut v = vec![]; $( v.push(langid!($langid)); )* v } }; }