arbitrary_value.rs 6.4 KB

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