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

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

#1903 in Encoding

Download history 590/week @ 2023-12-31 337/week @ 2024-01-07 595/week @ 2024-01-14 560/week @ 2024-01-21 535/week @ 2024-01-28 354/week @ 2024-02-04 478/week @ 2024-02-11 457/week @ 2024-02-18 403/week @ 2024-02-25 336/week @ 2024-03-03 423/week @ 2024-03-10 326/week @ 2024-03-17 567/week @ 2024-03-24 569/week @ 2024-03-31 285/week @ 2024-04-07 688/week @ 2024-04-14

2,126 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
~71K SLoC