-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created 'i18n_icu' crate to improve sharing of ICU4X data via the 'Da…
…taProvider' type. Updated 'i18n_lexer' and 'i18n_pattern' crates to use the 'IcuDataProvider' type.
- Loading branch information
1 parent
3c7611b
commit 86b2f11
Showing
26 changed files
with
595 additions
and
449 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.