mirror of https://github.com/boa-dev/boa.git
José Julián Espina
1 year ago
committed by
GitHub
9 changed files with 100 additions and 110 deletions
@ -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 |
||||
let val = value.to_string(context)?.to_std_string_escaped(); |
||||
|
||||
if val.len() < 3 { |
||||
return Err(JsNativeError::range() |
||||
.with_message("nonterminal `type` must be at least 3 characters long") |
||||
.into()); |
||||
} |
||||
|
||||
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()))?; |
||||
.map_err(|e| JsNativeError::range().with_message(e.to_string()).into()) |
||||
} |
||||
} |
||||
|
||||
if val.as_tinystr_slice().is_empty() { |
||||
return Err(JsNativeError::range() |
||||
.with_message("Unicode Locale Identifier `type` cannot be empty") |
||||
.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()) |
||||
} |
||||
} |
||||
|
||||
Ok(val) |
||||
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()) |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue