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
use crate::cardano_cli::wrapper::cli::api::{Address, Query, StakeAddress, Transaction};
use crate::cardano_cli::wrapper::cli::command::Root;
use std::path::PathBuf;
use std::process::Command;

pub mod api;
pub mod command;

/// Cardano Cli wrapper which allows to use cardano cli as Rust library.
/// It requires real `CardanoCli` to be installed on environment
pub struct Api {
    cardano_cli: PathBuf,
}

impl Api {
    /// Creates new Api object based on path to cardano cli
    #[must_use]
    pub fn new(cardano_cli: PathBuf) -> Self {
        Self { cardano_cli }
    }

    /// Address related sub commands
    #[must_use]
    pub fn address(&self) -> Address {
        let command = Command::new(self.cardano_cli.clone());
        let cardano_cli_command = Root::new(command);
        Address::new(cardano_cli_command.address())
    }

    /// Stake address related sub commands
    #[must_use]
    pub fn stake_address(&self) -> StakeAddress {
        let command = Command::new(self.cardano_cli.clone());
        let cardano_cli_command = Root::new(command);
        StakeAddress::new(cardano_cli_command.stake_address())
    }

    /// Transaction related sub commands
    #[must_use]
    pub fn transaction(&self) -> Transaction {
        let command = Command::new(self.cardano_cli.clone());
        let cardano_cli_command = Root::new(command);
        Transaction::new(cardano_cli_command.transaction())
    }

    /// Query related sub commands
    #[must_use]
    pub fn query(&self) -> Query {
        let command = Command::new(self.cardano_cli.clone());
        let cardano_cli_command = Root::new(command);
        Query::new(cardano_cli_command.query())
    }
}