mod.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. use serde::{de, Deserialize, Deserializer, Serialize};
  2. use sha2::{Digest, Sha256};
  3. use std::{fmt::Display, ops::Deref, str::FromStr};
  4. #[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq, Serialize)]
  5. /// A string with a max-length checked at compiled time
  6. pub struct MaxLengthString<const MAX_LENGTH: usize>(String);
  7. impl<const MAX_LENGTH: usize> PartialEq<str> for MaxLengthString<MAX_LENGTH> {
  8. fn eq(&self, other: &str) -> bool {
  9. self.0.eq(other)
  10. }
  11. }
  12. impl<const MAX_LENGTH: usize> FromStr for MaxLengthString<MAX_LENGTH> {
  13. type Err = Error;
  14. fn from_str(value: &str) -> Result<Self, Self::Err> {
  15. Self::new(value.to_owned())
  16. }
  17. }
  18. impl<const MAX_LENGTH: usize> From<&str> for MaxLengthString<MAX_LENGTH> {
  19. fn from(value: &str) -> Self {
  20. Self::new(value.to_owned()).expect("The string is too long")
  21. }
  22. }
  23. impl<const MAX_LENGTH: usize> Display for MaxLengthString<MAX_LENGTH> {
  24. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  25. write!(f, "{}", self.0)
  26. }
  27. }
  28. impl<const MAX_LENGTH: usize> MaxLengthString<MAX_LENGTH> {
  29. /// Creates a new instance of MaxLengthString
  30. pub fn new(value: String) -> Result<Self, Error> {
  31. if value.len() > MAX_LENGTH {
  32. Err(Error::TooLong(MAX_LENGTH))
  33. } else {
  34. Ok(Self(value))
  35. }
  36. }
  37. }
  38. impl<const MAX_LENGTH: usize> Deref for MaxLengthString<MAX_LENGTH> {
  39. type Target = String;
  40. fn deref(&self) -> &Self::Target {
  41. &self.0
  42. }
  43. }
  44. impl<'de, const MAX_LENGTH: usize> de::Deserialize<'de> for MaxLengthString<MAX_LENGTH> {
  45. fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
  46. where
  47. D: de::Deserializer<'de>,
  48. {
  49. <String as de::Deserialize>::deserialize(deserializer)
  50. .and_then(|inner| Self::new(inner).map_err(|e| de::Error::custom(e.to_string())))
  51. }
  52. }
  53. /// The AccountId data type
  54. pub type AccountId = MaxLengthString<64>;
  55. crate::BinaryId!(RevisionId, "tx");
  56. /// A generic ID wrapper
  57. ///
  58. /// This enum can be used whenever a human ID is passed and needs to be parsed and validated.
  59. #[derive(Debug)]
  60. pub enum AnyId {
  61. /// RevisionId
  62. Transaction(RevisionId),
  63. /// Payment
  64. Payment(PaymentId),
  65. /// Account ID
  66. Account(AccountId),
  67. }
  68. impl FromStr for AnyId {
  69. type Err = Error;
  70. fn from_str(value: &str) -> Result<Self, Self::Err> {
  71. if value.starts_with("tx") {
  72. if let Some(at) = value.find(':') {
  73. let (tx, pos) = value.split_at(at);
  74. return Ok(Self::Payment(PaymentId {
  75. transaction: tx.parse()?,
  76. position: (pos[1..]).parse()?,
  77. }));
  78. } else {
  79. return Ok(Self::Transaction(value.parse()?));
  80. }
  81. }
  82. Ok(Self::Account(value.parse()?))
  83. }
  84. }
  85. impl<'de> Deserialize<'de> for AnyId {
  86. fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
  87. where
  88. D: Deserializer<'de>,
  89. {
  90. let s = String::deserialize(deserializer)?;
  91. AnyId::from_str(&s).map_err(serde::de::Error::custom)
  92. }
  93. }
  94. mod binary;
  95. mod error;
  96. mod payment;
  97. pub use self::{error::Error, payment::PaymentId};