arbitrary_value.rs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. use std::fmt::Formatter;
  2. // trying to keep values at 3 bytes
  3. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  4. #[derive(Clone, Debug, PartialEq)]
  5. pub enum AttributeValue<'a> {
  6. Text(&'a str),
  7. Float32(f32),
  8. Float64(f64),
  9. Int32(i32),
  10. Int64(i64),
  11. Uint32(u32),
  12. Uint64(u64),
  13. Bool(bool),
  14. ColorRGB(u8, u8, u8),
  15. ColorRGBA(u8, u8, u8, u8),
  16. ColorHex(u32),
  17. ColorHexAlpha(u32, u8),
  18. Vec3Float(f32, f32, f32),
  19. Vec3Int(i32, i32, i32),
  20. Vec3Uint(u32, u32, u32),
  21. Vec4Float(f32, f32, f32, f32),
  22. Vec4Int(i32, i32, i32, i32),
  23. Vec4Uint(u32, u32, u32, u32),
  24. Bytes(&'a [u8]),
  25. Any(ArbitraryAttributeValue<'a>),
  26. }
  27. impl<'a> AttributeValue<'a> {
  28. pub fn is_truthy(&self) -> bool {
  29. match self {
  30. AttributeValue::Text(t) => *t == "true",
  31. AttributeValue::Bool(t) => *t,
  32. _ => false,
  33. }
  34. }
  35. pub fn is_falsy(&self) -> bool {
  36. match self {
  37. AttributeValue::Text(t) => *t == "false",
  38. AttributeValue::Bool(t) => !(*t),
  39. _ => false,
  40. }
  41. }
  42. }
  43. impl<'a> std::fmt::Display for AttributeValue<'a> {
  44. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  45. match self {
  46. AttributeValue::Text(a) => write!(f, "{}", a),
  47. AttributeValue::Float32(a) => write!(f, "{}", a),
  48. AttributeValue::Float64(a) => write!(f, "{}", a),
  49. AttributeValue::Int32(a) => write!(f, "{}", a),
  50. AttributeValue::Int64(a) => write!(f, "{}", a),
  51. AttributeValue::Uint32(a) => write!(f, "{}", a),
  52. AttributeValue::Uint64(a) => write!(f, "{}", a),
  53. AttributeValue::Bool(a) => write!(f, "{}", a),
  54. AttributeValue::ColorRGB(_, _, _) => todo!(),
  55. AttributeValue::ColorRGBA(_, _, _, _) => todo!(),
  56. AttributeValue::ColorHex(_) => todo!(),
  57. AttributeValue::ColorHexAlpha(_, _) => todo!(),
  58. AttributeValue::Vec3Float(_, _, _) => todo!(),
  59. AttributeValue::Vec3Int(_, _, _) => todo!(),
  60. AttributeValue::Vec3Uint(_, _, _) => todo!(),
  61. AttributeValue::Vec4Float(_, _, _, _) => todo!(),
  62. AttributeValue::Vec4Int(_, _, _, _) => todo!(),
  63. AttributeValue::Vec4Uint(_, _, _, _) => todo!(),
  64. AttributeValue::Bytes(_) => todo!(),
  65. AttributeValue::Any(_) => todo!(),
  66. }
  67. }
  68. }
  69. #[derive(Clone, Copy)]
  70. pub struct ArbitraryAttributeValue<'a> {
  71. pub value: &'a dyn std::any::Any,
  72. pub cmp: fn(&'a dyn std::any::Any, &'a dyn std::any::Any) -> bool,
  73. }
  74. impl PartialEq for ArbitraryAttributeValue<'_> {
  75. fn eq(&self, other: &Self) -> bool {
  76. (self.cmp)(self.value, other.value)
  77. }
  78. }
  79. impl std::fmt::Debug for ArbitraryAttributeValue<'_> {
  80. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  81. f.debug_struct("ArbitraryAttributeValue").finish()
  82. }
  83. }
  84. #[cfg(feature = "serialize")]
  85. impl<'a> serde::Serialize for ArbitraryAttributeValue<'a> {
  86. fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
  87. where
  88. S: serde::Serializer,
  89. {
  90. panic!("ArbitraryAttributeValue should not be serialized")
  91. }
  92. }
  93. #[cfg(feature = "serialize")]
  94. impl<'de, 'a> serde::Deserialize<'de> for &'a ArbitraryAttributeValue<'a> {
  95. fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error>
  96. where
  97. D: serde::Deserializer<'de>,
  98. {
  99. panic!("ArbitraryAttributeValue is not deserializable!")
  100. }
  101. }
  102. #[cfg(feature = "serialize")]
  103. impl<'de, 'a> serde::Deserialize<'de> for ArbitraryAttributeValue<'a> {
  104. fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error>
  105. where
  106. D: serde::Deserializer<'de>,
  107. {
  108. panic!("ArbitraryAttributeValue is not deserializable!")
  109. }
  110. }
  111. impl<'a> AttributeValue<'a> {
  112. pub fn as_text(&self) -> Option<&'a str> {
  113. match self {
  114. AttributeValue::Text(s) => Some(s),
  115. _ => None,
  116. }
  117. }
  118. pub fn as_float32(&self) -> Option<f32> {
  119. match self {
  120. AttributeValue::Float32(f) => Some(*f),
  121. _ => None,
  122. }
  123. }
  124. pub fn as_float64(&self) -> Option<f64> {
  125. match self {
  126. AttributeValue::Float64(f) => Some(*f),
  127. _ => None,
  128. }
  129. }
  130. pub fn as_int32(&self) -> Option<i32> {
  131. match self {
  132. AttributeValue::Int32(i) => Some(*i),
  133. _ => None,
  134. }
  135. }
  136. pub fn as_int64(&self) -> Option<i64> {
  137. match self {
  138. AttributeValue::Int64(i) => Some(*i),
  139. _ => None,
  140. }
  141. }
  142. pub fn as_uint32(&self) -> Option<u32> {
  143. match self {
  144. AttributeValue::Uint32(i) => Some(*i),
  145. _ => None,
  146. }
  147. }
  148. pub fn as_uint64(&self) -> Option<u64> {
  149. match self {
  150. AttributeValue::Uint64(i) => Some(*i),
  151. _ => None,
  152. }
  153. }
  154. pub fn as_bool(&self) -> Option<bool> {
  155. match self {
  156. AttributeValue::Bool(b) => Some(*b),
  157. _ => None,
  158. }
  159. }
  160. pub fn as_color_rgb(&self) -> Option<(u8, u8, u8)> {
  161. match self {
  162. AttributeValue::ColorRGB(r, g, b) => Some((*r, *g, *b)),
  163. _ => None,
  164. }
  165. }
  166. pub fn as_color_rgba(&self) -> Option<(u8, u8, u8, u8)> {
  167. match self {
  168. AttributeValue::ColorRGBA(r, g, b, a) => Some((*r, *g, *b, *a)),
  169. _ => None,
  170. }
  171. }
  172. pub fn as_color_hex(&self) -> Option<u32> {
  173. match self {
  174. AttributeValue::ColorHex(c) => Some(*c),
  175. _ => None,
  176. }
  177. }
  178. pub fn as_color_hex_alpha(&self) -> Option<(u32, u8)> {
  179. match self {
  180. AttributeValue::ColorHexAlpha(c, a) => Some((*c, *a)),
  181. _ => None,
  182. }
  183. }
  184. pub fn as_vec3_float(&self) -> Option<(f32, f32, f32)> {
  185. match self {
  186. AttributeValue::Vec3Float(x, y, z) => Some((*x, *y, *z)),
  187. _ => None,
  188. }
  189. }
  190. pub fn as_vec3_int(&self) -> Option<(i32, i32, i32)> {
  191. match self {
  192. AttributeValue::Vec3Int(x, y, z) => Some((*x, *y, *z)),
  193. _ => None,
  194. }
  195. }
  196. pub fn as_vec3_uint(&self) -> Option<(u32, u32, u32)> {
  197. match self {
  198. AttributeValue::Vec3Uint(x, y, z) => Some((*x, *y, *z)),
  199. _ => None,
  200. }
  201. }
  202. pub fn as_vec4_float(&self) -> Option<(f32, f32, f32, f32)> {
  203. match self {
  204. AttributeValue::Vec4Float(x, y, z, w) => Some((*x, *y, *z, *w)),
  205. _ => None,
  206. }
  207. }
  208. pub fn as_vec4_int(&self) -> Option<(i32, i32, i32, i32)> {
  209. match self {
  210. AttributeValue::Vec4Int(x, y, z, w) => Some((*x, *y, *z, *w)),
  211. _ => None,
  212. }
  213. }
  214. pub fn as_vec4_uint(&self) -> Option<(u32, u32, u32, u32)> {
  215. match self {
  216. AttributeValue::Vec4Uint(x, y, z, w) => Some((*x, *y, *z, *w)),
  217. _ => None,
  218. }
  219. }
  220. pub fn as_bytes(&self) -> Option<&[u8]> {
  221. match self {
  222. AttributeValue::Bytes(b) => Some(b),
  223. _ => None,
  224. }
  225. }
  226. pub fn as_any(&self) -> Option<&'a ArbitraryAttributeValue> {
  227. match self {
  228. AttributeValue::Any(a) => Some(a),
  229. _ => None,
  230. }
  231. }
  232. }
  233. #[test]
  234. fn test_attribute_value_size() {
  235. assert_eq!(std::mem::size_of::<AttributeValue<'_>>(), 24);
  236. }