Skip to content

Commit

Permalink
Merge pull request #16 from sancane/error_code
Browse files Browse the repository at this point in the history
Error code
  • Loading branch information
sancane authored Dec 4, 2021
2 parents 50b2b7b + ec29c47 commit bf76b1c
Show file tree
Hide file tree
Showing 21 changed files with 1,002 additions and 871 deletions.
6 changes: 3 additions & 3 deletions precis-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "precis-core"
version = "0.1.3"
version = "0.1.4"
authors = ["Santiago Carot-Nemesio <[email protected]>"]
description = """
PRECIS Framework: Preparation, Enforcement, and Comparison of
Expand All @@ -17,11 +17,11 @@ categories = ["text-processing", "internationalization"]
edition = "2018"

[dev-dependencies]
precis-tools = { path = "../precis-tools", version = "0.1.3" }
precis-tools = { path = "../precis-tools", version = "0.1.4" }
ucd-parse = "0.1.8"

[build-dependencies]
precis-tools = { path = "../precis-tools", version = "0.1.3" }
precis-tools = { path = "../precis-tools", version = "0.1.4" }
ucd-parse = "0.1.8"

[dependencies]
Expand Down
8 changes: 4 additions & 4 deletions precis-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

PRECIS Framework: Preparation, Enforcement, and Comparison of
Internationalized Strings in Application Protocols as described in
[rfc8264](https://datatracker.ietf.org/doc/html/rfc8264)
[`rfc8264`](https://datatracker.ietf.org/doc/html/rfc8264)

This crate implements the PRECIS base string classes and tables
that profiles can use for their implementation. The crate `precis-profiles`
Expand All @@ -17,7 +17,7 @@ to your `Cargo.toml`:

```toml
[dependencies]
precis-core = "0.1.3"
precis-core = "0.1.4"
```

# Documentation
Expand All @@ -26,5 +26,5 @@ https://docs.rs/precis-core
# License

This project is licensed under either of
* Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
* MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT) at your option.
* [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0)
* [MIT license](https://opensource.org/licenses/MIT)
140 changes: 76 additions & 64 deletions precis-core/src/context.rs

Large diffs are not rendered by default.

59 changes: 51 additions & 8 deletions precis-core/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,57 @@
use crate::DerivedPropertyValue;

/// Represents any kind of error that may happen when
/// preparing, enforcing or comparing of internationalized
/// preparing, enforcing or comparing internationalized
/// strings
#[derive(Debug, PartialEq)]
pub enum Error {
/// Operation disallowed
Disallowed,
/// Operation not applicable
NotApplicable,
/// Undefined error
/// Invalid label
Invalid,
/// Detected a disallowed Unicode code pint in the label.
/// [`CodepointInfo`] contains information about the code point.
BadCodepoint(CodepointInfo),
/// Error used to deal with any unexpected condition not directly
/// covered by any other category.
Unexpected(UnexpectedError),
}

/// Error that contains information regarding the wrong Unicode code point
#[derive(Debug, PartialEq)]
pub struct CodepointInfo {
/// Unicode code point
pub cp: u32,
/// The position of the Unicode code point in the label
pub position: usize,
/// The derived property value
pub property: DerivedPropertyValue,
}

impl CodepointInfo {
/// Creates a new `CodepointInfo` `struct`
pub fn new(cp: u32, position: usize, property: DerivedPropertyValue) -> Self {
Self {
cp,
position,
property,
}
}
}

/// Internal errors that group unusual error conditions that mostly
/// have to do with the processing of wrong labels, unexpected Unicode
/// code points if tested against another version defined in PRECIS, etc.
#[derive(Debug, PartialEq)]
pub enum UnexpectedError {
/// Error caused when trying to apply a context rule over
/// an invalid code point.
ContextRuleNotApplicable(CodepointInfo),
/// The code point requires a context rule that is not implemented.
/// [`CodepointInfo`] contains information about the code point.
MissingContextRule(CodepointInfo),
/// Error caused when trying to apply a context rule that is not defined
/// by the PRECIS profile.
ProfileRuleNotApplicable,
/// Unexpected error condition such as an attempt to access to a character before
/// the start of a label or after the end of a label.
Undefined,
/// Unexpected error
Unexpected,
}
4 changes: 3 additions & 1 deletion precis-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//!
//! ```toml
//! [dependencies]
//! precis-core = "0.1.3"
//! precis-core = "0.1.4"
//! ```
#![deny(missing_docs)]
Expand All @@ -24,7 +24,9 @@ mod common;

pub mod context;

pub use crate::error::CodepointInfo;
pub use crate::error::Error;
pub use crate::error::UnexpectedError;
pub use crate::stringclasses::FreeformClass;
pub use crate::stringclasses::IdentifierClass;
pub use crate::stringclasses::StringClass;
Expand Down
14 changes: 7 additions & 7 deletions precis-core/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! must implement such as it is defined by the PRECIS framework
//! [`rfc8264`](https://datatracker.ietf.org/doc/html/rfc8264#section-5)
use crate::Error;
use crate::{Error, UnexpectedError};
use std::borrow::Cow;

/// Rules that any profile of a PRECIS string class MUST define
Expand All @@ -22,7 +22,7 @@ pub trait Rules {
where
T: Into<Cow<'a, str>>,
{
Err(Error::NotApplicable)
Err(Error::Unexpected(UnexpectedError::ProfileRuleNotApplicable))
}

/// Applies the additional mapping rule of a profile to an input string.
Expand All @@ -35,7 +35,7 @@ pub trait Rules {
where
T: Into<Cow<'a, str>>,
{
Err(Error::NotApplicable)
Err(Error::Unexpected(UnexpectedError::ProfileRuleNotApplicable))
}

/// Applies the case mapping rule of a profile to an input string
Expand All @@ -48,7 +48,7 @@ pub trait Rules {
where
T: Into<Cow<'a, str>>,
{
Err(Error::NotApplicable)
Err(Error::Unexpected(UnexpectedError::ProfileRuleNotApplicable))
}

/// Applies the normalization rule of a profile to an input string
Expand All @@ -61,7 +61,7 @@ pub trait Rules {
where
T: Into<Cow<'a, str>>,
{
Err(Error::NotApplicable)
Err(Error::Unexpected(UnexpectedError::ProfileRuleNotApplicable))
}

/// Applies the directionality rule of a profile to an input string
Expand All @@ -74,7 +74,7 @@ pub trait Rules {
where
T: Into<Cow<'a, str>>,
{
Err(Error::NotApplicable)
Err(Error::Unexpected(UnexpectedError::ProfileRuleNotApplicable))
}
}

Expand Down Expand Up @@ -176,5 +176,5 @@ where
}

// The string did not stabilized after applying the rules three times.
Err(Error::Disallowed)
Err(Error::Invalid)
}
Loading

0 comments on commit bf76b1c

Please sign in to comment.