#file #derive #toml #derive-macro #macro #deserialization #derive-macros #deserialize #read #type

from_as_file

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

2 releases

0.1.1 Dec 22, 2021
0.1.0 Dec 13, 2020

#1253 in Rust patterns

Download history 46/week @ 2022-08-14 55/week @ 2022-08-21 50/week @ 2022-08-28 41/week @ 2022-09-04 42/week @ 2022-09-11 203/week @ 2022-09-18 87/week @ 2022-09-25 116/week @ 2022-10-02 56/week @ 2022-10-09 113/week @ 2022-10-16 81/week @ 2022-10-23 94/week @ 2022-10-30 72/week @ 2022-11-06 43/week @ 2022-11-13 40/week @ 2022-11-20 24/week @ 2022-11-27

184 downloads per month
Used in 4 crates (2 directly)

MIT license

6KB
124 lines

This crate is meant to be used in the from_as crate and may work as intended by itself.

Please use https://crates.io/crates/from_as

from_as_file

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

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.

Example

    #[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();
    }

Dependencies

~1.7–2.6MB
~59K SLoC