Trait Fetchable

Source
pub trait Fetchable {
    // Required methods
    fn deserialize_l<T: DeserializeOwned + Default + Fetchable>(
        f_path: &PathBuf,
    ) -> Result<T, Box<dyn Error>>;
    fn serialize_l(&self) -> Result<Vec<u8>, Box<dyn Error>>
       where Self: Serialize + Fetchable;

    // Provided methods
    fn deserialize_bin<T>(f_path: &PathBuf) -> Result<T, Box<dyn Error>>
       where T: DeserializeOwned + Default { ... }
    fn deserialize_json<T>(f_path: &PathBuf) -> Result<T, Box<dyn Error>>
       where T: DeserializeOwned + Default { ... }
    fn deserialize_ron<T>(f_path: &PathBuf) -> Result<T, Box<dyn Error>>
       where T: DeserializeOwned + Default { ... }
    fn serialize_bin(&self) -> Result<Vec<u8>, Box<dyn Error>>
       where Self: Serialize { ... }
    fn serialize_ron(&self) -> Result<Vec<u8>, Box<dyn Error>>
       where Self: Serialize { ... }
    fn serialize_json(&self) -> Result<Vec<u8>, Box<dyn Error>>
       where Self: Serialize { ... }
    fn save(&self, path: &PathBuf) -> Result<(), Box<dyn Error>>
       where Self: Serialize + Fetchable { ... }
    fn fetch_or_default<T>(
        file_path: &PathBuf,
    ) -> Result<(T, bool), Box<dyn Error>>
       where T: Default + DeserializeOwned + Fetchable { ... }
}
Expand description

§

§Fetch

Simple trait that can be implemented on structs/enums for serialisation to and from disk.

  • deserialize_l: impl to pick specific

  • serialize_l: ..

use std::env;
use std::error::Error;
use std::path::PathBuf;
use serde::*;
use serde::de::DeserializeOwned;
use fetch_file::Fetchable;

#[derive(Deserialize, Serialize)]
pub struct Config {
    setting1: usize,
    setting2: usize
}

impl Default for Config {
    fn default() -> Self {
        Config {
            setting1: 0,
            setting2: 5,
        }
    }
}

impl Fetchable for Config {
    fn deserialize_l<T>(f_path: &PathBuf) -> Result<T, Box<dyn Error>>
        where T: DeserializeOwned + Default + Fetchable {
        //Config::deserialize_ron(f_path)
        //Config::deserialize_json(f_path)
        Config::deserialize_bin(f_path)
     }

    fn serialize_l(&self) -> Result<Vec<u8>, Box<dyn Error>>
        where Self: serde::Serialize + Fetchable {
        //self.serialize_ron()
        //self.serialize_json()
        self.serialize_bin()
    }
}

fn main() -> std::result::Result<(),  Box<dyn Error>> {
    // Example directory
    let mut path = env::current_dir()?;
    // adding file name
    path.push("config.bin");
    // fetch or default will either open file from disk and deserialize
    // or return the default for Config and a boolean indicating the
    // config is default.
    let config: (Config, bool) = Config::fetch_or_default(&path)?;
    if config.1 {
        //config.0.save(&path);
    }
    let config = config.0;
    println!("Config: {}, {}", config.setting1, config.setting2);
    Ok(())
}

Required Methods§

Source

fn deserialize_l<T: DeserializeOwned + Default + Fetchable>( f_path: &PathBuf, ) -> Result<T, Box<dyn Error>>

§
§Parameters

f_path: Path to file to open /path/to/your/file.txt

Impl this method to define behavior.

T::deserialize_json

Source

fn serialize_l(&self) -> Result<Vec<u8>, Box<dyn Error>>
where Self: Serialize + Fetchable,

§
§Impl method

Impl this method to define behavior.

Provided Methods§

Source

fn deserialize_bin<T>(f_path: &PathBuf) -> Result<T, Box<dyn Error>>

§

Deserialize from bincode format.

§Parameters

f_path: Path to file to open /path/to/your/file.txt

§Panics

If File::open fails

Source

fn deserialize_json<T>(f_path: &PathBuf) -> Result<T, Box<dyn Error>>

§

Deserialize from Json format.

§Parameters

f_path: Path to file to open /path/to/your/file.txt

§Panics

Panics if File::open fails

Source

fn deserialize_ron<T>(f_path: &PathBuf) -> Result<T, Box<dyn Error>>

§

Deserialize from ron format.

§Parameters

f_path: Path to file to open /home/$USER/folder/file.txt

§Panics

Panics if File::open fails

Source

fn serialize_bin(&self) -> Result<Vec<u8>, Box<dyn Error>>
where Self: Serialize,

§
§Bin
Source

fn serialize_ron(&self) -> Result<Vec<u8>, Box<dyn Error>>
where Self: Serialize,

§
§RON
Source

fn serialize_json(&self) -> Result<Vec<u8>, Box<dyn Error>>
where Self: Serialize,

§
§Json
Source

fn save(&self, path: &PathBuf) -> Result<(), Box<dyn Error>>
where Self: Serialize + Fetchable,

§
§Save

Use this version of save when container cannot be implemented.

Source

fn fetch_or_default<T>(file_path: &PathBuf) -> Result<(T, bool), Box<dyn Error>>

§

Fetch an item serialized to file

§Parameters
  • file_path - Reference to path buffer of file of type T
§Returns
  • Result<(T, bool)> - Returns tuple containing T and a bool value indicating if a config was retrieved or default. True for default config use.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§