leo_ast/common/location.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 itertools::Itertools;
18use leo_span::Symbol;
19use serde::{Deserialize, Serialize};
20use snarkvm::prelude::{Locator, Network};
21use std::fmt::Display;
22
23#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
24pub struct Location {
25 /// The program name. e.g. `credits`.
26 /// Note. This does not include the `.aleo` 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
32impl Location {
33 pub fn new(program: Symbol, path: Vec<Symbol>) -> Location {
34 Location { program, path }
35 }
36}
37
38impl Display for Location {
39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40 write!(f, "{}.aleo/{}", self.program, self.path.iter().format("::"))
41 }
42}
43
44impl<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}