我如何在不使用命令行的情况下使用具有CLAP的Rust crate的功能?



新手,如果这是一个愚蠢的问题,我很抱歉。

我想在我的代码中使用和牛箱的功能。这个crate有命令行功能,所以我可以从命令行运行代码,但我似乎不能从我自己的代码中引用它。

我已经尝试了两个选项:

  1. 复制调用crate(带参数的结构体)时期望的输入'clap'
  2. 从crate中调用一个特定的函数

对于第2项我已经试过了:

use wagyu::cli::ethereum;
fn main() {
let m: String = String::from("sunny story shrimp absent valid today film floor month measure fatigue pet");

// Returns the address of the corresponding mnemonic.
let passphrase = "";
let pathway = "m/44'/60'/0'/0";
let address = ethereum::from_mnemonic(m, passphrase, pathway);

println!("phrase: {:?}", address);

当我尝试构建此代码时,我得到以下编译错误:

error[E0425]: cannot find function `from_mnemonic` in module `ethereum`
--> srcmain.rs:37:29
|
37 |     let address = ethereum::from_mnemonic::<>(s, passphrase, pathway);
|                             ^^^^^^^^^^^^^ not found in `ethereum`

但是我从检查以太坊的代码中知道。Rs文件中有一个名为'from_mnemon '的公共函数(在第88行定义)。

有人知道为什么我不能调用这个函数吗?或者,是否有一种简单的方法可以在不使用命令行接口的情况下使用具有clap依赖的crate ?

许多谢谢。

from_mnemonic函数应该这样调用:

use wagyu::cli::ethereum::EthereumWallet;
use wagyu_ethereum::network::Mainnet; 
use wagyu_ethereum::wordlist::English;
fn main() {
let mnemonic: String = String::from(
"sunny story shrimp absent valid today. film floor month measure fatigue pet"
);
let passphrase = "";
let path = "m/44'/60'/0'/0";
let wallet = EthereumWallet::from_mnemonic::<Mainnet, English>(
mnemonic,
Some(passphrase),
path
).unwrap()
}

但是wagyu::cli::ethereum::EthereumWallet不是pub,所以你不能简单地这样做。你必须从github下载wagyu的源代码并编辑它,以便wagyu::cli::ethereum::EthereumWallet将是公开的。

我认为你必须做的唯一改变是替换这个(在ethereum.rs):

struct EthereumWallet {

与这个:

pub struct EthereumWallet {

最新更新