Impl new fn Records::overwrite

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-27 22:15:21 +10:00
parent 1047a782e5
commit df3af9727e
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -95,6 +95,10 @@ where
.open(path)?, .open(path)?,
)?; )?;
write_to(file, &mut iter.into_iter())
}
pub fn write_to(file: FileLock, iter: &mut dyn Iterator<Item = MetaData>) -> Result<(), Error> {
let writer = io::BufWriter::with_capacity(512, file); let writer = io::BufWriter::with_capacity(512, file);
let mut ser = serde_json::Serializer::new(writer); let mut ser = serde_json::Serializer::new(writer);
@ -115,6 +119,7 @@ pub fn default_path() -> Result<PathBuf, Error> {
#[derive(Debug)] #[derive(Debug)]
pub struct Records { pub struct Records {
file: FileLock, file: FileLock,
/// Use BTreeSet to dedup the metadata
data: BTreeSet<MetaData>, data: BTreeSet<MetaData>,
} }
@ -144,4 +149,9 @@ impl Records {
pub fn load() -> Result<Self, Error> { pub fn load() -> Result<Self, Error> {
Self::load_from_path(default_path()?) Self::load_from_path(default_path()?)
} }
/// **Warning: This will overwrite all existing records!**
pub fn overwrite(self) -> Result<(), Error> {
write_to(self.file, &mut self.data.into_iter())
}
} }