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() .as_object()
.unwrap() .unwrap()
.into_iter() .into_iter()
.map(|a| (a.0.to_owned(), KeywordSection::load(a.1))) .map(|a| (a.0.to_owned(), KeywordSection::load(a.1).unwrap()))
.collect(); .collect();
Self { Self {

View File

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