mod.rs 3.3 KB

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