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
use super::lexical_form;
use std::borrow::{Borrow, ToOwned};
use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};

lexical_form! {
	/// Boolean.
	///
	/// See: <https://www.w3.org/TR/xmlschema-2/#boolean>
	ty: Boolean,

	/// Owned boolean.
	///
	/// See: <https://www.w3.org/TR/xmlschema-2/#boolean>
	buffer: BooleanBuf,

	/// Creates a new boolean from a string.
	///
	/// If the input string is ot a [valid XSD boolean](https://www.w3.org/TR/xmlschema-2/#boolean),
	/// an [`InvalidBoolean`] error is returned.
	new,

	/// Creates a new boolean from a string without checking it.
	///
	/// # Safety
	///
	/// The input string must be a [valid XSD boolean](https://www.w3.org/TR/xmlschema-2/#boolean).
	new_unchecked,

	value: crate::Boolean,
	error: InvalidBoolean,
	as_ref: as_boolean,
	parent_forms: {}
}

impl Boolean {
	pub fn value(&self) -> bool {
		matches!(&self.0, b"true" | b"1")
	}
}

impl PartialEq for Boolean {
	fn eq(&self, other: &Self) -> bool {
		self.value() == other.value()
	}
}

impl Eq for Boolean {}

impl Hash for Boolean {
	fn hash<H: Hasher>(&self, state: &mut H) {
		self.value().hash(state)
	}
}

impl Ord for Boolean {
	fn cmp(&self, other: &Self) -> Ordering {
		self.value().cmp(&other.value())
	}
}

impl PartialOrd for Boolean {
	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
		Some(self.cmp(other))
	}
}

impl From<bool> for BooleanBuf {
	fn from(b: bool) -> Self {
		if b {
			unsafe { BooleanBuf::new_unchecked(vec![b't', b'r', b'u', b'e']) }
		} else {
			unsafe { BooleanBuf::new_unchecked(vec![b'f', b'a', b'l', b's', b'e']) }
		}
	}
}

impl<'a> From<&'a Boolean> for bool {
	fn from(b: &'a Boolean) -> bool {
		b.value()
	}
}

impl From<BooleanBuf> for bool {
	fn from(b: BooleanBuf) -> bool {
		b.value()
	}
}

fn check_bytes(s: &[u8]) -> bool {
	matches!(s, b"true" | b"false" | b"0" | b"1")
}