diff options
author | Freya Murphy <freya@freyacat.org> | 2025-01-16 14:45:14 -0500 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2025-01-16 14:45:14 -0500 |
commit | 60985236c7070425c28aa0c5ce675306d06ab123 (patch) | |
tree | 7448aef5dadf19d4504151ebc370f9e20757ef10 /src/parse/ser.rs | |
download | iris-60985236c7070425c28aa0c5ce675306d06ab123.tar.gz iris-60985236c7070425c28aa0c5ce675306d06ab123.tar.bz2 iris-60985236c7070425c28aa0c5ce675306d06ab123.zip |
Diffstat (limited to 'src/parse/ser.rs')
-rw-r--r-- | src/parse/ser.rs | 62 |
1 files changed, 62 insertions, 0 deletions
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<S>(&self, s: S) -> Result<S::Ok, S::Error> + 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<S>(&self, s: S) -> Result<S::Ok, S::Error> + 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<String> { + toml::to_string(self) + .map_err(Error::from) + .map_err(crate::Error::from) + } +} |