Skip to content

Commit

Permalink
Created 'i18n_icu' crate to improve sharing of ICU4X data via the 'Da…
Browse files Browse the repository at this point in the history
…taProvider' type. Updated 'i18n_lexer' and 'i18n_pattern' crates to use the 'IcuDataProvider' type.
  • Loading branch information
rizzen-yazston committed Mar 20, 2023
1 parent 3c7611b commit 86b2f11
Show file tree
Hide file tree
Showing 26 changed files with 595 additions and 449 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
[workspace]
resolver = "2"
members = [
"crates/icu",
"crates/lstring",
"crates/lexer",
"crates/registry",
"crates/provider/core",
"crates/provider/sqlite3",
"crates/pattern",
# "crates/message",
"crates/message",
]

exclude = [
Expand Down
17 changes: 8 additions & 9 deletions crates/i18n/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[package]
# suffix '-rizzen-yazston' appended to prevent package name clashes on 'crates.io'
name = "i18n-rizzen-yazston"
version = "0.4.0"
version = "0.5.0"
authors = ["Rizzen Yazston"]
edition = "2021"
rust-version = "1.67.0"
Expand All @@ -27,14 +27,13 @@ publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# Always exclude these crates:
# - i18n_utility-rizzen-yazston

i18n_lstring-rizzen-yazston = { version = "0.4.0", path = "../lstring" }
i18n_lexer-rizzen-yazston = { version = "0.4.0", path = "../lexer" }
i18n_pattern-rizzen-yazston = { version = "0.4.0", path = "../pattern" }
#i18n_provider-rizzen-yazston = { version = "0.0.0", path = "../provider" }
#i18n_message-rizzen-yazston = { version = "0.0.0", path = "../message" }
i18n_lexer-rizzen-yazston = { version = "0.5.0", path = "../lexer" }
i18n_lstring-rizzen-yazston = { version = "0.5.0", path = "../lstring" }
i18n_message-rizzen-yazston = { version = "0.5.0", path = "../message" }
i18n_pattern-rizzen-yazston = { version = "0.5.0", path = "../pattern" }
i18n_provider-rizzen-yazston = { version = "0.5.0", path = "../provider/core" }
i18n_provider_sqlite3-rizzen-yazston = { version = "0.5.0", path = "../provider/sqlite3" }
i18n_registry-rizzen-yazston = { version = "0.5.0", path = "../registry" }

[lib]
name = "i18n"
15 changes: 11 additions & 4 deletions crates/i18n/README.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@ Rizzen Yazston

This convenience meta crate contains selected available crates:

- i18n_lstring-rizzen-yazston
- i18n_icu-rizzen-yazston
- i18n_lexer-rizzen-yazston
- i18n_pattern-rizzen-yazston
- i18n_lstring-rizzen-yazston
- i18n_message-rizzen-yazston
- i18n_pattern-rizzen-yazston
- i18n_provider-rizzen-yazston
- i18n_provider_sqlite3-rizzen-yazston
- i18n_registry-rizzen-yazston
== Usage

For most use cases, just the use of `i18n-rizzen-yazston` crate will be sufficient to use the multilingual message system, though the individual crates can be selected individual if the entire `i18n` project is not required.
Expand All @@ -21,8 +29,7 @@ For most use cases, just the use of `i18n-rizzen-yazston` crate will be sufficie

```
[dependencies]
icu_locid = "1.0.0"
i18n-rizzen-yazston = "0.4.0"
i18n-rizzen-yazston = "0.5.0"
```

=== Examples
Expand Down
20 changes: 14 additions & 6 deletions crates/i18n/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
//!
//! This convenience meta crate contains selected available crates:
//!
//! * i18n_lstring-rizzen-yazston
//!
//! * i18n_lexer-rizzen-yazston
//!
//! * i18n_lstring-rizzen-yazston
//!
//! * i18n_pattern-rizzen-yazston
//!
//! * i18n_message-rizzen-yazston
//!
//! * i18n_provider-rizzen-yazston
//!
//! * i18n_provider_sqlite3-rizzen-yazston
//!
//! * i18n_registry-rizzen-yazston
//!
//! # Usage
//!
//! For most use cases, just the use of `i18n-rizzen-yazston` crate will be sufficient to use the multilingual message
Expand All @@ -22,15 +28,17 @@
//!
//! ```
//! [dependencies]
//! icu_locid = "1.0.0"
//! i18n-rizzen-yazston = "0.4.0"
//! i18n-rizzen-yazston = "0.5.0"
//! ```
//!
//! ## Examples
//!
//! See the various component crates for usage examples.

pub use i18n_lexer-rizzen-yazston as lexer;
pub use i18n_lstring-rizzen-yazston as lstring;
pub use i18n_lexer-rizzen-yazston as pattern;
pub use i18n_pattern-rizzen-yazston as pattern;
pub use i18n_message-rizzen-yazston as message;
pub use i18n_pattern-rizzen-yazston as pattern;
pub use i18n_provider-rizzen-yazston as provider;
pub use i18n_provider_sqlite3-rizzen-yazston as provider_sqlite3;
pub use i18n_registry-rizzen-yazston as registry;
47 changes: 47 additions & 0 deletions crates/icu/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# This file is part of `i18n_icu-rizzen-yazston` crate. For the terms of use, please see the file
# called `LICENSE-BSD-3-Clause` at the top level of the `i18n_icu-rizzen-yazston` crate.

[package]
# suffix '-rizzen-yazston' appended to prevent package name clashes on 'crates.io'
name = "i18n_icu-rizzen-yazston"
version = "0.5.0"
authors = ["Rizzen Yazston"]
edition = "2021"
rust-version = "1.67.0"
description = "The `i18n_icu` crate of the Internationalisation project."
license = "BSD-3-Clause"
readme = "README.asciidoc"
# false prevents publishing while developing, else string of repository identfier
publish = false

# these keys are used when preparing to publish to 'crates.io'
#documentation = "<url, default is docs.rs>"
#homepage = "<url"
# once public GitHub repository is used
#repository = "<github url>"
#'crates.io' has max 5 keywords, of max 20 characters [A-Za-z0-9_-]
#keywords = ["",...]
#'crates.io' has max 5 categories, selected from `crates.io` list
#categories = ["internationalization"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[dependencies.icu_provider]
version = "1.1.0"
# Needed for BufferProvider used in i18n_lexer.
features = [ "serde", "deserialize_bincode_1" ]

[dependencies.icu_properties]
version = "1.1.0"
# Needed for BufferProvider
features = [ "serde" ]

[dependencies.icu_segmenter]
version = "0.8.0"
# Needed for BufferProvider
features = [ "serde" ]

[lib]
name = "i18n_icu"
11 changes: 11 additions & 0 deletions crates/icu/LICENSE-BSD-3-Clause
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Copyright 2021 Rizzen Yazston

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 changes: 26 additions & 0 deletions crates/icu/README.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
= i18n_icu
Rizzen Yazston
:Locale: https://docs.rs/icu/latest/icu/locid/struct.Locale.html
:icu_locid: https://crates.io/crates/icu_locid
:icu: https://crates.io/crates/icu
:ICU4X: https://github.com/unicode-org/icu4x
:Unicode Consortium: https://home.unicode.org/
:LanguageIdentifier: https://docs.rs/icu/latest/icu/locid/struct.LanguageIdentifier.html
:BCP_47_Language_Tag: https://www.rfc-editor.org/rfc/bcp/bcp47.txt

ICU4X data provider helper.

The `IcuDataProvider` type contains a member `data_provider` holding the `DataProvider`, which is a deserialised `BufferProvider`.

The `IcuDataProvider` type also contains non-locale based data used within the `i18n_lexer` crate.

== Cargo.toml

```
[dependencies]
i18n_icu-rizzen-yazston = "0.5.0"
```

== Examples

See various examples of `i18n_lexer`, `i18n_pattern`, and `i18n_message` crates.
40 changes: 40 additions & 0 deletions crates/icu/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// This file is part of `i18n_icu-rizzen-yazston` crate. For the terms of use, please see the file
// called `LICENSE-BSD-3-Clause` at the top level of the `i18n_icu-rizzen-yazston` crate.

use icu_properties::PropertiesError;
use icu_segmenter::SegmenterError;
use std::error::Error; // Experimental in `core` crate.
use core::fmt::{ Display, Formatter, Result };

#[derive( Debug )]
#[non_exhaustive]
pub enum IcuError {
Properties( PropertiesError ),
Segmenter( SegmenterError ),
}

impl Display for IcuError {

/// Simply call the display formatter of embedded error.
fn fmt( &self, formatter: &mut Formatter ) -> Result {
match *self {
IcuError::Properties( ref error ) => error.fmt( formatter ),
IcuError::Segmenter( ref error ) => error.fmt( formatter ),
}
}
}

// Source is embedded in the enum value.
impl Error for IcuError {}

impl From<PropertiesError> for IcuError {
fn from( error: PropertiesError ) -> IcuError {
IcuError::Properties( error )
}
}

impl From<SegmenterError> for IcuError {
fn from( error: SegmenterError ) -> IcuError {
IcuError::Segmenter( error )
}
}
67 changes: 67 additions & 0 deletions crates/icu/src/icu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// This file is part of `i18n_icu-rizzen-yazston` crate. For the terms of use, please see the file
// called `LICENSE-BSD-3-Clause` at the top level of the `i18n_icu-rizzen-yazston` crate.

use crate::IcuError;
use icu_provider::serde::DeserializingBufferProvider;
use icu_properties::sets::{ load_pattern_white_space, load_pattern_syntax, CodePointSetData };
use icu_segmenter::GraphemeClusterSegmenter;

/// The `IcuDataProvider` type contains a member `data_provider` holding the `DataProvider`, which is a deserialised
/// `BufferProvider`.
///
/// The `IcuDataProvider` type also contains non-locale based data used within the `i18n_lexer` crate.
///
/// `IcuDataProvider` type is used within the `Rc` type to prevent unnecessary duplication.
pub struct IcuDataProvider<'a, P>
where
P: ?Sized
{
data_provider: DeserializingBufferProvider<'a, P>,
pattern_syntax: CodePointSetData,
pattern_white_space: CodePointSetData,
grapheme_segmenter: GraphemeClusterSegmenter,
}

impl<'a, P> IcuDataProvider<'a, P>
where
P: ?Sized + icu_provider::BufferProvider
{

/// Create a `IcuDataProvider` object using the ICU's `DeserializingBufferProvider` as the `DataProvider`. Besides
/// storing the `DataProvider`, it also obtains and stores the Pattern_Syntax character set, the
/// Pattern_White_Space character set, and the Grapheme Cluster Segmenter required for the `Lexer` types to
/// function.
pub fn try_new( data_provider: DeserializingBufferProvider<'a, P> ) -> Result<Self, IcuError> {
let syntax = load_pattern_syntax( &data_provider )?;
let white_space = load_pattern_white_space( &data_provider )?;
let grapheme_segmenter =
GraphemeClusterSegmenter::try_new_unstable( &data_provider )?;
Ok( IcuDataProvider {
data_provider,
pattern_syntax: syntax,
pattern_white_space: white_space,
grapheme_segmenter,
} )
}

/// Get the `DataProvider` object that can be used in any ICU function that accepts a `DataProvider` as a
/// parameter.
pub fn data_provider( &self ) -> &DeserializingBufferProvider<P> {
&self.data_provider
}

/// Get the preloaded Pattern_Syntax character set.
pub fn pattern_syntax( &self ) -> &CodePointSetData {
&self.pattern_syntax
}

/// Get the preloaded Pattern_White_Space character set.
pub fn pattern_white_space( &self ) -> &CodePointSetData {
&self.pattern_white_space
}

/// Get the Grapheme Cluster Segmenter with preloaded character data set.
pub fn grapheme_segmenter( &self ) -> &GraphemeClusterSegmenter {
&self.grapheme_segmenter
}
}
18 changes: 18 additions & 0 deletions crates/icu/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// This file is part of `i18n_icu-rizzen-yazston` crate. For the terms of use, please see the file
// called LICENSE-BSD-3-Clause at the top level of the `i18n_icu-rizzen-yazston` crate.

//! ICU4X data provider helper.
//!
//! The `IcuDataProvider` type contains a member `data_provider` holding the `DataProvider`, which is a deserialised
//! `BufferProvider`.
//!
//! The `IcuDataProvider` type also contains non-locale based data used within the `i18n_lexer` crate.
//!
//! # Examples
//!
//! See various examples of `i18n_lexer`, `i18n_pattern`, and `i18n_message` crates.

pub mod icu;
pub use icu::*;
pub mod error;
pub use error::*;
1 change: 1 addition & 0 deletions crates/lexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
i18n_icu-rizzen-yazston = { version = "0.5.0", path = "../icu" }

[dependencies.icu_provider]
version = "1.1.0"
Expand Down
Loading

0 comments on commit 86b2f11

Please sign in to comment.