0
点赞
收藏
分享

微信扫一扫

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


文章目录

  • ​​一、配置文件 ini​​
  • ​​二、时间 time​​
  • ​​三、uuid 生成唯一id​​
  • ​​四、base64​​
  • ​​五、base64 to image​​
  • ​​六、数据类型推导 infer​​

一、配置文件 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");


举报

相关推荐

0 条评论