如何解决如何在不使用命令行的情况下使用具有 CLAP 的 Rust crate 的功能?
这里是新手,如果这是一个愚蠢的问题,请见谅。
我想在我的代码中使用 wagyu 板条箱的功能。这个箱子有命令行功能,所以我可以从命令行运行代码,但我似乎无法从我自己的代码中引用它。
我尝试了 2 个选项:
- 在调用 crate 时复制期望的输入“clap”(带参数的结构)
- 从 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`
--> src\main.rs:37:29
|
37 | let address = ethereum::from_mnemonic::<>(s,pathway);
| ^^^^^^^^^^^^^ not found in `ethereum`
但我通过检查 ethereum.rs 文件中的代码知道有一个名为“from_mnemonic”的公共函数(在第 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 {
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。