#write #type #read #read-write #traits #file #serde

from_as_file

Traits 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

#2450 in Encoding

Download history 618/week @ 2024-07-22 343/week @ 2024-07-29 749/week @ 2024-08-05 525/week @ 2024-08-12 501/week @ 2024-08-19 407/week @ 2024-08-26 437/week @ 2024-09-02 591/week @ 2024-09-09 374/week @ 2024-09-16 366/week @ 2024-09-23 389/week @ 2024-09-30 332/week @ 2024-10-07 268/week @ 2024-10-14 236/week @ 2024-10-21 131/week @ 2024-10-28 259/week @ 2024-11-04

895 downloads per month
Used in 2 crates

MIT license

7KB
125 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

~2.3–3.5MB
~72K SLoC