json_ld_syntax/
nullable.rs

1use std::fmt;
2
3/// Value that can be null.
4///
5/// The `Option` type is used in this crate to indicate values that
6/// may or may not be defined.
7/// Sometimes however,
8/// value can be explicitly defined as `null`,
9/// hence the need for this type.
10#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)]
11pub enum Nullable<T> {
12	/// Null value.
13	Null,
14
15	/// Some other value.
16	Some(T),
17}
18
19impl<T> Nullable<T> {
20	/// Checks if the value is `null`.
21	#[inline(always)]
22	pub fn is_null(&self) -> bool {
23		matches!(self, Nullable::Null)
24	}
25
26	/// Checks if the value is not `null`.
27	#[inline(always)]
28	pub fn is_some(&self) -> bool {
29		matches!(self, Nullable::Some(_))
30	}
31
32	/// Unwraps a non-null value.
33	///
34	/// Panics if the value is `null`.
35	#[inline(always)]
36	pub fn unwrap(self) -> T {
37		match self {
38			Nullable::Some(t) => t,
39			Nullable::Null => panic!("cannot unwrap null"),
40		}
41	}
42
43	/// Returns a nullabl reference to the inner value.
44	#[inline(always)]
45	pub fn as_ref(&self) -> Nullable<&T> {
46		match self {
47			Nullable::Null => Nullable::Null,
48			Nullable::Some(t) => Nullable::Some(t),
49		}
50	}
51
52	pub fn as_deref(&self) -> Nullable<&T::Target>
53	where
54		T: std::ops::Deref,
55	{
56		match self {
57			Self::Null => Nullable::Null,
58			Self::Some(t) => Nullable::Some(t),
59		}
60	}
61
62	/// Transform into an `Option` value.
63	#[inline(always)]
64	pub fn option(self) -> Option<T> {
65		match self {
66			Nullable::Null => None,
67			Nullable::Some(t) => Some(t),
68		}
69	}
70
71	/// Map the inner value using the given function.
72	#[inline(always)]
73	pub fn map<F, U>(self, f: F) -> Nullable<U>
74	where
75		F: FnOnce(T) -> U,
76	{
77		match self {
78			Nullable::Null => Nullable::Null,
79			Nullable::Some(t) => Nullable::Some(f(t)),
80		}
81	}
82
83	pub fn cast<U>(self) -> Nullable<U>
84	where
85		T: Into<U>,
86	{
87		match self {
88			Self::Null => Nullable::Null,
89			Self::Some(t) => Nullable::Some(t.into()),
90		}
91	}
92
93	pub fn unwrap_or(self, default: T) -> T {
94		match self {
95			Self::Null => default,
96			Self::Some(t) => t,
97		}
98	}
99
100	pub fn unwrap_or_default(self) -> T
101	where
102		T: Default,
103	{
104		match self {
105			Self::Null => T::default(),
106			Self::Some(t) => t,
107		}
108	}
109}
110
111impl<T> From<T> for Nullable<T> {
112	fn from(value: T) -> Self {
113		Self::Some(value)
114	}
115}
116
117impl<T> From<Option<T>> for Nullable<T> {
118	fn from(value: Option<T>) -> Self {
119		match value {
120			Some(t) => Self::Some(t),
121			None => Self::Null,
122		}
123	}
124}
125
126impl<'a, T: Clone> Nullable<&'a T> {
127	/// Clone the referenced inner value.
128	#[inline(always)]
129	pub fn cloned(&self) -> Nullable<T> {
130		match self {
131			Nullable::Null => Nullable::Null,
132			Nullable::Some(t) => Nullable::Some((*t).clone()),
133		}
134	}
135}
136
137impl<T: fmt::Display> fmt::Display for Nullable<T> {
138	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139		match self {
140			Self::Null => write!(f, "null"),
141			Self::Some(v) => v.fmt(f),
142		}
143	}
144}
145
146impl<T: contextual::DisplayWithContext<V>, V> contextual::DisplayWithContext<V> for Nullable<T> {
147	fn fmt_with(&self, vocabulary: &V, f: &mut fmt::Formatter) -> fmt::Result {
148		match self {
149			Self::Null => write!(f, "null"),
150			Self::Some(v) => v.fmt_with(vocabulary, f),
151		}
152	}
153}
154
155#[cfg(feature = "serde")]
156impl<T: serde::Serialize> serde::Serialize for Nullable<T> {
157	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
158	where
159		S: serde::Serializer,
160	{
161		match self {
162			Self::Null => serializer.serialize_none(),
163			Self::Some(t) => serializer.serialize_some(t),
164		}
165	}
166}
167
168#[cfg(feature = "serde")]
169impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for Nullable<T> {
170	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
171	where
172		D: serde::Deserializer<'de>,
173	{
174		Ok(Option::<T>::deserialize(deserializer)?.into())
175	}
176}
177
178#[cfg(feature = "serde")]
179impl<T> Nullable<T> {
180	pub fn optional<'de, D>(deserializer: D) -> Result<Option<Self>, D::Error>
181	where
182		T: serde::Deserialize<'de>,
183		D: serde::Deserializer<'de>,
184	{
185		use serde::Deserialize;
186		Ok(Some(Option::<T>::deserialize(deserializer)?.into()))
187	}
188}