diff --git a/Cargo.lock b/Cargo.lock index 8cd1fba..1352f77 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,6 +26,7 @@ dependencies = [ "jlabel", "regex-automata", "regex-syntax", + "serde", "thiserror", ] diff --git a/crates/jlabel-question/Cargo.toml b/crates/jlabel-question/Cargo.toml index 185e523..66b8fb6 100644 --- a/crates/jlabel-question/Cargo.toml +++ b/crates/jlabel-question/Cargo.toml @@ -18,6 +18,7 @@ rustdoc-args = ["--cfg", "docsrs"] [features] regex = ["dep:regex-automata", "dep:regex-syntax"] +serde = ["dep:serde"] [dependencies] jlabel.workspace = true @@ -25,3 +26,4 @@ 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 } diff --git a/crates/jlabel-question/README.md b/crates/jlabel-question/README.md index 073273a..1e83710 100644 --- a/crates/jlabel-question/README.md +++ b/crates/jlabel-question/README.md @@ -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 diff --git a/crates/jlabel-question/src/lib.rs b/crates/jlabel-question/src/lib.rs index d8a6bdd..f7e2964 100644 --- a/crates/jlabel-question/src/lib.rs +++ b/crates/jlabel-question/src/lib.rs @@ -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 { @@ -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), @@ -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 { /// 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, } diff --git a/crates/jlabel-question/src/position.rs b/crates/jlabel-question/src/position.rs index 0042fd3..859af52 100644 --- a/crates/jlabel-question/src/position.rs +++ b/crates/jlabel-question/src/position.rs @@ -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 { @@ -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, @@ -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, @@ -133,6 +138,7 @@ fn range_i8>(s: S) -> Result, 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, @@ -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, @@ -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, @@ -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,