#write #read #read-write #traits #type #macro-derive #serde

from_as

Traits and derive macros to Read and write types that implement serde Serialize and deserialize to files

3 unstable releases

0.2.0 Apr 6, 2023
0.1.1 Dec 22, 2021
0.1.0 Dec 13, 2020

#2171 in Encoding

Download history 611/week @ 2024-07-21 339/week @ 2024-07-28 739/week @ 2024-08-04 528/week @ 2024-08-11 497/week @ 2024-08-18 412/week @ 2024-08-25 437/week @ 2024-09-01 584/week @ 2024-09-08 368/week @ 2024-09-15 350/week @ 2024-09-22 381/week @ 2024-09-29 330/week @ 2024-10-06 267/week @ 2024-10-13 231/week @ 2024-10-20 126/week @ 2024-10-27 257/week @ 2024-11-03

889 downloads per month

MIT license

13KB
147 lines

from_as

Rust traits and derive macros for reading and writing files for types that implement serde.

Available on crates.io

from_as = "0.2.0"

This crate provides two traits: FromFile and AsFile. FromFile is used for getting types from a file. AsFile is used for writing types to a file.

The traits can be used for writing json, yaml, and toml files.

Example

    #[macro_use]
    extern crate serde;
    
    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();
    }

Dependencies

~2.3–3.5MB
~72K SLoC