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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use crate::value::Value;


pub const METADATA_FIELD: &str = "playdate";


#[cfg(feature = "crate-metadata")]
pub use self::cargo::*;

mod cargo {
	#![cfg(feature = "crate-metadata")]
	use std::path::PathBuf;
	pub use crate_metadata::Package;
	use super::format::Metadata;
	use crate::config::Env;
	use super::Value;
	use super::error::Error;


	#[derive(Debug)]
	pub struct PackageInfo<T> {
		pub package: Package<Metadata<T>>,
		pub target_directory: PathBuf,
	}

	pub fn crate_metadata<T: Value>(env: &Env) -> Result<PackageInfo<T>, Error> {
		let name = env.cargo_pkg_name();
		let manifest = env.manifest_path();
		manifest.try_exists()?
		        .then(|| ())
		        .ok_or("unable to find crate manifest")?;

		let metadata = crate_metadata::crate_metadata::<Metadata<T>>().unwrap();
		let result = metadata.packages
		                     .into_iter()
		                     .find(|p| &p.name == name)
		                     .map(|package| {
			                     PackageInfo { package,
			                                   target_directory: metadata.target_directory }
		                     });
		result.ok_or("package not found".into())
	}
}


pub mod error {
	use std::io::Error as IoError;

	#[derive(Debug)]
	pub enum Error {
		Io(IoError),
		Err(&'static str),
		#[cfg(feature = "serde_json")]
		Json(serde_json::error::Error),
		#[cfg(feature = "toml")]
		Toml(toml::de::Error),
	}

	impl From<&'static str> for Error {
		fn from(value: &'static str) -> Self { Self::Err(value) }
	}

	impl From<IoError> for Error {
		fn from(err: IoError) -> Self { Self::Io(err) }
	}

	#[cfg(feature = "serde_json")]
	impl From<serde_json::error::Error> for Error {
		fn from(err: serde_json::error::Error) -> Self { Self::Json(err) }
	}

	#[cfg(feature = "toml")]
	impl From<toml::de::Error> for Error {
		fn from(err: toml::de::Error) -> Self { Self::Toml(err) }
	}

	impl std::fmt::Display for Error {
		fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
			match self {
				Error::Io(err) => err.fmt(f),
				Error::Err(err) => err.fmt(f),
				#[cfg(feature = "serde_json")]
				Error::Json(err) => err.fmt(f),
				#[cfg(feature = "toml")]
				Error::Toml(err) => err.fmt(f),
			}
		}
	}

	impl std::error::Error for Error {
		fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
			match self {
				Error::Err(_) => None,
				Error::Io(err) => Some(err),
				#[cfg(feature = "serde_json")]
				Error::Json(err) => Some(err),
				#[cfg(feature = "toml")]
				Error::Toml(err) => Some(err),
			}
		}
	}
}


pub mod format {
	use super::Value;
	use super::error::Error;
	use std::borrow::Cow;
	use std::collections::HashMap;
	use serde::Deserialize;


	#[derive(Deserialize, Debug)]
	#[serde(bound(deserialize = "T: Deserialize<'de>"))]
	#[serde(deny_unknown_fields)]
	pub struct Metadata<T> {
		// TODO: test deserialization with `crate::metadata::METADATA_FIELD` for field name.
		pub playdate: Option<PlayDateMetadata<T>>,
	}


	#[derive(Deserialize, Debug, Clone)]
	#[serde(bound(deserialize = "T: Deserialize<'de>"))]
	#[serde(deny_unknown_fields)]
	pub struct PlayDateMetadata<T> {
		pub name: Option<String>,
		pub version: Option<String>,
		pub author: Option<String>,
		#[serde(alias = "bundle-id", rename = "bundle-id")]
		pub bundle_id: String,
		pub description: Option<String>,
		#[serde(alias = "image-path", rename = "image-path")]
		pub image_path: Option<String>,
		#[serde(alias = "launch-sound-path", rename = "launch-sound-path")]
		pub launch_sound_path: Option<String>,
		#[serde(alias = "content-warning", rename = "content-warning")]
		pub content_warning: Option<String>,
		#[serde(alias = "content-warning2", rename = "content-warning2")]
		pub content_warning2: Option<String>,
		#[serde(alias = "build-number", rename = "build-number")]
		pub build_number: Option<String>,
		#[serde(default = "PlayDateMetadataAssets::<T>::default")]
		pub assets: PlayDateMetadataAssets<T>,
		#[serde(alias = "dev-assets", rename = "dev-assets")]
		pub dev_assets: Option<PlayDateMetadataAssets<T>>,
		#[serde(default)]
		pub options: Options,
		#[serde(default)]
		pub support: Support,
	}


	impl<T: Value> PlayDateMetadata<T> where Error: From<<T as TryInto<AssetsOptions>>::Error> {
		pub fn merge_opts(&mut self) -> Result<(), Error> {
			let opts = if let Some(res) = self.assets.extract_options() {
				Some(res?)
			} else {
				Default::default()
			};

			match (self.options.assets.is_some(), opts) {
				(_, None) => Ok(()),
				(true, Some(_)) => {
					Err(concat!(
						"[package.metadata.playdate.assets.options]",
						" conflicts with ",
						"[package.metadata.playdate.options.assets]"
					).into())
				},
				(false, Some(opts)) => {
					let _ = self.options.assets.insert(opts);
					Ok(())
				},
			}
		}
	}

	impl<T: Value> PlayDateMetadata<T> {
		pub fn assets_options(&self) -> Cow<'_, crate::metadata::format::AssetsOptions> {
			self.options
			    .assets
			    .as_ref()
			    .map_or_else(Default::default, Cow::Borrowed)
		}
	}


	impl<T: Value> PlayDateMetadataAssets<T> where Error: From<<T as TryInto<AssetsOptions>>::Error> {
		fn extract_options(&mut self) -> Option<Result<AssetsOptions, Error>> {
			match self {
				PlayDateMetadataAssets::Map(map) => {
					// Remove only value that have `table/map` (not bool or str) type:
					if map.get("options")
					      .filter(|v| v.as_str().is_none() && v.as_bool().is_none())
					      .is_some()
					{
						map.remove("options")
						   .map(|v| v.try_into())
						   .map(|res| res.map_err(Into::into))
					} else {
						None
					}
				},
				_ => None,
			}
		}
	}

	#[cfg(feature = "serde_json")]
	impl TryFrom<serde_json::Value> for AssetsOptions {
		type Error = serde_json::error::Error;
		fn try_from(value: serde_json::Value) -> std::result::Result<Self, Self::Error> {
			serde_json::from_value(value)
		}
	}

	#[cfg(feature = "toml")]
	impl TryFrom<toml::Value> for AssetsOptions {
		type Error = toml::de::Error;
		fn try_from(value: toml::Value) -> std::result::Result<Self, Self::Error> {
			toml::Value::try_into::<AssetsOptions>(value)
		}
	}


	#[derive(Deserialize, Debug, Clone, Default)]
	#[serde(deny_unknown_fields)]
	pub struct Options {
		pub assets: Option<AssetsOptions>,
		// This is temporary removed:
		// #[serde(alias = "dry-run", rename = "dry-run", default)]
		// pub dry_run: bool,
		// #[serde(alias = "cross-target", rename = "cross-target", default)]
		// pub cross_target: bool,
	}


	#[derive(Deserialize, Debug, Clone, Default)]
	#[serde(deny_unknown_fields)]
	pub struct AssetsOptions {
		#[serde(alias = "override", default = "bool::<true>")]
		pub overwrite: bool,

		#[serde(
		        alias = "follow-symlinks",
		        rename = "follow-symlinks",
		        default = "bool::<true>"
		)]
		pub follow_symlinks: bool,

		#[serde(alias = "build-method", default)]
		pub method: AssetsBuildMethod,

		/// Allow building assets for dependencies
		#[serde(default = "bool::<false>")]
		pub dependencies: bool,
	}

	#[derive(Deserialize, Debug, Clone, Copy)]
	#[serde(rename_all = "kebab-case")]
	pub enum AssetsBuildMethod {
		Copy,
		Link,
	}

	impl Default for AssetsBuildMethod {
		fn default() -> Self { Self::Link }
	}


	#[derive(Deserialize, Debug, Clone)]
	#[serde(bound(deserialize = "T: Deserialize<'de>"))]
	#[serde(untagged)]
	#[serde(deny_unknown_fields)]
	pub enum PlayDateMetadataAssets<T> {
		/// List of paths to include.
		List(Vec<String>),
		/// Rules & queries used to resolve paths to include.
		Map(HashMap<String, T>),
	}

	impl<T> Default for PlayDateMetadataAssets<T> {
		fn default() -> Self { Self::List(Vec::with_capacity(0)) }
	}

	impl<T> PlayDateMetadataAssets<T> {
		pub fn is_empty(&self) -> bool {
			match self {
				PlayDateMetadataAssets::List(list) => list.is_empty(),
				PlayDateMetadataAssets::Map(map) => map.is_empty(),
			}
		}
	}


	#[derive(Deserialize, Debug, Clone, Default)]
	pub struct Support {
		// #[serde(rename = "crank-manifest")]
		// #[serde(alias = "crank-manifest")]
		// pub crank_manifest: Option<CrankManifest>,
		// pub crank_manifest_rules: Option<Rules>,
	}

	const fn bool<const V: bool>() -> bool { V }
}