Use Stream Deserializer to load dumps

This commit is contained in:
ManyTheFish 2022-05-31 15:04:33 +02:00
parent 96152a3d32
commit 151f494110

View File

@ -1,7 +1,7 @@
use serde_json::Deserializer;
use serde_json::{Map, Value}; use serde_json::{Map, Value};
use std::fs; use std::fs;
use std::fs::File; use std::fs::File;
use std::io::BufRead;
use std::io::BufReader; use std::io::BufReader;
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
@ -39,10 +39,9 @@ impl AuthController {
return Ok(()); return Ok(());
} }
let mut reader = BufReader::new(File::open(&keys_file_path)?).lines(); let reader = BufReader::new(File::open(&keys_file_path)?);
while let Some(key) = reader.next().transpose()? { for key in Deserializer::from_reader(reader).into_iter() {
let key = serde_json::from_str(&key)?; store.put_api_key(key?)?;
store.put_api_key(key)?;
} }
Ok(()) Ok(())
@ -59,11 +58,14 @@ impl AuthController {
let keys_file_dst = dst.as_ref().join(KEYS_PATH); let keys_file_dst = dst.as_ref().join(KEYS_PATH);
let mut writer = File::create(&keys_file_dst)?; let mut writer = File::create(&keys_file_dst)?;
let mut reader = BufReader::new(File::open(&keys_file_src)?).lines(); let reader = BufReader::new(File::open(&keys_file_src)?);
while let Some(key) = reader.next().transpose()? { for key in Deserializer::from_reader(reader).into_iter() {
let mut key: Map<String, Value> = serde_json::from_str(&key)?; let mut key: Map<String, Value> = key?;
let uid = Uuid::new_v4().to_string();
key.insert("uid".to_string(), Value::String(uid)); // generate a new uuid v4 and insert it in the key.
let uid = serde_json::to_value(Uuid::new_v4()).unwrap();
key.insert("uid".to_string(), uid);
serde_json::to_writer(&mut writer, &key)?; serde_json::to_writer(&mut writer, &key)?;
writer.write_all(b"\n")?; writer.write_all(b"\n")?;
} }