Skip to main content

tuwunel_core/utils/string/
unquote.rs

1const QUOTE: char = '"';
2
3/// Slice a string between quotes
4pub trait Unquote<'a> {
5	/// Whether the input is quoted. If this is false the fallible methods of
6	/// this interface will fail.
7	fn is_quoted(&self) -> bool;
8
9	/// Unquotes a string. If the input is not quoted it is simply returned
10	/// as-is. If the input is partially quoted on either end that quote is not
11	/// removed.
12	fn unquote(&self) -> Option<&'a str>;
13
14	/// Unquotes a string. The input must be quoted on each side for Some to be
15	/// returned
16	fn unquote_infallible(&self) -> &'a str;
17}
18
19impl<'a> Unquote<'a> for &'a str {
20	#[inline]
21	fn unquote_infallible(&self) -> &'a str {
22		self.strip_prefix(QUOTE)
23			.unwrap_or(self)
24			.strip_suffix(QUOTE)
25			.unwrap_or(self)
26	}
27
28	#[inline]
29	fn unquote(&self) -> Option<&'a str> {
30		self.strip_prefix(QUOTE)
31			.and_then(|s| s.strip_suffix(QUOTE))
32	}
33
34	#[inline]
35	fn is_quoted(&self) -> bool { self.starts_with(QUOTE) && self.ends_with(QUOTE) }
36}