leo_ast/program/program_id.rs
1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use crate::Identifier;
18
19use core::fmt;
20use leo_span::Symbol;
21use serde::{Deserialize, Serialize};
22use snarkvm::{console::program::ProgramID, prelude::Network};
23
24/// An identifier for a program that is eventually deployed to the network.
25#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
26pub struct ProgramId {
27 /// The name of the program.
28 pub name: Identifier,
29 /// The network associated with the program.
30 pub network: Identifier,
31}
32
33impl fmt::Display for ProgramId {
34 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35 write!(f, "{}.{}", self.name, self.network)
36 }
37}
38
39impl<N: Network> From<&ProgramID<N>> for ProgramId {
40 fn from(program: &ProgramID<N>) -> Self {
41 Self { name: Identifier::from(program.name()), network: Identifier::from(program.network()) }
42 }
43}
44
45impl From<Identifier> for ProgramId {
46 fn from(name: Identifier) -> Self {
47 Self {
48 name,
49 network: Identifier { name: Symbol::intern("aleo"), span: Default::default(), id: Default::default() },
50 }
51 }
52}