1//! Helper utilities
2//!
34// TODO RPC: Consider replacing this with a derive-deftly template.
5//
6/// Define an `impl From<fromty> for toty`` that wraps its input as
7/// `toty::variant(Arc::new(e))``
8macro_rules! define_from_for_arc {
9 { $fromty:ty => $toty:ty [$variant:ident] } => {
10impl From<$fromty> for $toty {
11fn from(e: $fromty) -> $toty {
12Self::$variant(std::sync::Arc::new(e))
13 }
14 }
15 };
16}
17use std::ffi::{CStr, CString, NulError};
1819pub(crate) use define_from_for_arc;
2021/// A string that is guaranteed to be UTF-8 and NUL-terminated,
22/// for fast access as either type.
23//
24// TODO RPC: Rename so we can expose it more sensibly.
25#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
26pub struct Utf8CString {
27/// The body of this string.
28 ///
29 /// # Safety
30 ///
31 /// INVARIANT: This string must be valid UTF-8.
32 ///
33 /// (We do not _yet_ depend on this invariant for safety in our rust code, but we do promise in
34 /// our C ffi that it will hold.)
35string: Box<CStr>,
36}
3738impl AsRef<CStr> for Utf8CString {
39fn as_ref(&self) -> &CStr {
40&self.string
41 }
42}
4344impl AsRef<str> for Utf8CString {
45fn as_ref(&self) -> &str {
46// TODO: We might someday decide to implement this using unsafe methods, to avoid walking
47 // over the string to enforce properties that are already there.
48self.string.to_str().expect("Utf8CString was not UTF-8‽")
49 }
50}
5152// TODO: In theory we could have an unchecked version of this function, if we are 100%
53// sure that serde_json will reject every string that contains a NUL. But let's not do
54// that unless the NUL check shows up in profiles.
55impl TryFrom<String> for Utf8CString {
56type Error = NulError;
5758fn try_from(value: String) -> Result<Self, Self::Error> {
59// Safety: Since `value` is a `String`, it is guaranteed to be UTF-8.
60Ok(Utf8CString {
61 string: CString::new(value)?.into_boxed_c_str(),
62 })
63 }
64}
6566impl std::fmt::Display for Utf8CString {
67fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68let s: &str = self.as_ref();
69 std::fmt::Display::fmt(s, f)
70 }
71}
7273/// An error from trying to convert a byte-slice to a Utf8CString.
74#[derive(Clone, Debug, thiserror::Error)]
75enum Utf8CStringFromBytesError {
76/// The bytes contained a nul, so we can't convert into a nul-terminated string.
77#[error("Bytes contained 0")]
78Nul(#[from] NulError),
79/// The bytes were not value UTF-8
80#[error("Bytes were not utf-8.")]
81Utf8(#[from] std::str::Utf8Error),
82}
8384impl Utf8CString {
85/// Try to construct a new `Utf8CString` from a given byte slice.
86fn try_from_bytes(bytes: &[u8]) -> Result<Self, Utf8CStringFromBytesError> {
87let s: &str = std::str::from_utf8(bytes)?;
88Ok(s.to_owned().try_into()?)
89 }
90}
9192impl serde::Serialize for Utf8CString {
93fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
94where
95S: serde::Serializer,
96 {
97 serializer.serialize_str(self.as_ref())
98 }
99}
100impl<'de> serde::Deserialize<'de> for Utf8CString {
101fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
102where
103D: serde::Deserializer<'de>,
104 {
105/// Visitor to implement Deserialize for Utf8CString
106struct Visitor;
107impl<'de> serde::de::Visitor<'de> for Visitor {
108type Value = Utf8CString;
109110fn expecting(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
111 fmt.write_str("a UTF-8 string with no internal NULs")
112 }
113fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
114where
115E: serde::de::Error,
116 {
117 Utf8CString::try_from_bytes(v).map_err(|e| E::custom(e))
118 }
119fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
120where
121E: serde::de::Error,
122 {
123 Utf8CString::try_from(v.to_owned()).map_err(|e| E::custom(e))
124 }
125 }
126 deserializer.deserialize_str(Visitor)
127 }
128}
129130/// Ffi-related functionality for Utf8CStr
131#[cfg(feature = "ffi")]
132pub(crate) mod ffi {
133use std::ffi::c_char;
134135impl super::Utf8CString {
136/// Expose this Utf8CStr as a C string.
137pub(crate) fn as_ptr(&self) -> *const c_char {
138self.string.as_ptr()
139 }
140 }
141}
142143#[cfg(test)]
144/// Assert that s1 and s2 are both valid json, and parse to the same serde_json::Value.
145macro_rules! assert_same_json {
146 { $s1:expr, $s2:expr } => {
147let v1: serde_json::Value = serde_json::from_str($s1).unwrap();
148let v2: serde_json::Value = serde_json::from_str($s2).unwrap();
149assert_eq!(v1, v2);
150 }
151 }
152#[cfg(test)]
153pub(crate) use assert_same_json;