Impl binstall_v1::append_to_path

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-26 22:45:59 +10:00
parent 461571075d
commit f0b6b7b1af
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -84,3 +84,27 @@ pub enum Error {
#[error(transparent)]
SerdeJsonParse(#[from] serde_json::Error),
}
pub fn append_to_path<Iter>(path: impl AsRef<Path>, iter: Iter) -> Result<(), Error>
where
Iter: IntoIterator<Item = Entry>,
{
let file = FileLock::new_exclusive(
fs::OpenOptions::new()
.create(true)
.append(true)
.open(path)?,
)?;
let writer = io::BufWriter::with_capacity(512, file);
let mut ser = serde_json::Serializer::new(writer);
for item in iter {
item.serialize(&mut ser)?;
}
ser.into_inner().flush()?;
Ok(())
}