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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::fmt;
use std::fs::File;
use std::io;
use std::io::{BufReader, Read, Seek, Write};
use ndarray::ShapeError;
pub type Result<T> = ::std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
FinalFusion(ErrorKind),
Shape(ShapeError),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Error::*;
match *self {
FinalFusion(ref kind) => kind.fmt(f),
Shape(ref err) => err.fmt(f),
}
}
}
impl std::error::Error for Error {
fn description(&self) -> &str {
use self::Error::*;
match *self {
FinalFusion(ErrorKind::Format(ref desc)) => desc,
FinalFusion(ErrorKind::Io { ref desc, .. }) => desc,
Shape(ref err) => err.description(),
}
}
}
#[derive(Debug)]
pub enum ErrorKind {
Format(String),
Io { desc: String, error: io::Error },
}
impl ErrorKind {
pub fn io_error(desc: impl Into<String>, error: io::Error) -> Self {
ErrorKind::Io {
desc: desc.into(),
error,
}
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::ErrorKind::*;
match *self {
Format(ref desc) => write!(f, "{}", desc),
Io {
ref desc,
ref error,
} => write!(f, "{}: {}", desc, error),
}
}
}
impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Error {
Error::FinalFusion(kind)
}
}
pub trait ReadEmbeddings
where
Self: Sized,
{
fn read_embeddings<R>(read: &mut R) -> Result<Self>
where
R: Read + Seek;
}
pub trait ReadMetadata
where
Self: Sized,
{
fn read_metadata<R>(read: &mut R) -> Result<Self>
where
R: Read + Seek;
}
pub trait MmapEmbeddings
where
Self: Sized,
{
fn mmap_embeddings(read: &mut BufReader<File>) -> Result<Self>;
}
pub trait WriteEmbeddings {
fn write_embeddings<W>(&self, write: &mut W) -> Result<()>
where
W: Write + Seek;
}