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
use std::{path::Path, str::FromStr};

use crate::{run_scilla_fmt, Error, FieldList, TransitionList};

#[derive(Debug, PartialEq)]
pub struct Contract {
    pub name: String,
    pub init_params: FieldList,
    pub fields: FieldList,
    pub transitions: TransitionList,
}

impl FromStr for Contract {
    type Err = Error;

    fn from_str(sexp: &str) -> Result<Self, Self::Err> {
        let v = lexpr::from_str(sexp)?;
        let name = v["contr"][0]["cname"]["Ident"][0][1].to_string();
        let transitions = (&v["contr"][0]["ccomps"][0]).try_into()?;
        let init_params = (&v["contr"][0]["cparams"][0]).try_into()?;
        let fields = (&v["contr"][0]["cfields"][0]).try_into()?;
        Ok(Contract {
            name,
            transitions,
            init_params,
            fields,
        })
    }
}

impl Contract {
    pub fn from_path(contract_path: &Path) -> Result<Self, Error> {
        run_scilla_fmt(contract_path)?.replace("\\0", "").parse()
    }
}