leo_ast/common/
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, Default, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)]
25pub enum NetworkName {
26    #[default]
27    #[serde(rename = "testnet")]
28    TestnetV0 = 1,
29    #[serde(rename = "mainnet")]
30    MainnetV0 = 0,
31    #[serde(rename = "canary")]
32    CanaryV0 = 2,
33}
34
35impl NetworkName {
36    pub fn id(&self) -> u16 {
37        match self {
38            NetworkName::TestnetV0 => TestnetV0::ID,
39            NetworkName::MainnetV0 => MainnetV0::ID,
40            NetworkName::CanaryV0 => CanaryV0::ID,
41        }
42    }
43}
44
45impl FromStr for NetworkName {
46    type Err = LeoError;
47
48    fn from_str(s: &str) -> Result<Self, LeoError> {
49        match s {
50            "testnet" => Ok(NetworkName::TestnetV0),
51            "mainnet" => Ok(NetworkName::MainnetV0),
52            "canary" => Ok(NetworkName::CanaryV0),
53            _ => Err(LeoError::CliError(CliError::invalid_network_name(s))),
54        }
55    }
56}
57
58impl fmt::Display for NetworkName {
59    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60        match self {
61            NetworkName::TestnetV0 => write!(f, "testnet"),
62            NetworkName::MainnetV0 => write!(f, "mainnet"),
63            NetworkName::CanaryV0 => write!(f, "canary"),
64        }
65    }
66}