diff --git a/Cargo.lock b/Cargo.lock index 1b2c788336e..0e767dcb0d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -899,6 +899,7 @@ dependencies = [ "ecma402_traits", "icu", "icu_locid", + "icu_plurals", "icu_provider", "icu_testdata", "serde", diff --git a/components/decimal/Cargo.toml b/components/decimal/Cargo.toml index e438e96ee41..6ba8dd4e10f 100644 --- a/components/decimal/Cargo.toml +++ b/components/decimal/Cargo.toml @@ -47,6 +47,7 @@ rand_distr = "0.4" getrandom = { version = "0.2", features = ["js"] } [features] +std = ["icu_locid/std", "icu_provider/std", "fixed_decimal/std"] default = ["provider_serde"] bench = [] provider_serde = ["serde"] diff --git a/components/decimal/src/error.rs b/components/decimal/src/error.rs index c1eaf8066d1..61aa4b4cea6 100644 --- a/components/decimal/src/error.rs +++ b/components/decimal/src/error.rs @@ -12,6 +12,7 @@ pub enum Error { Data(icu_provider::DataError), } +#[cfg(feature = "std")] impl std::error::Error for Error {} impl From for Error { diff --git a/components/decimal/src/format.rs b/components/decimal/src/format.rs index 826da251ea3..132085699cc 100644 --- a/components/decimal/src/format.rs +++ b/components/decimal/src/format.rs @@ -32,9 +32,9 @@ impl<'l> FormattedFixedDecimal<'l> { } impl<'l> Writeable for FormattedFixedDecimal<'l> { - fn write_to(&self, sink: &mut W) -> std::result::Result<(), std::fmt::Error> + fn write_to(&self, sink: &mut W) -> core::result::Result<(), core::fmt::Error> where - W: std::fmt::Write + ?Sized, + W: core::fmt::Write + ?Sized, { let affixes = self.get_affixes(); if let Some(affixes) = affixes { diff --git a/components/decimal/src/lib.rs b/components/decimal/src/lib.rs index 6d759eafd05..75a46b77a5f 100644 --- a/components/decimal/src/lib.rs +++ b/components/decimal/src/lib.rs @@ -56,6 +56,10 @@ //! //! [`FixedDecimalFormat`]: FixedDecimalFormat +#![cfg_attr(not(any(test, feature = "std")), no_std)] + +extern crate alloc; + pub mod error; pub mod format; mod grouper; diff --git a/components/decimal/src/provider.rs b/components/decimal/src/provider.rs index 8e94544d847..39ab018d129 100644 --- a/components/decimal/src/provider.rs +++ b/components/decimal/src/provider.rs @@ -6,8 +6,8 @@ //! //! Read more about data providers: [`icu_provider`] +use alloc::borrow::Cow; use icu_provider::yoke::{self, *}; -use std::borrow::Cow; pub mod key { //! Resource keys for [`icu_decimal`](crate). diff --git a/components/plurals/Cargo.toml b/components/plurals/Cargo.toml index 55cdddf97af..f5a4f7c5937 100644 --- a/components/plurals/Cargo.toml +++ b/components/plurals/Cargo.toml @@ -51,6 +51,7 @@ path = "src/lib.rs" bench = false # This option is required for Benchmark CI [features] +std = ["icu_locid/std", "icu_provider/std"] default = ["provider_serde"] bench = [] provider_serde = ["serde"] @@ -73,6 +74,10 @@ required-features = ["provider_serde"] name = "plurals" required-features = ["provider_serde"] +[[test]] +name = "operands" +required-features = ["provider_serde", "std"] + [[example]] name = "unread_emails" required-features = ["provider_serde"] diff --git a/components/plurals/src/data.rs b/components/plurals/src/data.rs index 58723539a57..4132cee2d79 100644 --- a/components/plurals/src/data.rs +++ b/components/plurals/src/data.rs @@ -7,8 +7,8 @@ use crate::provider::PluralRuleStringsV1; use crate::rules; use crate::rules::ast; use crate::{PluralCategory, PluralRulesError}; -use std::borrow::Cow; -use std::convert::TryInto; +use alloc::borrow::Cow; +use core::convert::TryInto; /// A raw function pointer to a `PluralRulesFn` // pub type PluralRulesFn = fn(&PluralOperands) -> PluralCategory; diff --git a/components/plurals/src/error.rs b/components/plurals/src/error.rs index 5ace4bc1ee0..0e03cf10762 100644 --- a/components/plurals/src/error.rs +++ b/components/plurals/src/error.rs @@ -17,6 +17,7 @@ pub enum PluralRulesError { DataProvider(DataError), } +#[cfg(feature = "std")] impl std::error::Error for PluralRulesError {} impl From for PluralRulesError { diff --git a/components/plurals/src/lib.rs b/components/plurals/src/lib.rs index 43f15aa9557..6e314ae6202 100644 --- a/components/plurals/src/lib.rs +++ b/components/plurals/src/lib.rs @@ -64,18 +64,23 @@ //! [`Plural Category`]: PluralCategory //! [`Language Plural Rules`]: https://unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules //! [`CLDR`]: http://cldr.unicode.org/ + +#![cfg_attr(not(any(test, feature = "std")), no_std)] + +extern crate alloc; + mod data; mod error; mod operands; pub mod provider; pub mod rules; +use core::convert::TryInto; pub use error::PluralRulesError; use icu_locid::LanguageIdentifier; use icu_provider::prelude::*; pub use operands::PluralOperands; use provider::{resolver, PluralRuleStringsV1, PluralRuleStringsV1Marker}; -use std::convert::TryInto; /// A type of a plural rule which can be associated with the [`PluralRules`] struct. /// diff --git a/components/plurals/src/operands.rs b/components/plurals/src/operands.rs index b9dc92dcc90..7c7bed15033 100644 --- a/components/plurals/src/operands.rs +++ b/components/plurals/src/operands.rs @@ -2,13 +2,12 @@ // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). +use core::convert::TryFrom; +use core::isize; +use core::num::ParseIntError; +use core::str::FromStr; use displaydoc::Display; use fixed_decimal::FixedDecimal; -use std::convert::TryFrom; -use std::io::Error as IOError; -use std::isize; -use std::num::ParseIntError; -use std::str::FromStr; /// A full plural operands representation of a number. See [CLDR Plural Rules](http://unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules) for complete operands description. /// Plural operands in compliance with [CLDR Plural Rules](http://unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules). @@ -80,6 +79,9 @@ impl PluralOperands { /// Returns the number represented by this [`PluralOperands`] as floating point. /// The precision of the number returned is up to the representation accuracy /// of a double. + /// + /// This method requires the `"std"` feature be enabled + #[cfg(feature = "std")] pub fn n(&self) -> f64 { let fraction = self.t as f64 / 10_f64.powi(self.v as i32); self.i as f64 + fraction @@ -96,6 +98,7 @@ pub enum OperandsError { Invalid, } +#[cfg(feature = "std")] impl std::error::Error for OperandsError {} impl From for OperandsError { @@ -104,8 +107,9 @@ impl From for OperandsError { } } -impl From for OperandsError { - fn from(_: IOError) -> Self { +#[cfg(feature = "std")] +impl From for OperandsError { + fn from(_: std::io::Error) -> Self { Self::Invalid } } @@ -230,8 +234,8 @@ impl From<&FixedDecimal> for PluralOperands { /// digits each from the integer and fraction parts. fn from(dec: &FixedDecimal) -> Self { let mag_range = dec.magnitude_range(); - let mag_high = std::cmp::min(17, *mag_range.end()); - let mag_low = std::cmp::max(-18, *mag_range.start()); + let mag_high = core::cmp::min(17, *mag_range.end()); + let mag_low = core::cmp::max(-18, *mag_range.start()); let mut i: u64 = 0; for magnitude in (0..=mag_high).rev() { diff --git a/components/plurals/src/provider.rs b/components/plurals/src/provider.rs index 03b5e639f01..247874ae3e8 100644 --- a/components/plurals/src/provider.rs +++ b/components/plurals/src/provider.rs @@ -6,8 +6,8 @@ //! //! Read more about data providers: [`icu_provider`] +use alloc::borrow::Cow; use icu_provider::yoke::{self, *}; -use std::borrow::Cow; pub mod key { use icu_provider::{resource_key, ResourceKey}; diff --git a/components/plurals/src/rules/ast.rs b/components/plurals/src/rules/ast.rs index 09fb6b9d0d1..0512a04efb1 100644 --- a/components/plurals/src/rules/ast.rs +++ b/components/plurals/src/rules/ast.rs @@ -38,7 +38,9 @@ //! [`PluralCategory`]: crate::PluralCategory //! [`parse`]: super::parse() //! [`test_condition`]: super::test_condition() -use std::ops::RangeInclusive; +use alloc::boxed::Box; +use alloc::string::String; +use core::ops::RangeInclusive; /// A complete AST representation of a plural rule. /// Comprises a vector of [`AndConditions`] and optionally a set of [`Samples`]. diff --git a/components/plurals/src/rules/lexer.rs b/components/plurals/src/rules/lexer.rs index 4277cf5b11a..4ab4f668bb7 100644 --- a/components/plurals/src/rules/lexer.rs +++ b/components/plurals/src/rules/lexer.rs @@ -34,6 +34,7 @@ pub enum LexerError { UnknownToken(u8), } +#[cfg(feature = "std")] impl std::error::Error for LexerError {} /// Unicode Plural Rule lexer is an iterator diff --git a/components/plurals/src/rules/parser.rs b/components/plurals/src/rules/parser.rs index 9c29b95432a..aebe89fa8f5 100644 --- a/components/plurals/src/rules/parser.rs +++ b/components/plurals/src/rules/parser.rs @@ -4,8 +4,12 @@ use super::ast; use super::lexer::{Lexer, Token}; +use alloc::string::String; +use alloc::string::ToString; +use alloc::vec; +use alloc::vec::Vec; +use core::iter::Peekable; use displaydoc::Display; -use std::iter::Peekable; #[derive(Display, Debug, PartialEq, Eq)] pub enum ParserError { @@ -23,6 +27,7 @@ pub enum ParserError { ExpectedSampleType, } +#[cfg(feature = "std")] impl std::error::Error for ParserError {} /// Unicode Plural Rule parser converts an diff --git a/components/plurals/src/rules/serializer.rs b/components/plurals/src/rules/serializer.rs index fab3f7c6271..9d6334f7cf4 100644 --- a/components/plurals/src/rules/serializer.rs +++ b/components/plurals/src/rules/serializer.rs @@ -3,8 +3,8 @@ // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). use crate::rules::ast; -use std::fmt; -use std::ops::RangeInclusive; +use core::fmt; +use core::ops::RangeInclusive; /// Unicode Plural Rule serializer converts an [`AST`] to a [`String`]. /// diff --git a/ffi/ecma402/Cargo.toml b/ffi/ecma402/Cargo.toml index 056d20fa7dd..a49bebfd2a3 100644 --- a/ffi/ecma402/Cargo.toml +++ b/ffi/ecma402/Cargo.toml @@ -29,6 +29,7 @@ all-features = true ecma402_traits = { version = "0.2.0" } icu = { path = "../../components/icu", default-features = false } icu_provider = { version = "0.2", path = "../../provider/core" } +icu_plurals = { version = "0.2", path = "../../components/plurals", features = ["std"] } [dev-dependencies]