playdate_build/manifest/
format.rs

1pub use crate::metadata::format::Manifest;
2use crate::metadata::source::ManifestSourceOptExt;
3
4
5pub trait ManifestFmt {
6	fn write_to<W: std::io::Write>(&self, to: &mut W) -> std::io::Result<()>
7		where Self: ManifestSourceOptExt {
8		let mut buf = String::new();
9		self.write_to_fmt(&mut buf).map_err(std::io::Error::other)?;
10		to.write_all(buf.as_bytes())
11	}
12
13
14	fn write_to_fmt<W: std::fmt::Write>(&self, to: &mut W) -> std::fmt::Result
15		where Self: ManifestSourceOptExt {
16		let data = self;
17
18		let is_not_empty = |s: &&str| !s.trim().is_empty();
19
20		{
21			let mut write_fmt = |k: &str, v: &str| to.write_fmt(format_args!("{}={}\n", k.trim(), v.trim()));
22
23			if let Some(s) = data.name().filter(is_not_empty) {
24				write_fmt("name", s)?;
25			}
26			if let Some(s) = data.version().filter(is_not_empty) {
27				write_fmt("version", s)?
28			}
29			if let Some(s) = data.author().filter(is_not_empty) {
30				write_fmt("author", s)?
31			}
32			if let Some(s) = data.bundle_id().filter(is_not_empty) {
33				write_fmt("bundleID", s)?
34			}
35			if let Some(s) = data.description().filter(is_not_empty) {
36				write_fmt("description", s)?
37			}
38			if let Some(s) = data.image_path().filter(is_not_empty) {
39				write_fmt("imagePath", s)?
40			}
41			if let Some(s) = data.launch_sound_path().filter(is_not_empty) {
42				write_fmt("launchSoundPath", s)?
43			}
44			if let Some(s) = data.content_warning().filter(is_not_empty) {
45				write_fmt("contentWarning", s)?
46			}
47			if let Some(s) = data.content_warning2().filter(is_not_empty) {
48				write_fmt("contentWarning2", s)?
49			}
50		}
51
52		if let Some(v) = data.build_number() {
53			to.write_fmt(format_args!("{}={}\n", "buildNumber", v))?
54		}
55
56		if let Some(extra) = data.iter_extra() {
57			for (key, value) in extra.into_iter() {
58				let (key, value) = (key.as_ref(), value.as_ref());
59				if is_not_empty(&key) && !value.is_empty() {
60					to.write_fmt(format_args!("{}={}\n", key.trim(), value))?
61				}
62			}
63		}
64
65		Ok(())
66	}
67
68
69	fn to_manifest_string(&self) -> Result<String, std::fmt::Error>
70		where Self: ManifestSourceOptExt {
71		let mut buf = String::new();
72		self.write_to_fmt(&mut buf)?;
73		Ok(buf)
74	}
75}
76
77
78impl<T: ManifestSourceOptExt> ManifestFmt for T {}
79
80
81#[cfg(test)]
82mod tests {
83	use crate::metadata::format::Ext;
84	use crate::metadata::format::ExtraFields;
85	use super::ManifestFmt;
86	use super::Manifest;
87
88
89	#[test]
90	fn fmt_empty() {
91		let m = Manifest::<&str>::default();
92		assert!(m.to_manifest_string().unwrap().is_empty());
93	}
94
95	#[test]
96	fn fmt_full() {
97		let m = Manifest::<&str> { name: "name".into(),
98		                           version: "version".into(),
99		                           author: "author".into(),
100		                           bundle_id: "bundle_id".into(),
101		                           description: "description".into(),
102		                           image_path: "image_path".into(),
103		                           launch_sound_path: "launch_sound_path".into(),
104		                           content_warning: "content_warning".into(),
105		                           content_warning2: "content_warning2".into(),
106		                           build_number: 42.into() };
107		let s = m.to_manifest_string().unwrap();
108		assert_eq!(
109		           "name=name\nversion=version\nauthor=author\nbundleID=bundle_id\ndescription=description\nimagePath=image_path\nlaunchSoundPath=launch_sound_path\ncontentWarning=content_warning\ncontentWarning2=content_warning2\nbuildNumber=42\n",
110		           s
111		);
112	}
113
114	#[test]
115	fn fmt_ext() {
116		let main = Manifest::<&str> { bundle_id: "bundle_id".into(),
117		                              ..Default::default() };
118		let mut extra = ExtraFields::new();
119		extra.insert("foo".to_owned(), "bar".to_owned().into());
120
121		let m = Ext::new(main, extra);
122		let s = m.to_manifest_string().unwrap();
123		assert_eq!("bundleID=bundle_id\nfoo=bar\n", s);
124	}
125
126	#[test]
127	fn fmt_trim() {
128		let main = Manifest::<&str> { name: "  name".into(),
129		                              bundle_id: "bundle_id   ".into(),
130		                              description: "   description   ".into(),
131		                              ..Default::default() };
132		let mut extra = ExtraFields::new();
133		extra.insert("   foo    ".to_owned(), "  bar  ".to_owned().into());
134
135		let m = Ext::new(main, extra);
136		let s = m.to_manifest_string().unwrap();
137		assert_eq!(
138		           "name=name\nbundleID=bundle_id\ndescription=description\nfoo=bar\n",
139		           s
140		);
141	}
142}