Crates.io | derive_from_as |
lib.rs | derive_from_as |
version | 0.2.0 |
source | src |
created_at | 2020-12-13 11:21:31.50451 |
updated_at | 2023-04-06 12:55:13.121714 |
description | Traits to Read and write types that implement serde Serialize and deserialize to files |
homepage | |
repository | https://github.com/sreeise/from_as |
max_upload_size | |
id | 322380 |
size | 9,347 |
Rust derive macros for reading and writing files for types that implement serde. The derive macros implement the from_as_file traits.
The from_as_file crate provides two traits: FromFile and AsFile. FromFile is used for getting types from a file. AsFile is used for writing a types to a file.
The derive_from_as crate provides derive macros for these traits with the same names.
Currently, the only files types that can be used are json, yaml, and toml.
#[macro_use]
extern crate serde_derive;
use std::io::{Read, Write};
use std::convert::TryFrom;
use from_as::*;
#[derive(Debug, Deserialize, Serialize, AsFile, FromFile)]
struct Attribute {
name: String,
}
fn main() {
let attr = Attribute {
name: "attr_name".into()
};
// Write to the example directory.
attr.as_file("./examples/attr.json").unwrap();
let attr = Attribute::from_file("./examples/attr.json").unwrap();
println!("{:#?}", attr);
// For writing a prettified version.
attr.as_file_pretty("./examples/attr.json").uwnrap();
}