leo_package/
network_name.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 leo_errors::{CliError, LeoError};
18
19use serde::{Deserialize, Serialize};
20use snarkvm::prelude::{CanaryV0, MainnetV0, Network, TestnetV0};
21use std::{fmt, str::FromStr};
22
23// Retrievable networks for an external program
24#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)]
25pub enum NetworkName {
26    #[serde(rename = "testnet")]
27    TestnetV0,
28    #[serde(rename = "mainnet")]
29    MainnetV0,
30    #[serde(rename = "canary")]
31    CanaryV0,
32}
33
34impl NetworkName {
35    pub fn id(&self) -> u16 {
36        match self {
37            NetworkName::TestnetV0 => TestnetV0::ID,
38            NetworkName::MainnetV0 => MainnetV0::ID,
39            NetworkName::CanaryV0 => CanaryV0::ID,
40        }
41    }
42}
43
44impl FromStr for NetworkName {
45    type Err = LeoError;
46
47    fn from_str(s: &str) -> Result<Self, LeoError> {
48        match s {
49            "testnet" => Ok(NetworkName::TestnetV0),
50            "mainnet" => Ok(NetworkName::MainnetV0),
51            "canary" => Ok(NetworkName::CanaryV0),
52            _ => Err(LeoError::CliError(CliError::invalid_network_name(s))),
53        }
54    }
55}
56
57impl fmt::Display for NetworkName {
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        match self {
60            NetworkName::TestnetV0 => write!(f, "testnet"),
61            NetworkName::MainnetV0 => write!(f, "mainnet"),
62            NetworkName::CanaryV0 => write!(f, "canary"),
63        }
64    }
65}