123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- use serde::{de, Deserialize, Deserializer, Serialize};
- use sha2::{Digest, Sha256};
- use std::{fmt::Display, ops::Deref, str::FromStr};
- #[derive(
- Clone,
- Debug,
- Eq,
- Hash,
- Ord,
- PartialOrd,
- PartialEq,
- Serialize,
- borsh::BorshDeserialize,
- borsh::BorshSerialize,
- )]
- /// A string with a max-length checked at compiled time
- pub struct MaxLengthString<const MAX_LENGTH: usize>(String);
- impl<const MAX_LENGTH: usize> PartialEq<str> for MaxLengthString<MAX_LENGTH> {
- fn eq(&self, other: &str) -> bool {
- self.0.eq(other)
- }
- }
- impl<const MAX_LENGTH: usize> FromStr for MaxLengthString<MAX_LENGTH> {
- type Err = Error;
- fn from_str(value: &str) -> Result<Self, Self::Err> {
- Self::new(value.to_owned())
- }
- }
- impl<const MAX_LENGTH: usize> From<&str> for MaxLengthString<MAX_LENGTH> {
- fn from(value: &str) -> Self {
- Self::new(value.to_owned()).expect("The string is too long")
- }
- }
- impl<const MAX_LENGTH: usize> Display for MaxLengthString<MAX_LENGTH> {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "{}", self.0)
- }
- }
- impl<const MAX_LENGTH: usize> MaxLengthString<MAX_LENGTH> {
- /// Creates a new instance of MaxLengthString
- pub fn new(value: String) -> Result<Self, Error> {
- if value.len() > MAX_LENGTH {
- Err(Error::TooLong(MAX_LENGTH))
- } else {
- Ok(Self(value))
- }
- }
- }
- impl<const MAX_LENGTH: usize> Deref for MaxLengthString<MAX_LENGTH> {
- type Target = String;
- fn deref(&self) -> &Self::Target {
- &self.0
- }
- }
- impl<'de, const MAX_LENGTH: usize> de::Deserialize<'de> for MaxLengthString<MAX_LENGTH> {
- fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
- where
- D: de::Deserializer<'de>,
- {
- <String as de::Deserialize>::deserialize(deserializer)
- .and_then(|inner| Self::new(inner).map_err(|e| de::Error::custom(e.to_string())))
- }
- }
- /// The AccountId data type
- pub type AccountId = MaxLengthString<64>;
- crate::BinaryId!(TxId, "tx-");
- crate::BinaryId!(RevId, "rev-");
- /// A generic ID wrapper
- ///
- /// This enum can be used whenever a human ID is passed and needs to be parsed and validated.
- #[derive(Debug)]
- pub enum AnyId {
- /// TxId
- Transaction(TxId),
- /// Payment
- Payment(PaymentId),
- /// Account ID
- Account(AccountId),
- }
- impl FromStr for AnyId {
- type Err = Error;
- fn from_str(value: &str) -> Result<Self, Self::Err> {
- if value.starts_with("tx") {
- if let Some(at) = value.find(':') {
- let (tx, pos) = value.split_at(at);
- return Ok(Self::Payment(PaymentId {
- transaction: tx.parse()?,
- position: (pos[1..]).parse()?,
- }));
- } else {
- return Ok(Self::Transaction(value.parse()?));
- }
- }
- Ok(Self::Account(value.parse()?))
- }
- }
- impl<'de> Deserialize<'de> for AnyId {
- fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
- where
- D: Deserializer<'de>,
- {
- let s = String::deserialize(deserializer)?;
- AnyId::from_str(&s).map_err(serde::de::Error::custom)
- }
- }
- mod binary;
- mod error;
- mod payment;
- pub use self::{error::Error, payment::PaymentId};
|