actor.rs
1 use candid::{CandidType, Principal}; 2 use derive_more::{AsRef, Display, From, Into}; 3 use ic_stable_structures::{storable::Bound, Storable}; 4 use serde::{Deserialize, Serialize}; 5 6 pub mod account; 7 pub mod id; 8 9 pub use id::{ActorId, ActorIdError}; 10 11 #[derive( 12 CandidType, 13 Clone, 14 Copy, 15 Serialize, 16 Deserialize, 17 Debug, 18 Hash, 19 Eq, 20 PartialEq, 21 PartialOrd, 22 Ord, 23 AsRef, 24 From, 25 Display, 26 Into, 27 )] 28 pub struct ActorPrincipal(Principal); 29 30 impl Storable for ActorPrincipal { 31 fn to_bytes(&self) -> std::borrow::Cow<[u8]> { 32 self.0.to_bytes() 33 } 34 35 fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self { 36 Self(Principal::from_bytes(bytes)) 37 } 38 39 const BOUND: Bound = Bound::Bounded { 40 max_size: Principal::MAX_LENGTH_IN_BYTES as u32, 41 is_fixed_size: false, 42 }; 43 } 44 45 /// New type for actor name up to 50 characters 46 #[derive( 47 CandidType, 48 Clone, 49 Serialize, 50 Deserialize, 51 Debug, 52 Default, 53 Hash, 54 Eq, 55 PartialEq, 56 PartialOrd, 57 Ord, 58 AsRef, 59 From, 60 Display, 61 Into, 62 )] 63 pub struct ActorName(String); 64 65 impl Storable for ActorName { 66 fn to_bytes(&self) -> std::borrow::Cow<[u8]> { 67 self.0.to_bytes() 68 } 69 70 fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self { 71 Self(String::from_bytes(bytes)) 72 } 73 74 const BOUND: Bound = Bound::Bounded { 75 max_size: 200, 76 is_fixed_size: false, 77 }; 78 } 79 80 impl ActorName { 81 pub fn new<T: AsRef<str>>(name: T) -> Result<Self, ActorNameError> { 82 let name = name.as_ref(); 83 if name.chars().count() > 50 { 84 return Err(ActorNameError::TooLong); 85 } 86 Ok(ActorName(name.to_string())) 87 } 88 } 89 90 #[derive(Debug, Clone, PartialEq, Eq)] 91 pub enum ActorNameError { 92 TooLong, 93 }