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
use std::{fmt, str::FromStr};

#[derive(Debug, thiserror::Error)]
#[error("invalid JSON-LD text direction `{0}`")]
pub struct InvalidDirection<T>(pub T);

impl<'a, T: ?Sized + ToOwned> InvalidDirection<&'a T> {
	pub fn into_owned(self) -> InvalidDirection<T::Owned> {
		InvalidDirection(self.0.to_owned())
	}
}

/// Internationalized string direction.
///
/// Specifies the direction used to read a string.
/// This can be either left-to-right (`"ltr"`) or right-to-left (`"rtl"`).
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Direction {
	/// Left-to-right direction.
	Ltr,

	/// Right-to-left direction.
	Rtl,
}

impl Direction {
	pub fn as_str(&self) -> &'static str {
		match self {
			Direction::Ltr => "ltr",
			Direction::Rtl => "rtl",
		}
	}

	pub fn into_str(self) -> &'static str {
		self.as_str()
	}
}

impl FromStr for Direction {
	type Err = InvalidDirection<String>;

	fn from_str(s: &str) -> Result<Self, Self::Err> {
		Self::try_from(s).map_err(InvalidDirection::into_owned)
	}
}

impl<'a> TryFrom<&'a str> for Direction {
	type Error = InvalidDirection<&'a str>;

	/// Convert the strings `"rtl"` and `"ltr"` into a `Direction`.
	#[inline(always)]
	fn try_from(name: &'a str) -> Result<Direction, InvalidDirection<&'a str>> {
		match name {
			"ltr" => Ok(Direction::Ltr),
			"rtl" => Ok(Direction::Rtl),
			_ => Err(InvalidDirection(name)),
		}
	}
}

impl fmt::Display for Direction {
	#[inline(always)]
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Direction::Ltr => write!(f, "ltr"),
			Direction::Rtl => write!(f, "rtl"),
		}
	}
}

#[cfg(feature = "serde")]
impl serde::Serialize for Direction {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: serde::Serializer,
	{
		self.as_str().serialize(serializer)
	}
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Direction {
	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
	where
		D: serde::Deserializer<'de>,
	{
		struct Visitor;

		impl<'de> serde::de::Visitor<'de> for Visitor {
			type Value = Direction;

			fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
				formatter.write_str("JSON-LD text direction")
			}

			fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
			where
				E: serde::de::Error,
			{
				v.parse().map_err(|e| E::custom(e))
			}
		}

		deserializer.deserialize_str(Visitor)
	}
}