Rust actix-web服务(第三方库的使用ini time uuid base64 photon)

一、配置文件 ini

使用 ini
安装:cargo install -f cargo-edit
并执行:cargo add rust-ini
Cargo.toml文件中添加依赖

[dependencies]
actix-web = "3"
rust-ini = "0.16.0"

使用方法( 代码来源 ), 【其它方法】

use ini::Ini;

// 初始化,设置文件变量及属性,并保存文件
let mut conf = Ini::new();
conf.with_section(Some("User".to_owned()))
    .set("name", "Raspberry树莓")
    .set("value", "Pi");
conf.with_section(Some("Library".to_owned()))
    .set("name", "Sun Yat-sen U")
    .set("location", "Guangzhou=world");
conf.write_to_file("conf.ini").unwrap();

// 读取 ini 文件
let i = Ini::load_from_file("conf.ini").unwrap();
for (sec, prop) in i.iter() {
   println!("Section: {:?}", *sec);
   for (k, v) in prop.iter() {
       println!("{}:{}", *k, *v);
   }
}

// 读取 ini 文件,获取其中的变量
let i = Ini::load_from_file("conf.ini").unwrap();
let port = i.get_from(Some("server"),"port").unwrap();
let worker_num:usize = i.get_from_or(Some("process"),"workers","4").parse().unwrap();
let max_size = i.get_from_or(Some("log"),"max_size","134217728");

二、时间 time

Cargo.toml文件中添加依赖 【文档及其它方法使用】

[dependencies]
chrono = { version = "0.4", features = ["serde"] }
use chrono::{DateTime, Local, NaiveDateTime, Duration}

// 获取当前系统时间
let now = Local::now().naive_local();

// 获取当前系统时间
let run_time = now.format("%Y-%m-%d %H:%M:%S").to_string();

// 打印时间戳
println!("now={}",now.timestamp().to_owned());              //now=1605015910

// 时间格式化输出
let run_time = now.format("%Y-%m-%d %H:%M:%S").to_string(); //2020-11-10 13:45:10

// 字符串转时间
let end_time = NaiveDateTime::parse_from_str("2020-03-06 18:36:27", "%Y-%m-%d %H:%M:%S").unwrap_or(now);

三、uuid 生成唯一id

Cargo.toml文件中添加依赖【文档及其它方法使用】

[dependencies]
uuid = { version = "0.8", features = ["serde", "v4"] }
use uuid::Uuid;

// 生成id并使用
let my_uuid = Uuid::new_v4().to_string().replace("-","");

四、base64

base64 标准的编解码 【文档及其它方法使用】
Cargo.toml文件中添加依赖:

base64 = "0.13.0"

image-base64:【文档及其它方法使用】
Cargo.toml文件中添加依赖:

image-base64 = "0.1.0"
extern crate rustc_serialize;
extern crate regex;

use std::fs::File;
use rustc_serialize::base64::{FromBase64, ToBase64, MIME};
use rustc_serialize::hex::{ToHex};
use regex::Regex;
use std::io::Read;
use std::string::String;

pub fn get_file_type(hex: &str) -> &str {
    if Regex::new(r"^ffd8ffe0").unwrap().is_match(hex) { 
        return "jpeg" 
    } else if Regex::new(r"^89504e47").unwrap().is_match(hex) {  
        return "png" 
    } else if Regex::new(r"^47494638").unwrap().is_match(hex) { 
        return "gif"
    } 
    panic!("invalid file type")
}

pub fn to_base64(path: &str) -> String {
    let mut file = File::open(path).unwrap();
    let mut vec = Vec::new();
    let _ = file.read_to_end(&mut vec);
    let base64 = vec.to_base64(MIME);
    let hex = vec.to_hex();
    return format!("data:image/{};base64,{}", get_file_type(&hex), base64.replace("\r\n", ""));
}

pub fn from_base64(base64: String) -> Vec<u8> {
    let offset = base64.find(',').unwrap_or(base64.len())+1;
    let mut value = base64;
    value.drain(..offset);
    return value.from_base64().unwrap();
}

五、base64 to image

photon (base64 to image):【文档及其它方法使用】, 【源码】

Cargo.toml文件中添加依赖:

photon-rs = "0.2.0"
extern crate rustc_serialize;
extern crate regex;

use std::fs::File;
use rustc_serialize::base64::{FromBase64, ToBase64, MIME};
use rustc_serialize::hex::{ToHex};
use regex::Regex;
use std::io::Read;
use std::string::String;

pub fn get_file_type(hex: &str) -> &str {
    if Regex::new(r"^ffd8ffe0").unwrap().is_match(hex) { 
        return "jpeg" 
    } else if Regex::new(r"^89504e47").unwrap().is_match(hex) {  
        return "png" 
    } else if Regex::new(r"^47494638").unwrap().is_match(hex) { 
        return "gif"
    } 
    panic!("invalid file type")
}

pub fn to_base64(path: &str) -> String {
    let mut file = File::open(path).unwrap();
    let mut vec = Vec::new();
    let _ = file.read_to_end(&mut vec);
    let base64 = vec.to_base64(MIME);
    let hex = vec.to_hex();
    return format!("data:image/{};base64,{}", get_file_type(&hex), base64.replace("\r\n", ""));
}

pub fn from_base64(base64: String) -> Vec<u8> {
    let offset = base64.find(',').unwrap_or(base64.len())+1;
    let mut value = base64;
    value.drain(..offset);
    return value.from_base64().unwrap();
}

六、数据类型推导 infer

Cargo.toml文件中添加依赖:【文档及其它方法使用】

infer = "0.3.0"
let buf = [0xFF, 0xD8, 0xFF, 0xAA];
let kind = infer::get(&buf).expect("file type is known");

assert_eq!(kind.mime_type(), "image/jpeg");
assert_eq!(kind.extension(), "jpg");

拓展阅读:
模块结构:https://www.cnblogs.com/li-peng/p/13587910.html
本机ip地址:https://www.cnblogs.com/egmkang/p/11450741.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SongpingWang

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值