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
use std::path::Path;
use std::path::PathBuf;
use libimagentryutil::isa::Is;
use libimagentryutil::isa::IsKindHeaderPathProvider;
use libimagstore::store::Entry;
use libimagerror::errors::ErrorMsg as EM;
use toml::Value;
use toml_query::read::TomlValueReadExt;
use toml_query::delete::TomlValueDeleteExt;
use toml_query::insert::TomlValueInsertExt;
use failure::Fallible as Result;
use failure::Error;
use refstore::UniqueRefPathGenerator;
pub trait Ref {
fn is_ref(&self) -> Result<bool>;
fn get_hash(&self) -> Result<&str>;
fn make_ref<P: AsRef<Path>>(&mut self, hash: String, path: P) -> Result<()>;
fn get_path(&self) -> Result<PathBuf>;
fn hash_valid<RPG: UniqueRefPathGenerator>(&self) -> Result<bool>;
fn remove_ref(&mut self) -> Result<()>;
fn is_ref_to_file(&self) -> Result<bool> {
self.get_path().map(|p| p.is_file())
}
fn is_ref_to_dir(&self) -> Result<bool> {
self.get_path().map(|p| p.is_dir())
}
fn is_dangling(&self) -> Result<bool> {
self.get_path().map(|p| !p.exists())
}
}
provide_kindflag_path!(pub IsRef, "ref.is_ref");
impl Ref for Entry {
fn is_ref(&self) -> Result<bool> {
self.is::<IsRef>().map_err(Error::from)
}
fn get_hash(&self) -> Result<&str> {
self.get_header()
.read("ref.hash")
.map_err(Error::from)?
.ok_or_else(|| Error::from(EM::EntryHeaderFieldMissing("ref.hash")))
.and_then(|v| {
v.as_str().ok_or_else(|| Error::from(EM::EntryHeaderTypeError2("ref.hash", "string")))
})
}
fn make_ref<P: AsRef<Path>>(&mut self, hash: String, path: P) -> Result<()> {
let path_str : String = path
.as_ref()
.to_str()
.map(String::from)
.ok_or_else(|| EM::UTF8Error)?;
let _ = self.set_isflag::<IsRef>()?;
let hdr = self.get_header_mut();
hdr.insert("ref.path", Value::String(String::from(path_str)))?;
hdr.insert("ref.hash", Value::String(hash))?;
Ok(())
}
fn get_path(&self) -> Result<PathBuf> {
self.get_header()
.read("ref.path")
.map_err(Error::from)?
.ok_or_else(|| Error::from(EM::EntryHeaderFieldMissing("ref.path")))
.and_then(|v| {
v.as_str()
.ok_or_else(|| EM::EntryHeaderTypeError2("ref.path", "string"))
.map_err(Error::from)
})
.map(PathBuf::from)
}
fn hash_valid<RPG: UniqueRefPathGenerator>(&self) -> Result<bool> {
self.get_path()
.map(PathBuf::from)
.map_err(Error::from)
.and_then(|pb| RPG::unique_hash(pb))
.and_then(|h| Ok(h == self.get_hash()?))
}
fn remove_ref(&mut self) -> Result<()> {
let hdr = self.get_header_mut();
let _ = hdr.delete("ref.hash")?;
let _ = hdr.delete("ref.path")?;
let _ = hdr.delete("ref")?;
Ok(())
}
}