Skip to content

Commit

Permalink
Moving data provider structs into their own crates (#451)
Browse files Browse the repository at this point in the history
Adds "provider_serde" feature across components
  • Loading branch information
sffc authored Jan 29, 2021
1 parent de60d16 commit d2f47f7
Show file tree
Hide file tree
Showing 46 changed files with 499 additions and 368 deletions.
24 changes: 6 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion components/datetime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ include = [
"README.md"
]

[package.metadata.cargo-all-features]
skip_optional_dependencies = true

[dependencies]
icu_locid = { version = "0.1", path = "../locid" }
icu_provider = { version = "0.1", path = "../provider" }
writeable = { version = "0.2", path = "../../utils/writeable" }
serde = { version = "1.0", features = ["derive"], optional = true }

[dev-dependencies]
criterion = "0.3"
Expand All @@ -36,14 +40,25 @@ serde_json = "1.0"
bench = false # This option is required for Benchmark CI

[features]
default = []
default = ["provider_serde"]
bench = []
provider_serde = ["serde"]
serialize_none = []

[[bench]]
name = "datetime"
harness = false
required-features = ["provider_serde"]

[[bench]]
name = "pattern"
harness = false
required-features = ["bench"]

[[test]]
name = "datetime"
required-features = ["provider_serde"]

[[example]]
name = "work_log"
required-features = ["provider_serde"]
8 changes: 4 additions & 4 deletions components/datetime/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::date::{self, DateTimeType};
use crate::error::DateTimeFormatError;
use crate::fields::{self, FieldLength, FieldSymbol};
use crate::pattern::{Pattern, PatternItem};
use crate::provider::DateTimeDates;
use icu_provider::structs;
use crate::provider;
use crate::provider::helpers::DateTimeDates;
use std::fmt;
use writeable::Writeable;

Expand Down Expand Up @@ -42,7 +42,7 @@ where
T: DateTimeType,
{
pub(crate) pattern: &'l Pattern,
pub(crate) data: &'l structs::dates::gregory::DatesV1,
pub(crate) data: &'l provider::gregory::DatesV1,
pub(crate) date_time: &'l T,
}

Expand Down Expand Up @@ -98,7 +98,7 @@ fn get_day_of_week(year: usize, month: date::Month, day: date::Day) -> date::Wee

pub fn write_pattern<T, W>(
pattern: &crate::pattern::Pattern,
data: &structs::dates::gregory::DatesV1,
data: &provider::gregory::DatesV1,
date_time: &T,
w: &mut W,
) -> Result<(), DateTimeFormatError>
Expand Down
16 changes: 10 additions & 6 deletions components/datetime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/master/LICENSE ).

//! `icu_datetime` is one of the [`ICU4X`] components.
//!
//! This API provides necessary functionality for formatting date and time to user readable textual representation.
Expand All @@ -12,6 +13,7 @@
//! # Examples
//!
//! ```
//! # #[cfg(feature = "provider_serde")] {
//! use icu_locid_macros::langid;
//! use icu_datetime::{DateTimeFormat, DateTimeFormatOptions, date::MockDateTime, options::style};
//!
Expand All @@ -35,12 +37,14 @@
//!
//! let formatted_date = dtf.format(&date);
//! assert_eq!(formatted_date.to_string(), "Sep 12, 2020, 12:35 PM");
//! # } // feature = "provider_serde"
//! ```
//!
//! The options can be created more ergonomically using the `Into` trait to automatically
//! convert a [`options::style::Bag`] into a [`DateTimeFormatOptions::Style`].
//!
//! ```
//! # #[cfg(feature = "provider_serde")] {
//! # use icu_locid_macros::langid;
//! # use icu_datetime::{DateTimeFormat, DateTimeFormatOptions, date::MockDateTime, options::style};
//! # let provider = icu_testdata::get_provider();
Expand All @@ -52,6 +56,7 @@
//! }.into();
//!
//! let dtf = DateTimeFormat::try_new(lid, &provider, &options);
//! # } // feature = "provider_serde"
//! ```
//!
//! At the moment, the crate provides only options using the [`Style`] bag, but in the future,
Expand All @@ -73,19 +78,18 @@ mod format;
pub mod options;
#[doc(hidden)]
pub mod pattern;
mod provider;
pub mod provider;

use crate::provider::helpers::DateTimeDates;
use date::DateTimeType;
pub use error::DateTimeFormatError;
use format::write_pattern;
pub use format::FormattedDateTime;
use icu_locid::LanguageIdentifier;
use icu_provider::prelude::*;
use icu_provider::structs;
#[doc(inline)]
pub use options::DateTimeFormatOptions;
use pattern::Pattern;
use provider::DateTimeDates;
use std::borrow::Cow;

/// `DateTimeFormat` is the main structure of the `icu_datetime` component.
Expand Down Expand Up @@ -127,7 +131,7 @@ use std::borrow::Cow;
pub struct DateTimeFormat<'d> {
_langid: LanguageIdentifier,
pattern: Pattern,
data: Cow<'d, structs::dates::gregory::DatesV1>,
data: Cow<'d, provider::gregory::DatesV1>,
}

impl<'d> DateTimeFormat<'d> {
Expand All @@ -152,15 +156,15 @@ impl<'d> DateTimeFormat<'d> {
///
/// assert_eq!(dtf.is_ok(), true);
/// ```
pub fn try_new<D: DataProvider<'d, structs::dates::gregory::DatesV1> + ?Sized>(
pub fn try_new<D: DataProvider<'d, provider::gregory::DatesV1> + ?Sized>(
langid: LanguageIdentifier,
data_provider: &D,
options: &DateTimeFormatOptions,
) -> Result<Self, DateTimeFormatError> {
let data = data_provider
.load_payload(&DataRequest {
resource_path: ResourcePath {
key: structs::dates::key::GREGORY_V1,
key: provider::key::GREGORY_V1,
options: ResourceOptions {
variant: None,
langid: Some(langid.clone()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::error::DateTimeFormatError;
use crate::fields;
use crate::options::{style, DateTimeFormatOptions};
use crate::pattern::Pattern;
use icu_provider::structs;
use crate::provider;
use std::borrow::Cow;

type Result<T> = std::result::Result<T, DateTimeFormatError>;
Expand Down Expand Up @@ -42,7 +42,7 @@ pub trait DateTimeDates {
) -> &Cow<str>;
}

impl DateTimeDates for structs::dates::gregory::DatesV1 {
impl DateTimeDates for provider::gregory::DatesV1 {
fn get_pattern_for_options(&self, options: &DateTimeFormatOptions) -> Result<Option<Pattern>> {
match options {
DateTimeFormatOptions::Style(bag) => self.get_pattern_for_style_bag(bag),
Expand Down
Loading

0 comments on commit d2f47f7

Please sign in to comment.