Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serde #135

Merged
merged 4 commits into from
Aug 16, 2023
Merged

Serde #135

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -42,6 +42,12 @@ jobs:
command: test
args: --features arbitrary

- name: cargo test --all-features
uses: actions-rs/cargo@v1
with:
command: test
args: --all-features

rustfmt:
name: Rustfmt
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,6 @@
### Next
* Add `serde` feature (`Serialize` and `Deserialize` for `Lang` and `Script`).

### v0.16.2 - 2022-10-23
* Support [Arbitrary](https://crates.io/crates/arbitrary)

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -22,6 +22,7 @@ include = [
hashbrown = "0.12.0"
once_cell = "1.10.0"
enum-map = { version = "2", optional = true }
serde = { version = "1", optional = true, features = ["derive"] }
arbitrary = { version = "1", optional = true, features = ["derive"] }

[dev-dependencies]
Expand Down
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -72,7 +72,8 @@ You're gonna be in a great company using Whatlang:
| Feature | Description |
|-------------|---------------------------------------------------------------------------------------|
| `enum-map` | `Lang` and `Script` implement `Enum` trait from [enum-map](https://docs.rs/enum-map/) |
| `arbitrary` | Support [Arbitrary](https://crates.io/crates/arbitrary) |
| `arbitrary` | Support [Arbitrary](https://crates.io/crates/arbitrary) |
| `serde` | Implements `Serialize` and `Deserialize` for `Lang` and `Script` |
| `dev` | Enables `whatlang::dev` module which provides some internal API.<br/> It exists for profiling purposes and normal users are discouraged to to rely on this API. |

## How does it work?
Expand Down
15 changes: 5 additions & 10 deletions src/core/filter_list.rs
@@ -1,8 +1,9 @@
use crate::Lang;

#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub enum FilterList {
#[default]
All,
Allow(Vec<Lang>),
Deny(Vec<Lang>),
Expand All @@ -18,25 +19,19 @@ impl FilterList {
Self::Allow(allowlist)
}

pub fn deny(blacklist: Vec<Lang>) -> Self {
Self::Deny(blacklist)
pub fn deny(denylist: Vec<Lang>) -> Self {
Self::Deny(denylist)
}

pub fn is_allowed(&self, lang: Lang) -> bool {
match self {
Self::All => true,
Self::Allow(ref allowlist) => allowlist.contains(&lang),
Self::Deny(ref blacklist) => !blacklist.contains(&lang),
Self::Deny(ref denylist) => !denylist.contains(&lang),
}
}
}

impl Default for FilterList {
fn default() -> Self {
FilterList::All
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
9 changes: 2 additions & 7 deletions src/core/method.rs
Expand Up @@ -3,10 +3,11 @@ use std::fmt;
use std::str::FromStr;

#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Method {
Trigram,
Alphabet,
#[default]
Combined,
}

Expand Down Expand Up @@ -34,12 +35,6 @@ impl fmt::Display for Method {
}
}

impl Default for Method {
fn default() -> Self {
Method::Combined
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
15 changes: 15 additions & 0 deletions src/lang.rs
Expand Up @@ -10,6 +10,11 @@ use crate::error::ParseError;
/// Represents a language following [ISO 639-3](https://en.wikipedia.org/wiki/ISO_639-3) standard.
#[cfg_attr(feature = "enum-map", derive(::enum_map::Enum))]
#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
#[cfg_attr(
feature = "serde",
derive(::serde::Serialize, ::serde::Deserialize),
serde(rename_all = "lowercase")
)]
#[derive(PartialEq, Eq, Debug, Hash, Clone, Copy)]
pub enum Lang {
/// Esperanto (Esperanto)
Expand Down Expand Up @@ -721,4 +726,14 @@ mod tests {
assert_eq!(Lang::Deu.to_string(), "Deutsch");
assert_eq!(Lang::Eng.to_string(), "English");
}

#[cfg(feature = "serde")]
#[test]
fn test_serialize_and_deserialize() {
let langs = vec![Lang::Epo, Lang::Ukr, Lang::Spa];
let json_langs = serde_json::to_string(&langs).unwrap();
assert_eq!(json_langs, r#"["epo","ukr","spa"]"#);
let parsed_langs: Vec<Lang> = serde_json::from_str(&json_langs).unwrap();
assert_eq!(parsed_langs, langs);
}
}
10 changes: 6 additions & 4 deletions src/lib.rs
Expand Up @@ -34,10 +34,12 @@
//!
//! # Features
//!
//! | Feature | Description |
//! |------------|---------------------------------------------------------------------------------------|
//! | `enum-map` | `Lang` and `Script` implement `Enum` trait from [enum-map](https://docs.rs/enum-map/) |
//!
//! | Feature | Description |
//! |-------------|---------------------------------------------------------------------------------------|
//! | `enum-map` | `Lang` and `Script` implement `Enum` trait from [enum-map](https://docs.rs/enum-map/) |
//! | `arbitrary` | Support [Arbitrary](https://crates.io/crates/arbitrary) |
//! | `serde` | Implements `Serialize` and `Deserialize` for `Lang` and `Script` |
//! | `dev` | Enables `whatlang::dev` module which provides some internal API.<br/> It exists for profiling purposes and normal users are discouraged to to rely on this API. |
//!
mod alphabets;
mod combined;
Expand Down
11 changes: 11 additions & 0 deletions src/scripts/script.rs
Expand Up @@ -8,6 +8,7 @@ use crate::Lang;
/// Represents a writing system (Latin, Cyrillic, Arabic, etc).
#[cfg_attr(feature = "enum-map", derive(::enum_map::Enum))]
#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
#[derive(PartialEq, Eq, Debug, Clone, Copy, Hash)]
pub enum Script {
// Keep this in alphabetic order (for C bindings)
Expand Down Expand Up @@ -205,4 +206,14 @@ mod tests {
assert_eq!(Script::Cyrillic.to_string(), "Cyrillic");
assert_eq!(Script::Arabic.to_string(), "Arabic");
}

#[cfg(feature = "serde")]
#[test]
fn test_serialize_and_deserialize() {
let scripts = vec![Script::Georgian, Script::Cyrillic];
let json_scripts = serde_json::to_string(&scripts).unwrap();
assert_eq!(json_scripts, r#"["Georgian","Cyrillic"]"#);
let parsed_scripts: Vec<Script> = serde_json::from_str(&json_scripts).unwrap();
assert_eq!(parsed_scripts, scripts);
}
}