将 TOML 反序列化为具有值的枚举向量
Deserializing TOML into vector of enum with values
我正在尝试读取 TOML 文件以创建一个结构,该结构包含具有关联值的枚举向量。这是示例代码:
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate toml;
use std::fs::File;
use std::io::Read;
#[derive(Debug, Deserialize, PartialEq)]
struct Actor {
name: String,
actions: Vec<Actions>,
}
#[derive(Debug, Deserialize, PartialEq)]
enum Actions {
Wait(usize),
Move { x: usize, y: usize },
}
fn main() {
let input_file = "./sample_actor.toml";
let mut file = File::open(input_file).unwrap();
let mut file_content = String::new();
let _bytes_read = file.read_to_string(&mut file_content).unwrap();
let actor: Actor = toml::from_str(&file_content).unwrap();
println!("Read actor {:?}", actor);
}
Cargo.toml
[dependencies]
serde_derive = "1.0.10"
serde = "1.0.10"
toml = "0.4.2"
sample_actor.toml
name = "actor1"
actions = [move 3 4, wait 20, move 5 6]
我知道文件看起来 "wrong",但我不知道应该如何在 TOML 文件中编写操作,以便 crate 能够将它们识别为具有 X 个值的枚举。
当 运行 这个例子 cargo run
时我得到的错误如下:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { inner: ErrorInner { kind: NumberInvalid, line: Some(1), col: 11, message: "", key: [] } }', /checkout/src/libcore/result.rs:906:4
note: Run with `RUST_BACKTRACE=1` for a backtrace.
我知道我可能需要为我的枚举实现 FromStr
以将字符串转换为我的枚举,并且我简要地知道可以实现自定义反序列化器以特定方式反序列化,但我不确定这些部分如何组合在一起。
这似乎是一个使用 serde_json 而不是 TOML 的等效示例(当然是使用 JSON 文件而不是 TOML)。
JSON 代码版本:
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
use serde_json::Error;
use std::fs::File;
use std::io::Read;
#[derive(Debug, Serialize, Deserialize)]
enum Foo {
bar(u32),
baz { x: u32, y: u32 },
}
#[derive(Debug, Serialize, Deserialize)]
struct Address {
street: String,
city: String,
nums: Vec<Foo>,
}
fn main() {
/*
let address = Address {
street: "10 Downing Street".to_owned(),
city: "London".to_owned(),
nums : vec![Foo::bar(1), Foo::baz{x : 2, y : 3}],
};
// Serialize it to a JSON string.
let j = serde_json::to_string(&address).unwrap();
// Print, write to a file, or send to an HTTP server.
println!("{}", j);
*/
let input_file = "./sample_address.json";
let mut file = File::open(input_file).unwrap();
let mut file_content = String::new();
let _bytes_read = file.read_to_string(&mut file_content).unwrap();
let address: Address = serde_json::from_str(&file_content).unwrap();
println!("{:?}", address);
}
本例中的JSON数据read/written为:
Address { street: "10 Downing Street", city: "London", nums: [bar(1), baz { x: 2, y: 3 }] }
也许 TOML crate 不能支持我的用例?
Serde 有 lots of options for serializing enums。一个适合你的案例:
use serde::{Deserialize, Serialize}; // 1.0.117
use toml; // 0.5.7
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", content = "args")]
enum Actions {
Wait(usize),
Move { x: usize, y: usize },
}
fn main() {
let a_wait = Actions::Wait(5);
println!("{}", toml::to_string(&a_wait).unwrap());
let a_move = Actions::Move { x: 1, y: 1 };
println!("{}", toml::to_string(&a_move).unwrap());
}
type = "Wait"
args = 5
type = "Move"
[args]
x = 1
y = 1
我正在尝试读取 TOML 文件以创建一个结构,该结构包含具有关联值的枚举向量。这是示例代码:
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate toml;
use std::fs::File;
use std::io::Read;
#[derive(Debug, Deserialize, PartialEq)]
struct Actor {
name: String,
actions: Vec<Actions>,
}
#[derive(Debug, Deserialize, PartialEq)]
enum Actions {
Wait(usize),
Move { x: usize, y: usize },
}
fn main() {
let input_file = "./sample_actor.toml";
let mut file = File::open(input_file).unwrap();
let mut file_content = String::new();
let _bytes_read = file.read_to_string(&mut file_content).unwrap();
let actor: Actor = toml::from_str(&file_content).unwrap();
println!("Read actor {:?}", actor);
}
Cargo.toml
[dependencies]
serde_derive = "1.0.10"
serde = "1.0.10"
toml = "0.4.2"
sample_actor.toml
name = "actor1"
actions = [move 3 4, wait 20, move 5 6]
我知道文件看起来 "wrong",但我不知道应该如何在 TOML 文件中编写操作,以便 crate 能够将它们识别为具有 X 个值的枚举。
当 运行 这个例子 cargo run
时我得到的错误如下:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { inner: ErrorInner { kind: NumberInvalid, line: Some(1), col: 11, message: "", key: [] } }', /checkout/src/libcore/result.rs:906:4
note: Run with `RUST_BACKTRACE=1` for a backtrace.
我知道我可能需要为我的枚举实现 FromStr
以将字符串转换为我的枚举,并且我简要地知道可以实现自定义反序列化器以特定方式反序列化,但我不确定这些部分如何组合在一起。
这似乎是一个使用 serde_json 而不是 TOML 的等效示例(当然是使用 JSON 文件而不是 TOML)。
JSON 代码版本:
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
use serde_json::Error;
use std::fs::File;
use std::io::Read;
#[derive(Debug, Serialize, Deserialize)]
enum Foo {
bar(u32),
baz { x: u32, y: u32 },
}
#[derive(Debug, Serialize, Deserialize)]
struct Address {
street: String,
city: String,
nums: Vec<Foo>,
}
fn main() {
/*
let address = Address {
street: "10 Downing Street".to_owned(),
city: "London".to_owned(),
nums : vec![Foo::bar(1), Foo::baz{x : 2, y : 3}],
};
// Serialize it to a JSON string.
let j = serde_json::to_string(&address).unwrap();
// Print, write to a file, or send to an HTTP server.
println!("{}", j);
*/
let input_file = "./sample_address.json";
let mut file = File::open(input_file).unwrap();
let mut file_content = String::new();
let _bytes_read = file.read_to_string(&mut file_content).unwrap();
let address: Address = serde_json::from_str(&file_content).unwrap();
println!("{:?}", address);
}
本例中的JSON数据read/written为:
Address { street: "10 Downing Street", city: "London", nums: [bar(1), baz { x: 2, y: 3 }] }
也许 TOML crate 不能支持我的用例?
Serde 有 lots of options for serializing enums。一个适合你的案例:
use serde::{Deserialize, Serialize}; // 1.0.117
use toml; // 0.5.7
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", content = "args")]
enum Actions {
Wait(usize),
Move { x: usize, y: usize },
}
fn main() {
let a_wait = Actions::Wait(5);
println!("{}", toml::to_string(&a_wait).unwrap());
let a_move = Actions::Move { x: 1, y: 1 };
println!("{}", toml::to_string(&a_move).unwrap());
}
type = "Wait"
args = 5
type = "Move"
[args]
x = 1
y = 1