leo_ast/program/
program_id.rs1use crate::{Identifier, NetworkName};
18
19use core::fmt;
20use leo_span::Symbol;
21use serde::{Deserialize, Serialize};
22use snarkvm::{
23 console::program::ProgramID,
24 prelude::{CanaryV0, MainnetV0, Network, Result, TestnetV0},
25};
26use std::str::FromStr;
27
28#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
30pub struct ProgramId {
31 pub name: Identifier,
33 pub network: Identifier,
35}
36
37impl ProgramId {
38 pub fn from_str_with_network(string: &str, network: NetworkName) -> Result<Self> {
40 match network {
41 NetworkName::MainnetV0 => ProgramID::<MainnetV0>::from_str(string).map(|id| (&id).into()),
42 NetworkName::TestnetV0 => ProgramID::<TestnetV0>::from_str(string).map(|id| (&id).into()),
43 NetworkName::CanaryV0 => ProgramID::<CanaryV0>::from_str(string).map(|id| (&id).into()),
44 }
45 }
46
47 pub fn to_address_string(&self, network: NetworkName) -> Result<String> {
49 match network {
50 NetworkName::MainnetV0 => {
51 ProgramID::<MainnetV0>::from_str(&self.to_string())?.to_address().map(|addr| addr.to_string())
52 }
53 NetworkName::TestnetV0 => {
54 ProgramID::<TestnetV0>::from_str(&self.to_string())?.to_address().map(|addr| addr.to_string())
55 }
56 NetworkName::CanaryV0 => {
57 ProgramID::<CanaryV0>::from_str(&self.to_string())?.to_address().map(|addr| addr.to_string())
58 }
59 }
60 }
61}
62
63impl fmt::Display for ProgramId {
64 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65 write!(f, "{}.{}", self.name, self.network)
66 }
67}
68
69impl<N: Network> From<&ProgramID<N>> for ProgramId {
70 fn from(program: &ProgramID<N>) -> Self {
71 Self { name: Identifier::from(program.name()), network: Identifier::from(program.network()) }
72 }
73}
74
75impl From<Identifier> for ProgramId {
76 fn from(name: Identifier) -> Self {
77 Self {
78 name,
79 network: Identifier { name: Symbol::intern("aleo"), span: Default::default(), id: Default::default() },
80 }
81 }
82}