string.rs
1 // Copyright (c) 2025 ADnet Contributors 2 // This file is part of the AlphaVM library. 3 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at: 7 8 // http://www.apache.org/licenses/LICENSE-2.0 9 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 //! String representations for Governor types. 17 18 use crate::{ 19 console::{prelude::*, types::Field}, 20 gid::{GidId, GidStatus}, 21 proposal::ProposalStatus, 22 }; 23 use std::{fmt, str::FromStr}; 24 25 impl<N: Network> fmt::Display for GidId<N> { 26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 27 write!(f, "gid1{}", self.id()) 28 } 29 } 30 31 impl fmt::Display for GidStatus { 32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 33 match self { 34 GidStatus::Active => write!(f, "active"), 35 GidStatus::Frozen => write!(f, "frozen"), 36 GidStatus::Disabled => write!(f, "disabled"), 37 } 38 } 39 } 40 41 impl fmt::Display for ProposalStatus { 42 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 43 match self { 44 ProposalStatus::Active => write!(f, "active"), 45 ProposalStatus::Passed => write!(f, "passed"), 46 ProposalStatus::Rejected => write!(f, "rejected"), 47 ProposalStatus::Executed => write!(f, "executed"), 48 ProposalStatus::Expired => write!(f, "expired"), 49 ProposalStatus::Cancelled => write!(f, "cancelled"), 50 } 51 } 52 } 53 54 // ============================================================================ 55 // FromStr implementations 56 // ============================================================================ 57 58 impl<N: Network> FromStr for GidId<N> { 59 type Err = Error; 60 61 fn from_str(s: &str) -> Result<Self, Self::Err> { 62 // Parse "gid1{field}" format 63 let field_str = 64 s.strip_prefix("gid1").ok_or_else(|| anyhow!("Invalid GidId format: expected 'gid1' prefix"))?; 65 let field = Field::<N>::from_str(field_str).map_err(|e| anyhow!("Invalid GidId field: {}", e))?; 66 Ok(Self::new(field)) 67 } 68 } 69 70 impl FromStr for GidStatus { 71 type Err = Error; 72 73 fn from_str(s: &str) -> Result<Self, Self::Err> { 74 match s.to_lowercase().as_str() { 75 "active" => Ok(GidStatus::Active), 76 "frozen" => Ok(GidStatus::Frozen), 77 "disabled" => Ok(GidStatus::Disabled), 78 _ => Err(anyhow!("Invalid GidStatus: '{}'. Expected 'active', 'frozen', or 'disabled'", s)), 79 } 80 } 81 } 82 83 impl FromStr for ProposalStatus { 84 type Err = Error; 85 86 fn from_str(s: &str) -> Result<Self, Self::Err> { 87 match s.to_lowercase().as_str() { 88 "active" => Ok(ProposalStatus::Active), 89 "passed" => Ok(ProposalStatus::Passed), 90 "rejected" => Ok(ProposalStatus::Rejected), 91 "executed" => Ok(ProposalStatus::Executed), 92 "expired" => Ok(ProposalStatus::Expired), 93 "cancelled" => Ok(ProposalStatus::Cancelled), 94 _ => Err(anyhow!("Invalid ProposalStatus: '{}'", s)), 95 } 96 } 97 }