capsule_library/
sidecar_io.rs1use std::fs::{self, OpenOptions};
2use std::io::{BufReader, BufWriter};
3use std::path::Path;
4
5use capsule_core::sidecar::{LibraryConfigCbor, LibraryVersionCbor};
6
7use crate::utils::paths::tmp_path;
8
9pub fn read_library_version(
10 path: &Path,
11) -> Result<LibraryVersionCbor, Box<dyn std::error::Error + Send + Sync>> {
12 let file = fs::File::open(path)?;
13 let v = ciborium::de::from_reader(BufReader::new(file))?;
14 Ok(v)
15}
16
17pub fn write_library_version(
18 path: &Path,
19 v: &LibraryVersionCbor,
20) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
21 let tmp = tmp_path(path);
22 {
23 let file = OpenOptions::new()
24 .write(true)
25 .create(true)
26 .truncate(true)
27 .open(&tmp)?;
28 ciborium::ser::into_writer(v, BufWriter::new(file))?;
29 }
30 fs::rename(&tmp, path)?;
31 Ok(())
32}
33
34pub fn read_library_config(
35 path: &Path,
36) -> Result<LibraryConfigCbor, Box<dyn std::error::Error + Send + Sync>> {
37 let file = fs::File::open(path)?;
38 let cfg = ciborium::de::from_reader(BufReader::new(file))?;
39 Ok(cfg)
40}
41
42pub fn write_library_config(
43 path: &Path,
44 cfg: &LibraryConfigCbor,
45) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
46 let tmp = tmp_path(path);
47 {
48 let file = OpenOptions::new()
49 .write(true)
50 .create(true)
51 .truncate(true)
52 .open(&tmp)?;
53 ciborium::ser::into_writer(cfg, BufWriter::new(file))?;
54 }
55 fs::rename(&tmp, path)?;
56 Ok(())
57}
58
59#[cfg(test)]
60mod tests;