From 60985236c7070425c28aa0c5ce675306d06ab123 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Thu, 16 Jan 2025 14:45:14 -0500 Subject: initial --- src/parse/ser.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/parse/ser.rs (limited to 'src/parse/ser.rs') diff --git a/src/parse/ser.rs b/src/parse/ser.rs new file mode 100644 index 0000000..3a29fde --- /dev/null +++ b/src/parse/ser.rs @@ -0,0 +1,62 @@ +//! Serialize an iris structure into a string. + +use crate::{Config, Plugin}; + +use serde::ser; + +/// Errors that can occur when serializing a type +#[derive(thiserror::Error, Debug)] +pub enum Error { + /// Occurs when toml could not be eserialized + #[error(transparent)] + Toml(#[from] toml::ser::Error), +} + +impl ser::Serialize for Plugin { + fn serialize(&self, s: S) -> Result + where + S: ser::Serializer, + { + use ser::SerializeMap; + let mut s = s.serialize_map(None)?; + s.serialize_entry("id", &self.id)?; + s.serialize_entry("url", self.url().as_str())?; + if let Some(commit) = &self.commit { + s.serialize_entry("commit", commit)?; + } + if let Some(branch) = &self.branch { + s.serialize_entry("branch", branch)?; + } + if let Some(run) = &self.run { + s.serialize_entry("run", run)?; + } + s.serialize_entry("before", &self.before)?; + s.serialize_entry("after", &self.after)?; + s.end() + } +} + +impl ser::Serialize for Config { + fn serialize(&self, s: S) -> Result + where + S: ser::Serializer, + { + use ser::SerializeMap; + let mut s = s.serialize_map(Some(1))?; + + // plugins + s.serialize_key("plugins")?; + s.serialize_value(&self.plugins)?; + + s.end() + } +} + +impl Config { + /// Serializes a string from the iris config + pub fn to_string(&self) -> crate::Result { + toml::to_string(self) + .map_err(Error::from) + .map_err(crate::Error::from) + } +} -- cgit v1.2.3-freya