Skip to content

Commit 01c8b8b

Browse files
authored
Merge pull request #135 from greyblake/serde
Serde
2 parents 0c03d28 + 7a3b93c commit 01c8b8b

File tree

9 files changed

+51
-22
lines changed

9 files changed

+51
-22
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ jobs:
4242
command: test
4343
args: --features arbitrary
4444

45+
- name: cargo test --all-features
46+
uses: actions-rs/cargo@v1
47+
with:
48+
command: test
49+
args: --all-features
50+
4551
rustfmt:
4652
name: Rustfmt
4753
runs-on: ubuntu-latest

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### Next
2+
* Add `serde` feature (`Serialize` and `Deserialize` for `Lang` and `Script`).
3+
14
### v0.16.2 - 2022-10-23
25
* Support [Arbitrary](https://crates.io/crates/arbitrary)
36

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ include = [
2222
hashbrown = "0.12.0"
2323
once_cell = "1.10.0"
2424
enum-map = { version = "2", optional = true }
25+
serde = { version = "1", optional = true, features = ["derive"] }
2526
arbitrary = { version = "1", optional = true, features = ["derive"] }
2627

2728
[dev-dependencies]

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ You're gonna be in a great company using Whatlang:
7272
| Feature | Description |
7373
|-------------|---------------------------------------------------------------------------------------|
7474
| `enum-map` | `Lang` and `Script` implement `Enum` trait from [enum-map](https://docs.rs/enum-map/) |
75-
| `arbitrary` | Support [Arbitrary](https://crates.io/crates/arbitrary) |
75+
| `arbitrary` | Support [Arbitrary](https://crates.io/crates/arbitrary) |
76+
| `serde` | Implements `Serialize` and `Deserialize` for `Lang` and `Script` |
7677
| `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. |
7778

7879
## How does it work?

src/core/filter_list.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::Lang;
22

33
#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
4-
#[derive(Debug, Clone)]
4+
#[derive(Debug, Clone, Default)]
55
pub enum FilterList {
6+
#[default]
67
All,
78
Allow(Vec<Lang>),
89
Deny(Vec<Lang>),
@@ -18,25 +19,19 @@ impl FilterList {
1819
Self::Allow(allowlist)
1920
}
2021

21-
pub fn deny(blacklist: Vec<Lang>) -> Self {
22-
Self::Deny(blacklist)
22+
pub fn deny(denylist: Vec<Lang>) -> Self {
23+
Self::Deny(denylist)
2324
}
2425

2526
pub fn is_allowed(&self, lang: Lang) -> bool {
2627
match self {
2728
Self::All => true,
2829
Self::Allow(ref allowlist) => allowlist.contains(&lang),
29-
Self::Deny(ref blacklist) => !blacklist.contains(&lang),
30+
Self::Deny(ref denylist) => !denylist.contains(&lang),
3031
}
3132
}
3233
}
3334

34-
impl Default for FilterList {
35-
fn default() -> Self {
36-
FilterList::All
37-
}
38-
}
39-
4035
#[cfg(test)]
4136
mod tests {
4237
use super::*;

src/core/method.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ use std::fmt;
33
use std::str::FromStr;
44

55
#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
6-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
77
pub enum Method {
88
Trigram,
99
Alphabet,
10+
#[default]
1011
Combined,
1112
}
1213

@@ -34,12 +35,6 @@ impl fmt::Display for Method {
3435
}
3536
}
3637

37-
impl Default for Method {
38-
fn default() -> Self {
39-
Method::Combined
40-
}
41-
}
42-
4338
#[cfg(test)]
4439
mod tests {
4540
use super::*;

src/lang.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ use crate::error::ParseError;
1010
/// Represents a language following [ISO 639-3](https://en.wikipedia.org/wiki/ISO_639-3) standard.
1111
#[cfg_attr(feature = "enum-map", derive(::enum_map::Enum))]
1212
#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
13+
#[cfg_attr(
14+
feature = "serde",
15+
derive(::serde::Serialize, ::serde::Deserialize),
16+
serde(rename_all = "lowercase")
17+
)]
1318
#[derive(PartialEq, Eq, Debug, Hash, Clone, Copy)]
1419
pub enum Lang {
1520
/// Esperanto (Esperanto)
@@ -721,4 +726,14 @@ mod tests {
721726
assert_eq!(Lang::Deu.to_string(), "Deutsch");
722727
assert_eq!(Lang::Eng.to_string(), "English");
723728
}
729+
730+
#[cfg(feature = "serde")]
731+
#[test]
732+
fn test_serialize_and_deserialize() {
733+
let langs = vec![Lang::Epo, Lang::Ukr, Lang::Spa];
734+
let json_langs = serde_json::to_string(&langs).unwrap();
735+
assert_eq!(json_langs, r#"["epo","ukr","spa"]"#);
736+
let parsed_langs: Vec<Lang> = serde_json::from_str(&json_langs).unwrap();
737+
assert_eq!(parsed_langs, langs);
738+
}
724739
}

src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@
3434
//!
3535
//! # Features
3636
//!
37-
//! | Feature | Description |
38-
//! |------------|---------------------------------------------------------------------------------------|
39-
//! | `enum-map` | `Lang` and `Script` implement `Enum` trait from [enum-map](https://docs.rs/enum-map/) |
40-
//!
37+
//! | Feature | Description |
38+
//! |-------------|---------------------------------------------------------------------------------------|
39+
//! | `enum-map` | `Lang` and `Script` implement `Enum` trait from [enum-map](https://docs.rs/enum-map/) |
40+
//! | `arbitrary` | Support [Arbitrary](https://crates.io/crates/arbitrary) |
41+
//! | `serde` | Implements `Serialize` and `Deserialize` for `Lang` and `Script` |
42+
//! | `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. |
4143
//!
4244
mod alphabets;
4345
mod combined;

src/scripts/script.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::Lang;
88
/// Represents a writing system (Latin, Cyrillic, Arabic, etc).
99
#[cfg_attr(feature = "enum-map", derive(::enum_map::Enum))]
1010
#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
11+
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
1112
#[derive(PartialEq, Eq, Debug, Clone, Copy, Hash)]
1213
pub enum Script {
1314
// Keep this in alphabetic order (for C bindings)
@@ -205,4 +206,14 @@ mod tests {
205206
assert_eq!(Script::Cyrillic.to_string(), "Cyrillic");
206207
assert_eq!(Script::Arabic.to_string(), "Arabic");
207208
}
209+
210+
#[cfg(feature = "serde")]
211+
#[test]
212+
fn test_serialize_and_deserialize() {
213+
let scripts = vec![Script::Georgian, Script::Cyrillic];
214+
let json_scripts = serde_json::to_string(&scripts).unwrap();
215+
assert_eq!(json_scripts, r#"["Georgian","Cyrillic"]"#);
216+
let parsed_scripts: Vec<Script> = serde_json::from_str(&json_scripts).unwrap();
217+
assert_eq!(parsed_scripts, scripts);
218+
}
208219
}

0 commit comments

Comments
 (0)