Skip to content

Commit 9cf7544

Browse files
refactor(cmn): 🎨 refactoring code and removal of xtask
1 parent e371f8a commit 9cf7544

File tree

9 files changed

+226
-99
lines changed

9 files changed

+226
-99
lines changed

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ rustdoc-args = ["--generate-link-to-definition"]
7979

8080
## Warn
8181
# box_pointers = "warn"
82-
# missing_copy_implementations = "warn"
83-
# missing_docs = "warn"
84-
# unstable_features = "warn"
82+
missing_copy_implementations = "warn"
83+
missing_docs = "warn"
84+
unstable_features = "warn"
8585
# unused_crate_dependencies = "warn"
86-
# unused_extern_crates = "warn"
87-
# unused_results = "warn"
86+
unused_extern_crates = "warn"
87+
unused_results = "warn"
8888

8989
## Allow
9090
bare_trait_objects = "allow"

benches/criterion.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33

44
//! Benchmarks for the Common (CMN) library.
5+
56
#![allow(missing_docs)]
7+
68
use criterion::{
79
black_box, criterion_group, criterion_main, Criterion,
810
};
@@ -20,20 +22,22 @@ fn bench_cmn(c: &mut Criterion) {
2022
});
2123
});
2224
}
25+
2326
fn bench_words(c: &mut Criterion) {
2427
c.bench_function("Words::new", |b| {
2528
b.iter(|| {
2629
let words = black_box(Words::new());
2730
let _ = black_box(words.words_list());
2831
})
2932
});
33+
3034
c.bench_function("Words::default", |b| {
3135
b.iter(|| {
32-
let words = black_box(Words {});
36+
let words = black_box(Words::default());
3337
let _ = black_box(words.words_list());
3438
})
3539
});
3640
}
3741

3842
criterion_group!(benches, bench_cmn, bench_words);
39-
criterion_main!(benches);
43+
criterion_main!(benches);

src/constants.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright © 2023 Common (CMN) library. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33

4-
// Copyright © 2023 Common (CMN) library. All rights reserved.
5-
// SPDX-License-Identifier: Apache-2.0 OR MIT
6-
74
use serde::{Deserialize, Serialize};
85

96
/// Contains several commonly used mathematical and cryptographic constants.

src/lib.rs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,19 @@
7575
//! extern crate cmn;
7676
//! use cmn::Constants;
7777
//! use cmn::Words;
78+
//! use cmn::words::WORD_LIST;
7879
//!
7980
//! // Constants
8081
//! let constants = Constants::new();
8182
//! let constant = constants.constant("EULER");
8283
//! assert_eq!(constant.unwrap().name, "EULER");
8384
//!
8485
//! // Words
85-
//! let words = Words::new();
86+
//! let words = Words::default();
8687
//! let words_list = words.words_list();
87-
//! assert_eq!(words_list[0], "aboard");
88+
//! // Checking the first three elements to verify correct initialization and ordering
89+
//! assert_eq!(words_list.len(), WORD_LIST.len(), "Default words list length should match WORD_LIST length.");
90+
//! assert_eq!(words_list[0], "aboard", "Check that words list is sorted and starts with the first word.");
8891
//!
8992
//! ```
9093
//! ## License
@@ -110,6 +113,8 @@
110113
#![crate_name = "cmn"]
111114
#![crate_type = "lib"]
112115

116+
use std::collections::HashSet;
117+
113118
/// The `serde` crate provides the `Serialize` and `Deserialize` traits
114119
/// that are used to serialize and deserialize the data.
115120
use serde::{Deserialize, Serialize};
@@ -158,15 +163,44 @@ impl Common {
158163
}
159164
/// Returns a new instance of the `Words` structure.
160165
pub fn words(&self) -> Words {
161-
Words::new()
166+
let words_data = self
167+
.fields
168+
.get("words")
169+
.expect("Words data not found in JSON")
170+
.as_array()
171+
.expect("Words data is not an array")
172+
.iter()
173+
.map(|word_value| word_value.as_str().unwrap().to_string())
174+
.collect::<Vec<String>>(); // Add type annotation here
175+
176+
Words {
177+
words: HashSet::from_iter(words_data),
178+
}
162179
}
163180
/// Parses a string of JSON data and returns a new instance of the
164181
/// `Common` structure.
165-
pub fn parse(
166-
input: &str,
167-
) -> Result<Self, Box<dyn std::error::Error>> {
168-
let common: Common = serde_json::from_str(input)?;
169-
Ok(common)
182+
pub fn parse(input: &str) -> Result<Self, serde_json::Error> {
183+
match serde_json::from_str(input) {
184+
Ok(common) => Ok(common),
185+
Err(e) => {
186+
// Handle the JSON parsing error
187+
match e.classify() {
188+
serde_json::error::Category::Io => {
189+
eprintln!("I/O error occurred: {}", e);
190+
}
191+
serde_json::error::Category::Syntax => {
192+
eprintln!("JSON syntax error: {}", e);
193+
}
194+
serde_json::error::Category::Data => {
195+
eprintln!("Invalid JSON data: {}", e);
196+
}
197+
serde_json::error::Category::Eof => {
198+
eprintln!("Unexpected end of JSON input");
199+
}
200+
}
201+
Err(e)
202+
}
203+
}
170204
}
171205
}
172206

0 commit comments

Comments
 (0)