from_as_file

Crates.iofrom_as_file
lib.rsfrom_as_file
version0.2.0
sourcesrc
created_at2020-12-13 11:16:16.840352
updated_at2023-04-06 12:53:44.664603
descriptionTraits to Read and write types that implement serde Serialize and deserialize to files
homepage
repositoryhttps://github.com/sreeise/from_as
max_upload_size
id322379
size6,517
Sean Reeise (sreeise)

documentation

README

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();
    }
Commit count: 29

cargo fmt