summaryrefslogtreecommitdiff
path: root/src/parse/ser.rs
blob: 3a29fdeca43f754547d2521e2dc0cfd8b22feee2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)
	}
}