summaryrefslogtreecommitdiff
path: root/src/json.rs
diff options
context:
space:
mode:
authorKim Altintop <kim@eagain.io>2023-01-09 13:18:33 +0100
committerKim Altintop <kim@eagain.io>2023-01-09 13:18:33 +0100
commitd2f423521ec76406944ad83098ec33afe20c692b (patch)
treeafd86bcb088eebdd61ba4e52fa666ff0f41c42a2 /src/json.rs
This is it
Squashed commit of all the exploration history. Development starts here. Signed-off-by: Kim Altintop <kim@eagain.io>
Diffstat (limited to 'src/json.rs')
-rw-r--r--src/json.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/json.rs b/src/json.rs
new file mode 100644
index 0000000..52f6215
--- /dev/null
+++ b/src/json.rs
@@ -0,0 +1,49 @@
+// Copyright © 2022 Kim Altintop <kim@eagain.io>
+// SPDX-License-Identifier: GPL-2.0-only WITH openvpn-openssl-exception
+
+use std::{
+ fs::File,
+ io::BufReader,
+ path::Path,
+};
+
+use serde::{
+ de::DeserializeOwned,
+ Deserialize,
+ Serialize,
+};
+
+pub mod canonical;
+
+pub fn from_blob<'a, T>(blob: &'a git2::Blob) -> crate::Result<T>
+where
+ T: Deserialize<'a>,
+{
+ Ok(serde_json::from_slice(blob.content())?)
+}
+
+pub fn to_blob<T>(repo: &git2::Repository, data: &T) -> crate::Result<git2::Oid>
+where
+ T: Serialize,
+{
+ let mut writer = repo.blob_writer(None)?;
+ serde_json::to_writer_pretty(&mut writer, data)?;
+ Ok(writer.commit()?)
+}
+
+pub fn from_file<P, T>(path: P) -> crate::Result<T>
+where
+ P: AsRef<Path>,
+ T: DeserializeOwned,
+{
+ let file = File::open(path)?;
+ Ok(serde_json::from_reader(BufReader::new(file))?)
+}
+
+pub fn load<P, T>(path: P) -> crate::Result<T>
+where
+ P: AsRef<Path>,
+ T: DeserializeOwned,
+{
+ from_file(path)
+}