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

macro derive_from_as

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

#76 in #serde-derive

Download history 143/week @ 2024-11-14 94/week @ 2024-11-21 162/week @ 2024-11-28 198/week @ 2024-12-05 233/week @ 2024-12-12 32/week @ 2024-12-19 59/week @ 2024-12-26 108/week @ 2025-01-02 96/week @ 2025-01-09 174/week @ 2025-01-16 164/week @ 2025-01-23 154/week @ 2025-01-30 215/week @ 2025-02-06 71/week @ 2025-02-13 249/week @ 2025-02-20 143/week @ 2025-02-27

693 downloads per month
Used in from_as

MIT license

16KB
249 lines

Derive macros for the from_as crate

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

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

from_as

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.

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