value.rs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //! Generic Rust value representation for data from the database
  2. /// Generic Value representation of data from the any database
  3. #[derive(Clone, Debug, PartialEq)]
  4. pub enum Value {
  5. /// The value is a `NULL` value.
  6. Null,
  7. /// The value is a signed integer.
  8. Integer(i64),
  9. /// The value is a floating point number.
  10. Real(f64),
  11. /// The value is a text string.
  12. Text(String),
  13. /// The value is a blob of data
  14. Blob(Vec<u8>),
  15. }
  16. impl From<String> for Value {
  17. fn from(value: String) -> Self {
  18. Self::Text(value)
  19. }
  20. }
  21. impl From<&str> for Value {
  22. fn from(value: &str) -> Self {
  23. Self::Text(value.to_owned())
  24. }
  25. }
  26. impl From<&&str> for Value {
  27. fn from(value: &&str) -> Self {
  28. Self::Text(value.to_string())
  29. }
  30. }
  31. impl From<Vec<u8>> for Value {
  32. fn from(value: Vec<u8>) -> Self {
  33. Self::Blob(value)
  34. }
  35. }
  36. impl From<&[u8]> for Value {
  37. fn from(value: &[u8]) -> Self {
  38. Self::Blob(value.to_owned())
  39. }
  40. }
  41. impl From<u8> for Value {
  42. fn from(value: u8) -> Self {
  43. Self::Integer(value.into())
  44. }
  45. }
  46. impl From<i64> for Value {
  47. fn from(value: i64) -> Self {
  48. Self::Integer(value)
  49. }
  50. }
  51. impl From<u32> for Value {
  52. fn from(value: u32) -> Self {
  53. Self::Integer(value.into())
  54. }
  55. }
  56. impl From<bool> for Value {
  57. fn from(value: bool) -> Self {
  58. Self::Integer(if value { 1 } else { 0 })
  59. }
  60. }
  61. impl<T> From<Option<T>> for Value
  62. where
  63. T: Into<Value>,
  64. {
  65. fn from(value: Option<T>) -> Self {
  66. match value {
  67. Some(v) => v.into(),
  68. None => Value::Null,
  69. }
  70. }
  71. }