fontoxide-misc 0.1.0

Miscellaneous font utilities; a port of fontTools.misc, agl, afmLib and tfmLib
Documentation
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
//! Port of `fontTools.misc.plistlib`: property lists in XML.
//!
//! [`Value`] keeps the distinctions of the Python objects a plist is read into
//! (`bool`, `int`, `float`, `str`, `bytes`, `datetime`, `list`, `dict`), and
//! dictionaries keep their insertion order like Python dicts.

use std::fmt;

use indexmap::IndexMap;

use crate::etree::{self, Element};
use crate::py;
use crate::xml_reader::{self, Content};

/// A plist dictionary (`dict`).
pub type Dict = IndexMap<String, Value>;

/// An error reading or writing a plist.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum PlistError {
	/// Malformed plist structure or value (`ValueError`).
	#[error("{0}")]
	Value(String),
	/// An integer outside the range plists can store (`OverflowError`).
	#[error("{0}")]
	Overflow(String),
	/// Invalid base64 data (`binascii.Error`).
	#[error("{0}")]
	Base64(String),
	/// An XML syntax or serialization error.
	#[error("{0}")]
	Xml(String),
}

type Result<T> = std::result::Result<T, PlistError>;

/// A plist date (`datetime.datetime`, without microseconds or time zone).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Date {
	/// The year, 1 to 9999.
	pub year: u16,
	/// The month, 1 to 12.
	pub month: u8,
	/// The day of the month.
	pub day: u8,
	/// The hour, 0 to 23.
	pub hour: u8,
	/// The minute, 0 to 59.
	pub minute: u8,
	/// The second, 0 to 59.
	pub second: u8,
}

/// A plist value.
#[derive(Debug, Clone)]
pub enum Value {
	/// `<true/>` or `<false/>` (`bool`).
	Bool(bool),
	/// `<integer>` (`int`; plists store 64-bit signed or unsigned values).
	Integer(i128),
	/// `<real>` (`float`).
	Real(f64),
	/// `<string>` (`str`).
	String(String),
	/// `<data>` (`bytes`).
	Data(Vec<u8>),
	/// `<date>` (`datetime`).
	Date(Date),
	/// `<array>` (`list`).
	Array(Vec<Value>),
	/// `<dict>` (`dict`).
	Dict(Dict),
}

impl Value {
	/// The dictionary, if this is one.
	pub fn as_dict(&self) -> Option<&Dict> {
		match self {
			Value::Dict(d) => Some(d),
			_ => None,
		}
	}

	/// The string, if this is one.
	pub fn as_str(&self) -> Option<&str> {
		match self {
			Value::String(s) => Some(s),
			_ => None,
		}
	}

	/// The number as Python compares it (`bool` is an `int`).
	fn as_number(&self) -> Option<Number> {
		match *self {
			Value::Bool(b) => Some(Number::Int(i128::from(b))),
			Value::Integer(i) => Some(Number::Int(i)),
			Value::Real(f) => Some(Number::Float(f)),
			_ => None,
		}
	}
}

#[derive(Clone, Copy)]
enum Number {
	Int(i128),
	Float(f64),
}

impl PartialEq for Number {
	fn eq(&self, other: &Self) -> bool {
		match (*self, *other) {
			(Number::Int(a), Number::Int(b)) => a == b,
			(Number::Float(a), Number::Float(b)) => a == b,
			(Number::Int(i), Number::Float(f)) | (Number::Float(f), Number::Int(i)) => {
				f.fract() == 0.0 && f.is_finite() && (i as f64) == f && i == f as i128
			}
		}
	}
}

/// Python's `==`: numbers compare by value across `bool`, `int` and `float`;
/// dictionaries compare regardless of order.
impl PartialEq for Value {
	fn eq(&self, other: &Self) -> bool {
		if let (Some(a), Some(b)) = (self.as_number(), other.as_number()) {
			return a == b;
		}
		match (self, other) {
			(Value::String(a), Value::String(b)) => a == b,
			(Value::Data(a), Value::Data(b)) => a == b,
			(Value::Date(a), Value::Date(b)) => a == b,
			(Value::Array(a), Value::Array(b)) => a == b,
			(Value::Dict(a), Value::Dict(b)) => a == b,
			_ => false,
		}
	}
}

impl From<bool> for Value {
	fn from(v: bool) -> Self {
		Value::Bool(v)
	}
}

impl From<i64> for Value {
	fn from(v: i64) -> Self {
		Value::Integer(i128::from(v))
	}
}

impl From<i32> for Value {
	fn from(v: i32) -> Self {
		Value::Integer(i128::from(v))
	}
}

impl From<f64> for Value {
	fn from(v: f64) -> Self {
		Value::Real(v)
	}
}

impl From<&str> for Value {
	fn from(v: &str) -> Self {
		Value::String(v.to_owned())
	}
}

impl From<String> for Value {
	fn from(v: String) -> Self {
		Value::String(v)
	}
}

impl From<Vec<Value>> for Value {
	fn from(v: Vec<Value>) -> Self {
		Value::Array(v)
	}
}

impl From<Dict> for Value {
	fn from(v: Dict) -> Self {
		Value::Dict(v)
	}
}

impl fmt::Display for Date {
	/// `_date_to_string`.
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(
			f,
			"{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
			self.year, self.month, self.day, self.hour, self.minute, self.second
		)
	}
}

fn days_in_month(year: u16, month: u8) -> u8 {
	match month {
		1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
		4 | 6 | 9 | 11 => 30,
		_ => {
			if (year.is_multiple_of(4) && !year.is_multiple_of(100)) || year.is_multiple_of(400) {
				29
			} else {
				28
			}
		}
	}
}

/// `_date_from_string`: `YYYY[-MM[-DD[THH[:MM[:SS]]]]]Z` at the start of the
/// string, then `datetime(*fields)`, which needs at least the day.
pub fn date_from_string(s: &str) -> Result<Date> {
	let bytes = s.as_bytes();
	let mut pos = 0;
	let mut fields: Vec<u16> = Vec::new();
	let digits = |pos: usize, n: usize| -> Option<u16> {
		let part = bytes.get(pos..pos + n)?;
		if part.iter().all(u8::is_ascii_digit) {
			Some(
				part.iter()
					.fold(0u16, |acc, &d| acc * 10 + u16::from(d - b'0')),
			)
		} else {
			None
		}
	};
	let bad = || PlistError::Value(format!("Expected ISO 8601 date string, but got {s:?}."));
	fields.push(digits(0, 4).ok_or_else(bad)?);
	pos += 4;
	for sep in *b"--T::" {
		if bytes.get(pos) == Some(&sep)
			&& let Some(v) = digits(pos + 1, 2)
		{
			fields.push(v);
			pos += 3;
		} else {
			break;
		}
	}
	if bytes.get(pos) != Some(&b'Z') {
		return Err(bad());
	}
	if fields.len() < 3 {
		return Err(PlistError::Value(format!(
			"function missing required argument '{}'",
			["month", "day"][fields.len() - 1]
		)));
	}
	fields.resize(6, 0);
	let year = fields[0];
	let [month, day, hour, minute, second] =
		[fields[1], fields[2], fields[3], fields[4], fields[5]].map(|v| v as u8);
	if year == 0 {
		return Err(PlistError::Value(format!("year {year} is out of range")));
	}
	if !(1..=12).contains(&month) {
		return Err(PlistError::Value("month must be in 1..12".into()));
	}
	if day == 0 || day > days_in_month(year, month) {
		return Err(PlistError::Value("day is out of range for month".into()));
	}
	if hour > 23 {
		return Err(PlistError::Value("hour must be in 0..23".into()));
	}
	if minute > 59 {
		return Err(PlistError::Value("minute must be in 0..59".into()));
	}
	if second > 59 {
		return Err(PlistError::Value("second must be in 0..59".into()));
	}
	Ok(Date {
		year,
		month,
		day,
		hour,
		minute,
		second,
	})
}

const B64: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

/// `base64.b64encode`.
fn b64encode(data: &[u8]) -> String {
	let mut out = String::with_capacity(data.len().div_ceil(3) * 4);
	for chunk in data.chunks(3) {
		let b = [
			chunk[0],
			*chunk.get(1).unwrap_or(&0),
			*chunk.get(2).unwrap_or(&0),
		];
		let n = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]);
		for i in 0..4 {
			if i <= chunk.len() {
				out.push(B64[((n >> (18 - 6 * i)) & 63) as usize] as char);
			} else {
				out.push('=');
			}
		}
	}
	out
}

/// `base64.b64decode` (`binascii.a2b_base64` in non-strict mode, as of Python
/// 3.13): characters outside the alphabet are skipped, and padding only matters
/// at the end, where complete padding excuses an unfinished group.
pub fn b64decode(text: &str) -> Result<Vec<u8>> {
	let mut out = Vec::new();
	let mut quad_pos = 0;
	let mut left: u32 = 0;
	let mut pads = 0;
	let mut padded = false;
	let mut data_chars = 0usize;
	for c in text.bytes() {
		if c == b'=' {
			if quad_pos >= 2 {
				pads += 1;
				if quad_pos + pads >= 4 {
					padded = true;
				}
			}
			continue;
		}
		let Some(v) = B64.iter().position(|&b| b == c) else {
			continue;
		};
		let v = v as u32;
		pads = 0;
		padded = false;
		data_chars += 1;
		match quad_pos {
			0 => {
				left = v;
				quad_pos = 1;
			}
			1 => {
				out.push(((left << 2) | (v >> 4)) as u8);
				left = v & 0x0f;
				quad_pos = 2;
			}
			2 => {
				out.push(((left << 4) | (v >> 2)) as u8);
				left = v & 0x03;
				quad_pos = 3;
			}
			_ => {
				out.push(((left << 6) | v) as u8);
				left = 0;
				quad_pos = 0;
			}
		}
	}
	match quad_pos {
		_ if padded => Ok(out),
		0 => Ok(out),
		1 => Err(PlistError::Base64(format!(
			"Invalid base64-encoded string: number of data characters ({data_chars}) cannot be 1 more than a multiple of 4"
		))),
		_ => Err(PlistError::Base64("Incorrect padding".into())),
	}
}

/// `_encode_base64`: base64 lines of at most `maxlinelength` characters
/// including the indentation, each on its own line.
fn encode_base64(data: &[u8], maxlinelength: Option<usize>, indent_level: usize) -> String {
	let encoded = b64encode(data);
	match maxlinelength {
		Some(maxlinelength) if !encoded.is_empty() && maxlinelength > 0 => {
			let indent = format!("\n{}", "  ".repeat(indent_level));
			let max_length = 16.max(maxlinelength.saturating_sub(indent.len()));
			let mut out = String::new();
			for chunk in encoded.as_bytes().chunks(max_length) {
				out.push_str(&indent);
				out.push_str(std::str::from_utf8(chunk).unwrap_or_default());
			}
			out.push_str(&indent);
			out
		}
		_ => encoded,
	}
}

/// The options of [`totree`].
#[derive(Debug, Clone, Copy)]
pub struct TreeOptions {
	/// Whether keys of dictionaries are sorted (`sort_keys`).
	pub sort_keys: bool,
	/// Whether `<data>` is split into indented lines (`pretty_print`).
	pub pretty_print: bool,
	/// The indentation level of the value (`indent_level`).
	pub indent_level: usize,
}

impl Default for TreeOptions {
	fn default() -> Self {
		TreeOptions {
			sort_keys: true,
			pretty_print: true,
			indent_level: 1,
		}
	}
}

fn text_element(tag: &str, text: String) -> Element {
	let mut el = Element::new(tag);
	el.text = Some(text);
	el
}

fn make_element(value: &Value, ctx: &mut TreeOptions) -> Result<Element> {
	Ok(match value {
		Value::String(s) => text_element("string", s.clone()),
		Value::Bool(b) => Element::new(if *b { "true" } else { "false" }),
		Value::Integer(i) => {
			if -(1i128 << 63) <= *i && *i < (1i128 << 64) {
				text_element("integer", i.to_string())
			} else {
				return Err(PlistError::Overflow(i.to_string()));
			}
		}
		Value::Real(f) => text_element("real", py::float_repr(*f)),
		Value::Dict(d) => {
			let mut el = Element::new("dict");
			let mut items: Vec<(&String, &Value)> = d.iter().collect();
			if ctx.sort_keys {
				items.sort_by(|a, b| a.0.cmp(b.0));
			}
			ctx.indent_level += 1;
			for (key, value) in items {
				el.append(text_element("key", key.clone()));
				el.append(make_element(value, ctx)?);
			}
			ctx.indent_level -= 1;
			el
		}
		Value::Array(a) => {
			let mut el = Element::new("array");
			if a.is_empty() {
				return Ok(el);
			}
			ctx.indent_level += 1;
			for value in a {
				el.append(make_element(value, ctx)?);
			}
			ctx.indent_level -= 1;
			el
		}
		Value::Date(d) => text_element("date", d.to_string()),
		Value::Data(data) => text_element(
			"data",
			encode_base64(
				data,
				if ctx.pretty_print { Some(76) } else { None },
				ctx.indent_level,
			),
		),
	})
}

/// `totree`: converts a value to an XML element.
pub fn totree(value: &Value, options: TreeOptions) -> Result<Element> {
	let mut ctx = options;
	make_element(value, &mut ctx)
}

/// `PlistTarget`: builds a value from start/end/data events.
struct PlistTarget {
	stack: Vec<Value>,
	/// For each open container, the key it is stored under in its parent.
	keys: Vec<Option<String>>,
	current_key: Option<String>,
	root: Option<Value>,
	data: Vec<String>,
}

impl PlistTarget {
	fn start(&mut self, tag: &str) -> Result<()> {
		self.data.clear();
		match tag {
			"dict" => self.start_container(Value::Dict(Dict::new())),
			"array" => self.start_container(Value::Array(Vec::new())),
			_ => Ok(()),
		}
	}

	/// `add_object` for a container: it is filled while it is on the stack, then
	/// stored into its parent when it ends.
	fn start_container(&mut self, value: Value) -> Result<()> {
		let key = if self.current_key.is_some() {
			match self.stack.last() {
				Some(Value::Dict(_)) => {}
				Some(top) => return Err(unexpected(top)),
				None => return Err(PlistError::Value("unexpected element".into())),
			}
			self.current_key.take()
		} else {
			if let Some(top) = self.stack.last()
				&& !matches!(top, Value::Array(_))
			{
				return Err(unexpected(top));
			}
			None
		};
		self.stack.push(value);
		self.keys.push(key);
		Ok(())
	}

	fn end(&mut self, tag: &str) -> Result<()> {
		match tag {
			"dict" => {
				if let Some(key) = &self.current_key
					&& !key.is_empty()
				{
					return Err(PlistError::Value(format!("missing value for key '{key}'")));
				}
				self.end_container()
			}
			"array" => self.end_container(),
			"key" => {
				let is_dict = matches!(self.stack.last(), Some(Value::Dict(_)));
				if self.current_key.as_ref().is_some_and(|k| !k.is_empty()) || !is_dict {
					return Err(PlistError::Value("unexpected key".into()));
				}
				self.current_key = Some(self.get_data());
				Ok(())
			}
			"true" => self.add_object(Value::Bool(true)),
			"false" => self.add_object(Value::Bool(false)),
			"integer" => {
				let data = self.get_data();
				let v = py::parse_int(&data).ok_or_else(|| {
					PlistError::Value(format!(
						"invalid literal for int() with base 10: {}",
						py::str_repr(&data)
					))
				})?;
				self.add_object(Value::Integer(v))
			}
			"real" => {
				let data = self.get_data();
				let v = py::parse_float(&data).ok_or_else(|| {
					PlistError::Value(format!(
						"could not convert string to float: {}",
						py::str_repr(&data)
					))
				})?;
				self.add_object(Value::Real(v))
			}
			"string" => {
				let data = self.get_data();
				self.add_object(Value::String(data))
			}
			"data" => {
				let data = self.get_data();
				self.add_object(Value::Data(b64decode(&data)?))
			}
			"date" => {
				let data = self.get_data();
				self.add_object(Value::Date(date_from_string(&data)?))
			}
			_ => Ok(()),
		}
	}

	fn end_container(&mut self) -> Result<()> {
		// The stack can't be empty: every end matches a start.
		let (Some(value), Some(key)) = (self.stack.pop(), self.keys.pop()) else {
			return Err(PlistError::Value("pop from empty list".into()));
		};
		match (self.stack.last_mut(), key) {
			(Some(Value::Dict(d)), Some(key)) => {
				d.insert(key, value);
			}
			(Some(Value::Array(a)), None) => a.push(value),
			(None, _) => self.root = Some(value),
			_ => return Err(PlistError::Value("unexpected element".into())),
		}
		Ok(())
	}

	fn add_object(&mut self, value: Value) -> Result<()> {
		if self.current_key.is_some() {
			match self.stack.last_mut() {
				Some(Value::Dict(d)) => {
					let key = self.current_key.take().unwrap_or_default();
					d.insert(key, value);
				}
				Some(top) => return Err(unexpected(top)),
				None => return Err(PlistError::Value("unexpected element".into())),
			}
		} else if self.stack.is_empty() {
			self.root = Some(value);
		} else {
			match self.stack.last_mut() {
				Some(Value::Array(a)) => a.push(value),
				Some(top) => return Err(unexpected(top)),
				None => {}
			}
		}
		Ok(())
	}

	fn get_data(&mut self) -> String {
		let data = self.data.concat();
		self.data.clear();
		data
	}
}

fn unexpected(top: &Value) -> PlistError {
	PlistError::Value(format!("unexpected element: {top:?}"))
}

/// The text of an element before its first child (`element.text`).
pub fn element_text(element: &xml_reader::Element) -> Option<String> {
	let mut text: Option<String> = None;
	for content in &element.content {
		match content {
			Content::Text(t) => text.get_or_insert_with(String::new).push_str(t),
			Content::Element(_) => break,
		}
	}
	text
}

fn walk(target: &mut PlistTarget, element: &xml_reader::Element) -> Result<()> {
	target.start(&element.name)?;
	let mut has_children = false;
	for child in element.elements() {
		has_children = true;
		walk(target, child)?;
	}
	if !has_children {
		target.data.push(element_text(element).unwrap_or_default());
	}
	target.end(&element.name)
}

/// `fromtree`: converts an XML element to a value.
pub fn fromtree(tree: &xml_reader::Element) -> Result<Value> {
	let mut target = PlistTarget {
		stack: Vec::new(),
		keys: Vec::new(),
		current_key: None,
		root: None,
		data: Vec::new(),
	};
	walk(&mut target, tree)?;
	target
		.root
		.ok_or_else(|| PlistError::Value("No root set.".into()))
}

/// `loads`: reads a plist document.
pub fn loads(text: &str) -> Result<Value> {
	let root = xml_reader::parse(text.strip_prefix('\u{feff}').unwrap_or(text))
		.map_err(|e| PlistError::Xml(e.0))?;
	fromtree(&root)
}

/// `loads` of bytes: the document is decoded as an XML parser does it (see
/// [`decode_xml`]).
pub fn loads_bytes(data: &[u8]) -> Result<Value> {
	loads(&decode_xml(data)?)
}

fn decode_utf16(data: &[u8], big_endian: bool) -> Result<String> {
	let (pairs, rest) = data.as_chunks::<2>();
	if !rest.is_empty() {
		return Err(PlistError::Xml("truncated UTF-16 data".into()));
	}
	let units = pairs.iter().map(|&c| {
		if big_endian {
			u16::from_be_bytes(c)
		} else {
			u16::from_le_bytes(c)
		}
	});
	char::decode_utf16(units)
		.collect::<std::result::Result<String, _>>()
		.map_err(|e| PlistError::Xml(e.to_string()))
}

/// The `encoding` of an XML declaration at the start of ASCII-compatible
/// data.
fn declared_encoding(data: &[u8]) -> Option<String> {
	let head = data.get(..data.len().min(200))?;
	if !head.starts_with(b"<?xml") {
		return None;
	}
	let end = head.windows(2).position(|w| w == b"?>")?;
	let decl = std::str::from_utf8(&head[..end]).ok()?;
	let rest = &decl[decl.find("encoding")? + "encoding".len()..];
	let rest = rest.trim_start().strip_prefix('=')?.trim_start();
	let quote = rest.chars().next()?;
	let value = rest.get(1..)?;
	Some(value[..value.find(quote)?].to_ascii_lowercase())
}

/// Decodes an XML document the way an XML parser does: a UTF-8 or UTF-16
/// byte order mark, otherwise the encoding of the XML declaration (UTF-8,
/// UTF-16, ASCII or Latin-1), and UTF-8 by default.
pub fn decode_xml(data: &[u8]) -> Result<String> {
	if let Some(rest) = data.strip_prefix(b"\xef\xbb\xbf") {
		return String::from_utf8(rest.to_vec()).map_err(|e| PlistError::Xml(e.to_string()));
	}
	if let Some(rest) = data.strip_prefix(b"\xff\xfe") {
		return decode_utf16(rest, false);
	}
	if let Some(rest) = data.strip_prefix(b"\xfe\xff") {
		return decode_utf16(rest, true);
	}
	if data.starts_with(b"<\0?\0") {
		return decode_utf16(data, false);
	}
	if data.starts_with(b"\0<\0?") {
		return decode_utf16(data, true);
	}
	match declared_encoding(data).as_deref() {
		Some("latin-1" | "latin1" | "iso-8859-1" | "iso8859-1") => Ok(py::latin1_decode(data)),
		_ => String::from_utf8(data.to_vec()).map_err(|e| PlistError::Xml(e.to_string())),
	}
}

/// `dumps`: writes a plist document.
pub fn dumps(value: &Value, sort_keys: bool, pretty_print: bool) -> Result<String> {
	let mut root = Element::new("plist");
	root.set("version", "1.0");
	root.append(totree(
		value,
		TreeOptions {
			sort_keys,
			pretty_print,
			indent_level: 1,
		},
	)?);
	let mut out = String::from("<?xml version='1.0' encoding='UTF-8'?>");
	if pretty_print {
		out.push('\n');
	}
	out.push_str(
		"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">",
	);
	if pretty_print {
		out.push('\n');
	}
	out.push_str(&etree::tostring(&root, pretty_print, false).map_err(|e| PlistError::Xml(e.0))?);
	Ok(out)
}

#[cfg(test)]
mod tests {
	use super::*;

	fn dict(items: Vec<(&str, Value)>) -> Value {
		Value::Dict(items.into_iter().map(|(k, v)| (k.to_owned(), v)).collect())
	}

	#[test]
	fn target_doctest() -> Result<()> {
		let v = loads("<dict>    <key>something</key>    <string>blah</string></dict>")?;
		assert_eq!(v, dict(vec![("something", "blah".into())]));
		Ok(())
	}

	#[test]
	fn roundtrip() -> Result<()> {
		let value = dict(vec![
			("b", Value::Array(vec![1.into(), 2.5.into(), true.into()])),
			("a", Value::Data(b"<binary gunk>".to_vec())),
			("c", Value::Date(date_from_string("2020-01-02T03:04:05Z")?)),
			("d", dict(vec![("x", Value::Array(vec![]))])),
		]);
		let text = dumps(&value, true, true)?;
		// Checked against fontTools.misc.plistlib.dumps.
		assert_eq!(
			text,
			"<?xml version='1.0' encoding='UTF-8'?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n  <dict>\n    <key>a</key>\n    <data>\n    PGJpbmFyeSBndW5rPg==\n    </data>\n    <key>b</key>\n    <array>\n      <integer>1</integer>\n      <real>2.5</real>\n      <true/>\n    </array>\n    <key>c</key>\n    <date>2020-01-02T03:04:05Z</date>\n    <key>d</key>\n    <dict>\n      <key>x</key>\n      <array/>\n    </dict>\n  </dict>\n</plist>\n"
		);
		assert_eq!(loads(&text)?, value);
		Ok(())
	}

	#[test]
	fn numbers_compare_like_python() {
		assert_eq!(Value::Bool(true), Value::Integer(1));
		assert_eq!(Value::Integer(2), Value::Real(2.0));
		assert_ne!(Value::Integer(2), Value::String("2".into()));
	}

	#[test]
	fn base64_decode() -> Result<()> {
		assert_eq!(
			b64decode("\n  PGJp\n  bmFyeSBndW5rPg==\n  ")?,
			b"<binary gunk>"
		);
		assert!(b64decode("YQ").is_err());
		// Checked against binascii.a2b_base64 of Python 3.13.
		assert_eq!(b64decode("YQ==YQ==")?, b"a\x06\x10");
		assert_eq!(b64decode("YWI=Y")?, b"ab\x18");
		assert_eq!(b64decode("YQ===")?, b"a");
		assert!(b64decode("YQ==Y").is_err());
		assert!(b64decode("YQ=a").is_err());
		assert!(b64decode("YQ==YWI").is_err());
		Ok(())
	}
}