[][src]Trait fetch_file::Fetchable

pub trait Fetchable {
    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
; 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
, { ... } }

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

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

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

Impl method

Impl this method to define behavior.

Loading content...

Provided methods

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

Deserialize from bincode format.

Parameters

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

Panics

If File::open fails

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

Deserialize from Json format.

Parameters

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

Panics

Panics if File::open fails

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

Deserialize from ron format.

Parameters

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

Panics

Panics if File::open fails

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

Save

Use this version of save when container cannot be implemented.

fn fetch_or_default<T>(file_path: &PathBuf) -> Result<(T, bool), Box<dyn Error>> where
    T: Default + DeserializeOwned + Fetchable

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.
Loading content...

Implementors

Loading content...