| 1 | use derive_more::{Deref, DerefMut}; |
| 2 | |
| 3 | use schemars::JsonSchema; |
| 4 | use serde::{Deserialize, Serialize}; |
| 5 | use std::{ |
| 6 | borrow::{Borrow, Cow}, |
| 7 | sync::Arc, |
| 8 | }; |
| 9 | use util::arc_cow::ArcCow; |
| 10 | |
| 11 | |
| 12 | |
| 13 | #[derive(Deref, DerefMut, Eq, PartialEq, PartialOrd, Ord, Hash, Clone)] |
| 14 | pub struct SharedString(ArcCow<'static, str>); |
| 15 | |
| 16 | impl SharedString { |
| 17 | |
| 18 | pub const fn new_static(str: &'static str) -> Self { |
| 19 | Self(ArcCow::Borrowed(str)) |
| 20 | } |
| 21 | |
| 22 | |
| 23 | pub fn new(str: impl Into<Arc<str>>) -> Self { |
| 24 | SharedString(ArcCow::Owned(str.into())) |
| 25 | } |
| 26 | |
| 27 | |
| 28 | pub fn as_str(&self) -> &str { |
| 29 | &self.0 |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | impl JsonSchema for SharedString { |
| 34 | fn inline_schema() -> bool { |
| 35 | String::inline_schema() |
| 36 | } |
| 37 | |
| 38 | fn schema_name() -> Cow<'static, str> { |
| 39 | String::schema_name() |
| 40 | } |
| 41 | |
| 42 | fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema { |
| 43 | String::json_schema(generator) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl Default for SharedString { |
| 48 | fn default() -> Self { |
| 49 | Self(ArcCow::Owned(Arc::default())) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | impl AsRef<str> for SharedString { |
| 54 | fn as_ref(&self) -> &str { |
| 55 | &self.0 |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl Borrow<str> for SharedString { |
| 60 | fn borrow(&self) -> &str { |
| 61 | self.as_ref() |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | impl std::fmt::Debug for SharedString { |
| 66 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 67 | self.0.fmt(f) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | impl std::fmt::Display for SharedString { |
| 72 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 73 | write!(f, "{}", self.0.as_ref()) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | impl PartialEq<String> for SharedString { |
| 78 | fn eq(&self, other: &String) -> bool { |
| 79 | self.as_ref() == other |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | impl PartialEq<SharedString> for String { |
| 84 | fn eq(&self, other: &SharedString) -> bool { |
| 85 | self == other.as_ref() |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | impl PartialEq<str> for SharedString { |
| 90 | fn eq(&self, other: &str) -> bool { |
| 91 | self.as_ref() == other |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | impl<'a> PartialEq<&'a str> for SharedString { |
| 96 | fn eq(&self, other: &&'a str) -> bool { |
| 97 | self.as_ref() == *other |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | impl From<&SharedString> for SharedString { |
| 102 | fn from(value: &SharedString) -> Self { |
| 103 | value.clone() |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl From<SharedString> for Arc<str> { |
| 108 | fn from(val: SharedString) -> Self { |
| 109 | match val.0 { |
| 110 | ArcCow::Borrowed(borrowed) => Arc::from(borrowed), |
| 111 | ArcCow::Owned(owned) => owned, |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | impl<T: Into<ArcCow<'static, str>>> From<T> for SharedString { |
| 117 | fn from(value: T) -> Self { |
| 118 | Self(value.into()) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | impl From<SharedString> for String { |
| 123 | fn from(val: SharedString) -> Self { |
| 124 | val.0.to_string() |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | impl Serialize for SharedString { |
| 129 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 130 | where |
| 131 | S: serde::Serializer, |
| 132 | { |
| 133 | serializer.serialize_str(self.as_ref()) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | impl<'de> Deserialize<'de> for SharedString { |
| 138 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 139 | where |
| 140 | D: serde::Deserializer<'de>, |
| 141 | { |
| 142 | let s = String::deserialize(deserializer)?; |
| 143 | Ok(SharedString::from(s)) |
| 144 | } |
| 145 | } |