fuels_code_gen/program_bindings/abigen/
abigen_target.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use std::{
    convert::TryFrom,
    env, fs,
    path::{Path, PathBuf},
    str::FromStr,
};

use fuel_abi_types::abi::full_program::FullProgramABI;
use proc_macro2::Ident;

use crate::error::{error, Error, Result};

#[derive(Debug, Clone)]
pub struct AbigenTarget {
    pub(crate) name: String,
    pub(crate) source: Abi,
    pub(crate) program_type: ProgramType,
}

impl AbigenTarget {
    pub fn new(name: String, source: Abi, program_type: ProgramType) -> Self {
        Self {
            name,
            source,
            program_type,
        }
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn source(&self) -> &Abi {
        &self.source
    }

    pub fn program_type(&self) -> ProgramType {
        self.program_type
    }
}

#[derive(Debug, Clone)]
pub struct Abi {
    pub(crate) path: Option<PathBuf>,
    pub(crate) abi: FullProgramABI,
}

impl Abi {
    pub fn load_from(path: impl AsRef<Path>) -> Result<Abi> {
        let path = Self::canonicalize_path(path.as_ref())?;

        let json_abi = fs::read_to_string(&path).map_err(|e| {
            error!(
                "failed to read `abi` file with path {}: {}",
                path.display(),
                e
            )
        })?;
        let abi = Self::parse_from_json(&json_abi)?;

        Ok(Abi {
            path: Some(path),
            abi,
        })
    }

    fn canonicalize_path(path: &Path) -> Result<PathBuf> {
        let current_dir = env::current_dir()
            .map_err(|e| error!("unable to get current directory: ").combine(e))?;

        let root = current_dir.canonicalize().map_err(|e| {
            error!(
                "unable to canonicalize current directory {}: ",
                current_dir.display()
            )
            .combine(e)
        })?;

        let path = root.join(path);

        if path.is_relative() {
            path.canonicalize().map_err(|e| {
                error!(
                    "unable to canonicalize file from working dir {} with path {}: {}",
                    env::current_dir()
                        .map(|cwd| cwd.display().to_string())
                        .unwrap_or_else(|err| format!("??? ({err})")),
                    path.display(),
                    e
                )
            })
        } else {
            Ok(path)
        }
    }

    fn parse_from_json(json_abi: &str) -> Result<FullProgramABI> {
        FullProgramABI::from_json_abi(json_abi)
            .map_err(|e| error!("malformed `abi`. Did you use `forc` to create it?: ").combine(e))
    }

    pub fn path(&self) -> Option<&PathBuf> {
        self.path.as_ref()
    }

    pub fn abi(&self) -> &FullProgramABI {
        &self.abi
    }
}

impl FromStr for Abi {
    type Err = Error;

    fn from_str(json_abi: &str) -> Result<Self> {
        let abi = Abi::parse_from_json(json_abi)?;

        Ok(Abi { path: None, abi })
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProgramType {
    Script,
    Contract,
    Predicate,
}

impl FromStr for ProgramType {
    type Err = Error;

    fn from_str(string: &str) -> std::result::Result<Self, Self::Err> {
        let program_type = match string {
            "Script" => ProgramType::Script,
            "Contract" => ProgramType::Contract,
            "Predicate" => ProgramType::Predicate,
            _ => {
                return Err(error!(
                    "`{string}` is not a valid program type. Expected one of: `Script`, `Contract`, `Predicate`"
                ))
            }
        };

        Ok(program_type)
    }
}

impl TryFrom<Ident> for ProgramType {
    type Error = syn::Error;

    fn try_from(ident: Ident) -> std::result::Result<Self, Self::Error> {
        ident
            .to_string()
            .as_str()
            .parse()
            .map_err(|e| Self::Error::new(ident.span(), e))
    }
}