Skip to content

Commit

Permalink
Move options utils to builtins module
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Sep 24, 2023
1 parent e43d4b1 commit 2b1af66
Show file tree
Hide file tree
Showing 17 changed files with 233 additions and 202 deletions.
6 changes: 4 additions & 2 deletions boa_engine/src/builtins/intl/collator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use icu_locid::{
use icu_provider::DataLocale;

use crate::{
builtins::{BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject},
builtins::{
options::get_option, BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject,
},
context::{
intrinsics::{Intrinsics, StandardConstructor, StandardConstructors},
BoaProvider,
Expand All @@ -30,7 +32,7 @@ use crate::{

use super::{
locale::{canonicalize_locale_list, resolve_locale, supported_locales, validate_extension},
options::{coerce_options_to_object, get_option, IntlOptions, LocaleMatcher},
options::{coerce_options_to_object, IntlOptions, LocaleMatcher},
Service,
};

Expand Down
6 changes: 3 additions & 3 deletions boa_engine/src/builtins/intl/collator/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::str::FromStr;
use icu_collator::{CaseFirst, CaseLevel, Strength};

use crate::{
builtins::intl::options::{OptionType, OptionTypeParsable},
builtins::options::{OptionType, ParsableOptionType},
Context, JsNativeError, JsResult, JsValue,
};

Expand Down Expand Up @@ -50,7 +50,7 @@ impl FromStr for Sensitivity {
}
}

impl OptionTypeParsable for Sensitivity {}
impl ParsableOptionType for Sensitivity {}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) enum Usage {
Expand Down Expand Up @@ -80,7 +80,7 @@ impl FromStr for Usage {
}
}

impl OptionTypeParsable for Usage {}
impl ParsableOptionType for Usage {}

impl OptionType for CaseFirst {
fn from_value(value: JsValue, context: &mut Context<'_>) -> JsResult<Self> {
Expand Down
6 changes: 3 additions & 3 deletions boa_engine/src/builtins/intl/date_time_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
//! [spec]: https://tc39.es/ecma402/#datetimeformat-objects
use crate::{
builtins::{BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject},
builtins::{
options::OptionType, BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject,
},
context::intrinsics::{Intrinsics, StandardConstructor, StandardConstructors},
error::JsNativeError,
js_string,
Expand All @@ -22,8 +24,6 @@ use boa_gc::{Finalize, Trace};
use boa_profiler::Profiler;
use icu_datetime::options::preferences::HourCycle;

use super::options::OptionType;

impl OptionType for HourCycle {
fn from_value(value: JsValue, context: &mut Context<'_>) -> JsResult<Self> {
match value.to_string(context)?.to_std_string_escaped().as_str() {
Expand Down
7 changes: 5 additions & 2 deletions boa_engine/src/builtins/intl/list_format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use icu_locid::Locale;
use icu_provider::DataLocale;

use crate::{
builtins::{Array, BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject},
builtins::{
options::{get_option, get_options_object},
Array, BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject,
},
context::intrinsics::{Intrinsics, StandardConstructor, StandardConstructors},
object::{internal_methods::get_prototype_from_constructor, JsObject, ObjectData},
property::Attribute,
Expand All @@ -18,7 +21,7 @@ use crate::{

use super::{
locale::{canonicalize_locale_list, resolve_locale, supported_locales},
options::{get_option, get_options_object, IntlOptions, LocaleMatcher},
options::{IntlOptions, LocaleMatcher},
Service,
};

Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/builtins/intl/list_format/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::str::FromStr;
use icu_list::ListLength;

use crate::{
builtins::intl::options::{OptionType, OptionTypeParsable},
builtins::options::{OptionType, ParsableOptionType},
Context, JsNativeError, JsResult, JsValue,
};

Expand Down Expand Up @@ -37,7 +37,7 @@ impl FromStr for ListFormatType {
}
}

impl OptionTypeParsable for ListFormatType {}
impl ParsableOptionType for ListFormatType {}

impl OptionType for ListLength {
fn from_value(value: JsValue, context: &mut Context<'_>) -> JsResult<Self> {
Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/builtins/intl/locale/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{realm::Realm, string::utf16};
use crate::{builtins::options::get_option, realm::Realm, string::utf16};
use boa_profiler::Profiler;
use icu_collator::CaseFirst;
use icu_datetime::options::preferences::HourCycle;
Expand Down Expand Up @@ -26,7 +26,7 @@ use crate::{
Context, JsArgs, JsNativeError, JsResult, JsString, JsValue,
};

use super::options::{coerce_options_to_object, get_option};
use super::options::coerce_options_to_object;

#[derive(Debug, Clone)]
pub(crate) struct Locale;
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/builtins/intl/locale/options.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use icu_locid::extensions::unicode::Value;

use crate::{builtins::intl::options::OptionType, Context, JsNativeError};
use crate::{builtins::options::OptionType, Context, JsNativeError};

impl OptionType for Value {
fn from_value(value: crate::JsValue, context: &mut Context<'_>) -> crate::JsResult<Self> {
Expand Down
3 changes: 2 additions & 1 deletion boa_engine/src/builtins/intl/locale/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::{
builtins::{
intl::{
options::{coerce_options_to_object, get_option, IntlOptions, LocaleMatcher},
options::{coerce_options_to_object, IntlOptions, LocaleMatcher},
Service,
},
options::get_option,
Array,
},
context::{icu::Icu, BoaProvider},
Expand Down
69 changes: 4 additions & 65 deletions boa_engine/src/builtins/intl/number_format/options.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;

use crate::builtins::intl::options::OptionTypeParsable;
use crate::builtins::options::{ParsableOptionType, RoundingMode};

#[derive(Debug)]
pub(crate) struct DigitFormatOptions {
Expand Down Expand Up @@ -44,7 +44,7 @@ impl std::str::FromStr for Notation {
}
}

impl OptionTypeParsable for Notation {}
impl ParsableOptionType for Notation {}

#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
pub(crate) enum RoundingPriority {
Expand Down Expand Up @@ -76,7 +76,7 @@ impl std::str::FromStr for RoundingPriority {
}
}

impl OptionTypeParsable for RoundingPriority {}
impl ParsableOptionType for RoundingPriority {}

impl fmt::Display for RoundingPriority {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand All @@ -89,67 +89,6 @@ impl fmt::Display for RoundingPriority {
}
}

#[derive(Debug, Copy, Clone, Default)]
pub(crate) enum RoundingMode {
Ceil,
Floor,
Expand,
Trunc,
HalfCeil,
HalfFloor,
#[default]
HalfExpand,
HalfTrunc,
HalfEven,
}

#[derive(Debug)]
pub(crate) struct ParseRoundingModeError;

impl fmt::Display for ParseRoundingModeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("provided string was not a valid rounding mode")
}
}

impl std::str::FromStr for RoundingMode {
type Err = ParseRoundingModeError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"ceil" => Ok(Self::Ceil),
"floor" => Ok(Self::Floor),
"expand" => Ok(Self::Expand),
"trunc" => Ok(Self::Trunc),
"halfCeil" => Ok(Self::HalfCeil),
"halfFloor" => Ok(Self::HalfFloor),
"halfExpand" => Ok(Self::HalfExpand),
"halfTrunc" => Ok(Self::HalfTrunc),
"halfEven" => Ok(Self::HalfEven),
_ => Err(ParseRoundingModeError),
}
}
}

impl OptionTypeParsable for RoundingMode {}

impl fmt::Display for RoundingMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Ceil => "ceil",
Self::Floor => "floor",
Self::Expand => "expand",
Self::Trunc => "trunc",
Self::HalfCeil => "halfCeil",
Self::HalfFloor => "halfFloor",
Self::HalfExpand => "halfExpand",
Self::HalfTrunc => "halfTrunc",
Self::HalfEven => "halfEven",
}
.fmt(f)
}
}

#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
pub(crate) enum TrailingZeroDisplay {
#[default]
Expand Down Expand Up @@ -178,7 +117,7 @@ impl std::str::FromStr for TrailingZeroDisplay {
}
}

impl OptionTypeParsable for TrailingZeroDisplay {}
impl ParsableOptionType for TrailingZeroDisplay {}

impl fmt::Display for TrailingZeroDisplay {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand Down
9 changes: 6 additions & 3 deletions boa_engine/src/builtins/intl/number_format/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use boa_macros::utf16;
use fixed_decimal::{FixedDecimal, FloatPrecision};

use crate::{
builtins::intl::{
number_format::{Extrema, RoundingMode, RoundingType, TrailingZeroDisplay},
options::{default_number_option, get_number_option, get_option},
builtins::{
intl::{
number_format::{Extrema, RoundingType, TrailingZeroDisplay},
options::{default_number_option, get_number_option},
},
options::{get_option, RoundingMode},
},
Context, JsNativeError, JsObject, JsResult,
};
Expand Down
Loading

0 comments on commit 2b1af66

Please sign in to comment.