Skip to content

Commit 8d02177

Browse files
author
cht
committed
update the serde function
1 parent 45230f8 commit 8d02177

1 file changed

Lines changed: 10 additions & 12 deletions

File tree

src/utils.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66
io::{prelude::*, Result},
77
path::Path,
88
};
9+
use serde::{Deserialize, Serialize};
910
pub enum Save {
1011
Storage,
1112
Running,
@@ -60,6 +61,10 @@ pub fn start_v2core() -> String {
6061
let message_pre = v["v2core"].to_string();
6162
crate::spider::remove_quotation(message_pre)
6263
}
64+
#[derive(Clone, Serialize, Deserialize)]
65+
struct Urls {
66+
url : String,
67+
}
6368
pub fn get_subs() -> Vec<String> {
6469
create_storage_before();
6570
let messages = match get_json(Save::Subscribes) {
@@ -70,17 +75,10 @@ pub fn get_subs() -> Vec<String> {
7075
"[]".to_string()
7176
}
7277
};
73-
let mut subscribes = Vec::new();
74-
let v: Value = serde_json::from_str(messages.as_str()).unwrap();
75-
let mut index = 0;
76-
while v[index] != Value::Null {
77-
let sub = v[index]["url"].to_string();
78-
let length = sub.len();
79-
let sub = (&sub[1..length - 1]).to_string();
80-
subscribes.push(sub);
81-
index += 1;
82-
}
83-
subscribes
78+
serde_json::from_str::<Vec<Urls>>(messages.as_str()).unwrap_or_default()
79+
.iter()
80+
.map(|aurl| aurl.url.clone())
81+
.collect()
8482
}
8583
pub fn start() -> Vec<Vec<Information>> {
8684
create_storage_before();
@@ -93,5 +91,5 @@ pub fn start() -> Vec<Vec<Information>> {
9391
}
9492
};
9593
// 如果发生错误,就不读取
96-
serde_json::from_str(messages.as_str()).unwrap_or(vec![])
94+
serde_json::from_str(messages.as_str()).unwrap_or_default()
9795
}

0 commit comments

Comments
 (0)