Last active
April 7, 2016 05:28
-
-
Save mrhota/b6c848cd594967c64cb28cc0fbd35559 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(plugin)] | |
#![plugin(regex_macros)] | |
extern crate regex; | |
use regex::Regex; | |
const LANGUAGE_TAG_REGEX: Regex = regex!(r"(?ix) | |
(?P<language>[:alpha:]{2,3}(-(?P<extlang>[:alpha:]{3}(-[:alpha:]{3}){0,2}))?) | |
(?P<script>-[:alpha:]{4})? | |
(?P<region>-([:alpha:]{2}|[:digit:]{3}))? | |
(?P<variant>-([:alnum:]{5,8}|[:digit:][:alnum:]{3}))* | |
(?P<extension>-(?P<singleton>[0-9a-wy-z])(-[:alnum:]{2,8})+)* | |
(?P<privateuse>-x(-[:alnum:]{1,8})+)? | |
"); | |
// produces compiler error: | |
// src/languagetag.rs:8:35: 15:3 error: constants cannot refer to other statics, insert an intermediate constant instead [E0013] | |
// src/languagetag.rs: 8 const LANGUAGE_TAG_REGEX: Regex = regex!(r"(?ix) | |
// src/languagetag.rs: 9 (?P<language>[:alpha:]{2,3}(-(?P<extlang>[:alpha:]{3}(-[:alpha:]{3}){0,2}))?) | |
// .. | |
/////////// | |
// const LANGUAGE_TAG_RE: &'static strt = r"(?ix) | |
// (?P<language>[:alpha:]{2,3}(-(?P<extlang>[:alpha:]{3}(-[:alpha:]{3}){0,2}))?) | |
// (?P<script>-[:alpha:]{4})? | |
// (?P<region>-([:alpha:]{2}|[:digit:]{3}))? | |
// (?P<variant>-([:alnum:]{5,8}|[:digit:][:alnum:]{3}))* | |
// (?P<extension>-(?P<singleton>[0-9a-wy-z])(-[:alnum:]{2,8})+)* | |
// (?P<privateuse>-x(-[:alnum:]{1,8})+)? | |
// "; | |
// const RE: Regex = regex!(LANGUAGE_TAG_RE); | |
// produces compiler error: | |
// src/languagetag.rs:16:42: 16:54 error: expected string literal but got `LANGUAGE_TAG_RE` | |
// src/languagetag.rs:16 const LANGUAGE_TAG_REGEX: Regex = regex!(LANGUAGE_TAG_RE); | |
// ^~~~~~~~~~~~ | |
pub struct LanguageTag<'a> { | |
language: &'a str | |
} | |
impl<'a> LanguageTag<'a> { | |
pub fn new(lt: &'a str) -> Result<LanguageTag, &'static str> { | |
if Self::is_well_formed(lt) { | |
Ok(Self::canonicalize(lt)) | |
} else { | |
Err(NOT_WELL_FORMED) | |
} | |
} | |
fn is_well_formed(lt: &'a str) -> bool { | |
return LANGUAGE_TAG_REGEX.is_match(lt) | |
} | |
fn canonicalize(lt: &'a str) -> LanguageTag { | |
// stub | |
LanguageTag { | |
language: lt | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment