首页 技术 正文
技术 2022年11月19日
0 收藏 701 点赞 2,431 浏览 3809 个字

[易学易懂系列|rustlang语言|零基础|快速入门|(28)|实战5:实现BTC价格转换工具]

项目实战

实战5:实现BTC价格转换工具

今天我们来开发一个简单的BTC实时价格转换工具。

我们首先创建一个目录:

cargo new btc_converter

我们用TDD方式来开发。

然后 我们先写一些测试代码。

在src/main.rs下面,增加代码如下:

#[cfg(test)]
mod tests {
use super::*; #[test]
fn test_convert_success() {
match convert_btc(1.2, "BTC", "USD") {
Ok(_) => assert!(true),
Err(_) => assert!(false),
}
}
#[test]
fn test_convert_success2() {
match convert_btc(2.1, "BTC", "MKD") {
Ok(_) => assert!(true),
Err(_) => assert!(false),
}
} #[test]
fn test_convert_error_wrong_from() {
match convert_btc(1.2, "wrongvalue", "USD") {
Ok(_) => assert!(false),
Err(_) => assert!(true),
}
} #[test]
fn test_convert_error_wrong_to() {
match convert_btc(1.2, "USD", "wrongvalue") {
Ok(_) => assert!(false),
Err(_) => assert!(true),
}
}
}

我们运行命令:

cargo test

结果肯定是不通过。

我现在我们来增加核心业务代码 :

const API_URL: &str = "https://apiv2.bitcoinaverage.com/convert/global";//错误信息
#[derive(From, Display, Debug)]
enum BtcError {
ApiError,
Reqwest(reqwest::Error),
}
//响应信息结构体
#[derive(Deserialize, Debug)]
struct BtcResponse {
time: String,
success: bool,
price: f64,
}//价格转换,直接调用相关API
fn convert_btc(amount: f64, from: &str, to: &str) -> Result<BtcResponse, BtcError> {
use BtcError::*;
println!("---convert_btc-----{:?},{:?}", from, to);
let client = reqwest::Client::new();
let request =
client
.get(API_URL)
.query(&[("from", from), ("to", to), ("amount", &amount.to_string())]);
let response_result: BtcResponse = request.send()?.json()?; if !response_result.success {
return Err(ApiError);
} return Ok(response_result);
}

然后,我们再跑一下测试用例。

现在应该都通过 了。

当然我们要引用相关包:

[dependencies]
reqwest = "0.9.12"
serde_derive = "1.0.89"
serde = "1.0.89"
serde_json = "1.0.39"
structopt = "0.2.15"
derive_more = "0.14.0"

src/main.rs完整代码如下:

#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate derive_more;
use reqwest;
use std::process::exit;
use structopt::StructOpt;fn main() {
let opt = Opt::from_args(); let response = match convert_btc(opt.amount, &opt.from, &opt.to) {
Ok(value) => value,
Err(e) => {
println!("A error occurred when try to get value from api");
if opt.verbose {
println!("Message: {} - Details: {:?}", e, e);
}
exit(1);
}
}; if opt.silent {
println!("{}", response.price);
} else {
println!("{} {}", response.price, &opt.to);
}
}
const API_URL: &str = "https://apiv2.bitcoinaverage.com/convert/global";
//错误信息
#[derive(From, Display, Debug)]
enum BtcError {
ApiError,
Reqwest(reqwest::Error),
}
//响应信息结构体
#[derive(Deserialize, Debug)]
struct BtcResponse {
time: String,
success: bool,
price: f64,
}#[derive(Debug, StructOpt)]
#[structopt(
name = "btc_converter",
about = "Get value of a btc value to a currency"
)]
struct Opt {
/// Set amount to convert to a currency or from a currency
#[structopt(default_value = "1")]
amount: f64,
/// Set the initial currency of
#[structopt(short = "f", long = "from", default_value = "BTC")]
from: String,
/// Set the final currency to convert
#[structopt(short = "t", long = "to", default_value = "USD")]
to: String,
/// Silent information about currency result
#[structopt(short = "s", long = "silent")]
silent: bool,
/// Verbose errors
#[structopt(short = "v", long = "verbose")]
verbose: bool,
}
fn convert_btc(amount: f64, from: &str, to: &str) -> Result<BtcResponse, BtcError> {
use BtcError::*;
println!("---convert_btc-----{:?},{:?}", from, to);
let client = reqwest::Client::new();
let request =
client
.get(API_URL)
.query(&[("from", from), ("to", to), ("amount", &amount.to_string())]);
let response_result: BtcResponse = request.send()?.json()?; if !response_result.success {
return Err(ApiError);
} return Ok(response_result);
}
#[cfg(test)]
mod tests {
use super::*; #[test]
fn test_convert_success() {
match convert_btc(1.2, "BTC", "USD") {
Ok(_) => assert!(true),
Err(_) => assert!(false),
}
}
#[test]
fn test_convert_success2() {
match convert_btc(2.1, "BTC", "MKD") {
Ok(_) => assert!(true),
Err(_) => assert!(false),
}
} #[test]
fn test_convert_error_wrong_from() {
match convert_btc(1.2, "wrongvalue", "USD") {
Ok(_) => assert!(false),
Err(_) => assert!(true),
}
} #[test]
fn test_convert_error_wrong_to() {
match convert_btc(1.2, "USD", "wrongvalue") {
Ok(_) => assert!(false),
Err(_) => assert!(true),
}
}
}

API地址:

https://apiv2.bitcoinaverage.com

以上,希望对你有用。

如果遇到什么问题,欢迎加入:rust新手群,在这里我可以提供一些简单的帮助,加微信:360369487,注明:博客园+rust
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898