-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfile_id.rs
More file actions
73 lines (60 loc) · 1.86 KB
/
file_id.rs
File metadata and controls
73 lines (60 loc) · 1.86 KB
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
use super::*;
// sqlite allows *signed* integers up to 8 bytes. But this constrains us to using only the positive
// half unless we cast the sign back and forth. If we had that many files in a directory we're going
// to run into bigger problems first.
type FileIdInner = u32;
/// Values file identifier
#[derive(Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Copy)]
pub struct FileId(FileIdInner);
impl Debug for FileId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(self, f)
}
}
impl std::str::FromStr for FileId {
type Err = std::num::ParseIntError;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
Ok(Self(FileIdInner::from_str_radix(s, 16)?))
}
}
impl TryFrom<&OsStr> for FileId {
type Error = anyhow::Error;
fn try_from(value: &OsStr) -> std::result::Result<Self, Self::Error> {
let file_id_str = value
.to_str()
.ok_or(anyhow!("to_str"))?
.strip_prefix(VALUES_FILE_NAME_PREFIX)
.ok_or(anyhow!("missing values file name prefix"))?;
let file_id = file_id_str.parse()?;
Ok(file_id)
}
}
impl FromSql for FileId {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
let int = FileIdInner::column_result(value)?;
Ok(Self(int))
}
}
impl ToSql for FileId {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
self.0.to_sql()
}
}
impl Display for FileId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(self.values_file_path().to_str().unwrap())
}
}
impl AsRef<FileId> for FileId {
fn as_ref(&self) -> &FileId {
self
}
}
impl FileId {
pub fn values_file_path(&self) -> PathBuf {
format!("{}{:08x}", VALUES_FILE_NAME_PREFIX, self.0).into()
}
pub fn random() -> Self {
Self(rand::random())
}
}