1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use crate::{Result, error_msg};
use crate::{DeserializeOwned, Serialize};

use crate::use_case::*;

use std::fs::File;
use std::io::{Read, Write};

/// Trait for write and read
///
/// All [LtFmIndex] in [crate::use_case] have this trait
pub trait IO: Serialize + DeserializeOwned {
    /// Write to file
    fn write_to_file(&self, file_path: &str) -> Result<()> {
        let file = {
            match File::create(file_path) {
                Ok(file) => file,
                Err(err) => error_msg!("{}", err),
            }
        };
        self.write_to(file)
    }
    /// Read from file
    fn read_from_file(file_path: &str) -> Result<Self> {
        let file = {
            match File::open(file_path) {
                Ok(file) => file,
                Err(err) => error_msg!("{}", err),
            }
        };
        Self::read_from(file)
    }
    /// Write to [Write]r
    fn write_to<W>(&self, writer: W) -> Result<()>
        where W: Write 
    {
        match bincode::serialize_into(writer, self) {
            Ok(_) => Ok(()),
            Err(err) => {
                error_msg!("{}", err)
            },
        }
    }
    /// Read from [Read]r
    fn read_from<R>(reader: R) -> Result<Self>
        where R: Read,
    {
        match bincode::deserialize_from::<R, Self>(reader) {
            Ok(v) => {
                Ok(v)
            },
            Err(err) => {
                error_msg!("{}", err)
            },
        }
    }
}

impl IO for LtFmIndexNO64 {}
impl IO for LtFmIndexNO128 {}
impl IO for LtFmIndexNN64 {}
impl IO for LtFmIndexNN128 {}
impl IO for LtFmIndexAO64 {}
impl IO for LtFmIndexAO128 {}
impl IO for LtFmIndexAN64 {}
impl IO for LtFmIndexAN128 {}
impl IO for LtFmIndexAll {}