location.rs
1 // Copyright (C) 2019-2025 ADnet Contributors 2 // This file is part of the ADL library. 3 4 // The ADL 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 ADL 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 ADL library. If not, see <https://www.gnu.org/licenses/>. 16 17 use adl_span::Symbol; 18 use itertools::Itertools; 19 use serde::{Deserialize, Serialize}; 20 use snarkvm::prelude::{Locator, Network}; 21 use std::fmt::Display; 22 23 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] 24 pub struct Location { 25 /// The program name. e.g. `credits`. 26 /// Note. This does not include the `.alpha` network suffix. 27 pub program: Symbol, 28 /// The absolute path to the item that this `Location` points to. 29 pub path: Vec<Symbol>, 30 } 31 32 impl Location { 33 pub fn new(program: Symbol, path: Vec<Symbol>) -> Location { 34 Location { program, path } 35 } 36 } 37 38 impl Display for Location { 39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 40 write!(f, "{}.alpha/{}", self.program, self.path.iter().format("::")) 41 } 42 } 43 44 impl<N: Network> From<Locator<N>> for Location { 45 fn from(locator: Locator<N>) -> Self { 46 Location { 47 program: Symbol::intern(&locator.program_id().name().to_string()), 48 path: vec![Symbol::intern(&locator.resource().to_string())], 49 } 50 } 51 }