More efficient method for KeywordSection::load()

This commit is contained in:
0xf8 2023-04-19 20:53:16 -04:00
parent 516c2529e2
commit 16bb6a48a2
Signed by: 0xf8
GPG Key ID: 446580D758689584
2 changed files with 7 additions and 21 deletions

View File

@ -122,7 +122,7 @@ impl Config {
.as_object()
.unwrap()
.into_iter()
.map(|a| (a.0.to_owned(), KeywordSection::load(a.1)))
.map(|a| (a.0.to_owned(), KeywordSection::load(a.1).unwrap()))
.collect();
Self {

View File

@ -1,7 +1,8 @@
use serde_json::Value;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct KeywordSection {
pub threshold: u64,
pub required: Vec<String>,
@ -9,25 +10,10 @@ pub struct KeywordSection {
}
impl KeywordSection {
pub fn load(json: &Value) -> Self {
let threshold: u64 = json["threshold"].as_u64().unwrap();
let required: Vec<String> = json["requiredKeywords"]
.as_array()
.unwrap()
.into_iter()
.map(|a| a.as_str().unwrap().to_string())
.collect();
let keywords: Vec<String> = json["keywords"]
.as_array()
.unwrap()
.into_iter()
.map(|a| a.as_str().unwrap().to_string())
.collect();
Self {
threshold,
required,
keywords,
pub fn load(json: &Value) -> Option<Self> {
match serde_json::from_value(json.to_owned()) {
Ok(j) => Some(j),
Err(e) => { eprintln!("{e}"); None }
}
}