-
-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup
get_option
and calls to the functioncar (#3355)
- Loading branch information
Showing
9 changed files
with
100 additions
and
110 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,51 @@ | ||
use icu_locid::extensions::unicode::Value; | ||
use icu_locid::{ | ||
extensions::unicode::Value, | ||
subtags::{Language, Region, Script}, | ||
}; | ||
|
||
use crate::{builtins::options::OptionType, Context, JsNativeError}; | ||
|
||
impl OptionType for Value { | ||
fn from_value(value: crate::JsValue, context: &mut Context<'_>) -> crate::JsResult<Self> { | ||
let val = value | ||
.to_string(context)? | ||
.to_std_string_escaped() | ||
.parse::<Self>() | ||
.map_err(|e| JsNativeError::range().with_message(e.to_string()))?; | ||
let val = value.to_string(context)?.to_std_string_escaped(); | ||
|
||
if val.as_tinystr_slice().is_empty() { | ||
if val.len() < 3 { | ||
return Err(JsNativeError::range() | ||
.with_message("Unicode Locale Identifier `type` cannot be empty") | ||
.with_message("nonterminal `type` must be at least 3 characters long") | ||
.into()); | ||
} | ||
|
||
Ok(val) | ||
val.parse::<Self>() | ||
.map_err(|e| JsNativeError::range().with_message(e.to_string()).into()) | ||
} | ||
} | ||
|
||
impl OptionType for Language { | ||
fn from_value(value: crate::JsValue, context: &mut Context<'_>) -> crate::JsResult<Self> { | ||
value | ||
.to_string(context)? | ||
.to_std_string_escaped() | ||
.parse::<Self>() | ||
.map_err(|e| JsNativeError::range().with_message(e.to_string()).into()) | ||
} | ||
} | ||
|
||
impl OptionType for Script { | ||
fn from_value(value: crate::JsValue, context: &mut Context<'_>) -> crate::JsResult<Self> { | ||
value | ||
.to_string(context)? | ||
.to_std_string_escaped() | ||
.parse::<Self>() | ||
.map_err(|e| JsNativeError::range().with_message(e.to_string()).into()) | ||
} | ||
} | ||
|
||
impl OptionType for Region { | ||
fn from_value(value: crate::JsValue, context: &mut Context<'_>) -> crate::JsResult<Self> { | ||
value | ||
.to_string(context)? | ||
.to_std_string_escaped() | ||
.parse::<Self>() | ||
.map_err(|e| JsNativeError::range().with_message(e.to_string()).into()) | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.