Skip to content

Commit

Permalink
Support serde in question (#29)
Browse files Browse the repository at this point in the history
* support serde in question

* add Note to readme

* wording
  • Loading branch information
femshima committed Mar 24, 2024
1 parent e17b3c4 commit 0170f59
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/jlabel-question/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ rustdoc-args = ["--cfg", "docsrs"]

[features]
regex = ["dep:regex-automata", "dep:regex-syntax"]
serde = ["dep:serde"]

[dependencies]
jlabel.workspace = true
thiserror.workspace = true

regex-automata = { version = "0.4.4", optional = true }
regex-syntax = { version = "0.8.2", optional = true }
serde = { version = "1", features = ["derive"], optional = true }
2 changes: 2 additions & 0 deletions crates/jlabel-question/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Parser and matcher of htsvoice full-context-label questions.

**Note: `serde` support is experimental. Changes may not be treated as breaking.**

## Usage

Put the following in Cargo.toml
Expand Down
12 changes: 12 additions & 0 deletions crates/jlabel-question/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ use position::{
use jlabel::Label;
use parse_position::{estimate_position, PositionError};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Errors from jlabel-question.
#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)]
pub enum ParseError {
Expand Down Expand Up @@ -173,6 +176,7 @@ where

/// A main structure representing question.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum AllQuestion {
/// Question about phone fields of full-context label
Phone(Question<PhonePosition>),
Expand Down Expand Up @@ -236,10 +240,18 @@ impl QuestionMatcher for AllQuestion {
///
/// Used in variants of [`AllQuestion`]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Question<P: Position> {
/// The position this question matches to.
pub position: P,
/// The parsed range
#[cfg_attr(
feature = "serde",
serde(bound(
serialize = "P::Range: Serialize",
deserialize = "P::Range: Deserialize<'de>"
))
)]
pub range: Option<P::Range>,
}

Expand Down
9 changes: 9 additions & 0 deletions crates/jlabel-question/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use crate::Label;

use super::ParseError;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Enum that represent all positions
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AllPosition {
Expand Down Expand Up @@ -52,6 +55,7 @@ pub trait Position {

/// Positions of phone fields
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[allow(missing_docs)]
pub enum PhonePosition {
P1,
Expand Down Expand Up @@ -86,6 +90,7 @@ impl Position for PhonePosition {

/// Positions with signed integer type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[allow(missing_docs)]
pub enum SignedRangePosition {
A1,
Expand Down Expand Up @@ -133,6 +138,7 @@ fn range_i8<S: AsRef<str>>(s: S) -> Result<Range<i8>, ParseError> {

/// Positions with unsigned integer type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[allow(missing_docs)]
pub enum UnsignedRangePosition {
A2,
Expand Down Expand Up @@ -256,6 +262,7 @@ where

/// Positions with boolean type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[allow(missing_docs)]
pub enum BooleanPosition {
E3,
Expand Down Expand Up @@ -299,6 +306,7 @@ impl Position for BooleanPosition {

/// Positions with numerical representations of categorical value
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[allow(missing_docs)]
pub enum CategoryPosition {
B1,
Expand Down Expand Up @@ -344,6 +352,7 @@ impl Position for CategoryPosition {

/// Positions that are always `xx`
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[allow(missing_docs)]
pub enum UndefinedPotision {
E4,
Expand Down

0 comments on commit 0170f59

Please sign in to comment.