Rust调用toml库读取toml文件

接前一期的toml文件,我这次用rust实现读取。我用的rust版本是1.41.0。程序写法可能与老的rust有差别。请使用老版本rust的读者注意。

use serde_derive::Deserialize;
use toml::value::*;

use std::fs::File;
use std::io::prelude::*;

#[derive(Deserialize)]
#[derive(Debug)]
struct Owner {
    name : String,
    organization : String,
    bio : String,
    dob : Datetime,
}

#[derive(Deserialize)]
#[derive(Debug)]
struct Database {
    server : String,
    ports : Array,
    connection_max : toml::Value,
    enabled : toml::Value,
}

#[derive(Deserialize)]
#[derive(Debug)]
struct Server {
    ip : String,
    dc : String,
}

#[derive(Deserialize)]
#[derive(Debug)]
struct Servers {
    alpha : Server,
    beta : Server,
}

#[derive(Deserialize)]
#[derive(Debug)]
struct Clients {
    data : Array,
}

#[derive(Deserialize)]
#[derive(Debug)]
struct Config {
    title : String,
    hosts : Array,
    owner : Owner,
    database : Database,
    servers : Servers,
    clients : Clients,
}

fn main() {
    let file_path = "sample.toml";
    let mut file = match File::open(file_path) {
        Ok(f) => f,
        Err(e) => panic!("no such file {} exception:{}", file_path, e)
    };
    let mut str_val = String::new();
    match file.read_to_string(&mut str_val) {
        Ok(s) => s
        ,
        Err(e) => panic!("Error Reading file: {}", e)
    };
    let config : Config = toml::from_str(&str_val).unwrap();

    println!("\"Title\" = {}", config.title);
    let ct = config.hosts.len();
    let mut p = 1;
    println!("\"hosts\" = [");
    for h in config.hosts {
        print!("\t");
        print!("{}",h.as_str().unwrap());
        if p < ct {
            p = p + 1;
            println!(",");
        } else {
            println!("");
        }
    }
    println!("]");
    println!("\"owner\".\"name\" = {}", config.owner.name);
    println!("\"owner\".\"organization\" = {}", config.owner.organization);
    println!("\"owner\".\"bio\" = {}", config.owner.bio);
    println!("\"owner\".\"dob\" = {}", config.owner.dob);
    println!("\"database\".\"server\" = {}", config.database.server);
    let ps = config.database.ports.len();
    p = 1;
    println!("\"database\".\"ports\" = [");
    for q in config.database.ports {
        print!("\t {}", q.as_integer().unwrap());
        if p < ps {
            p = p + 1;
            println!(",");
        } else {
            println!("");
        }
    }
    println!("]");
    println!("\"servers\".\"alpha\".\"ip\" = {}", config.servers.alpha.ip);
    println!("\"servers\".\"alpha\".\"dc\" = {}", config.servers.alpha.dc);
    println!("\"servers\".\"beta\".\"ip\" = {}", config.servers.beta.ip);
    println!("\"servers\".\"beta\".\"dc\" = {}", config.servers.beta.dc);
    let als = config.clients.data.len();
    p = 1;
    println!("\"clients\".\"data\" = [");
    for al in config.clients.data {
        if al.is_array() {
            let su = al.as_array().unwrap();
            let suc = su.len();
            let mut sp = 1;
            println!("\t[");
            for sai in su {
                if sai.is_str() {
                    print!("\t\t{}", sai.as_str().unwrap());
                } else {
                    print!("\t\t{}", sai.as_integer().unwrap());
                }
                if sp < suc {
                    sp = sp + 1;
                    println!(",");
                } else {
                    println!("");
                }                
            }
            print!("\t]");
            if p < als {
                p = p + 1;
                println!(",");
            } else {
                println!("");
            }
        }
    }
    println!("]");
}

Cargo.toml文件如下:

[package]
name = "toml_sample"
version = "0.1.0"
authors = ["许阳 <mksword@126.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
toml = "0.5.6"
serde_derive = "1.0.104"
serde = "1.0.104"
datetime = "0.4.7"

运行结果如下:

示例代码仓库

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值