More efficient method for KeywordSection::load()
This commit is contained in:
parent
516c2529e2
commit
16bb6a48a2
@ -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 {
|
||||
|
@ -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 }
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user