Skip to content

Rust trait and derive macro for reading and writing files for types that implement serde

License

Notifications You must be signed in to change notification settings

sreeise/from_as

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Rust trait and derive macro for reading and writing files for types that implement serde

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages