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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use std::fs;
use std::fmt;
use std::time;
use std::error;
use std::hash::{
Hash,
Hasher
};
use std::collections::hash_map::DefaultHasher;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EntityTag {
pub weak: bool,
tag: String,
}
impl EntityTag {
pub fn new(weak: bool, tag: String) -> Self {
assert!(tag.is_ascii());
EntityTag { weak, tag }
}
pub fn weak(tag: String) -> Self {
Self::new(true, tag)
}
pub fn strong(tag: String) -> Self {
Self::new(false, tag)
}
pub fn from_file_meta(metadata: &fs::Metadata) -> Self {
let tag = match metadata.modified().map(|modified| modified.duration_since(time::UNIX_EPOCH).expect("Modified is earlier than time::UNIX_EPOCH!")) {
Ok(modified) => format!("{}.{}-{}", modified.as_secs(), modified.subsec_nanos(), metadata.len()),
_ => format!("{}", metadata.len())
};
Self {
weak: true,
tag
}
}
pub fn from_hash(bytes: &[u8]) -> Self {
let mut hasher = DefaultHasher::default();
bytes.hash(&mut hasher);
let tag = format!("{}-{}", bytes.len(), hasher.finish());
Self {
weak: false,
tag
}
}
pub fn tag(&self) -> &str {
self.tag.as_ref()
}
pub fn set_tag(&mut self, tag: String) {
self.tag = tag
}
pub fn strong_eq(&self, other: &EntityTag) -> bool {
!self.weak && !other.weak && self.tag == other.tag
}
pub fn weak_eq(&self, other: &EntityTag) -> bool {
self.tag == other.tag
}
pub fn strong_ne(&self, other: &EntityTag) -> bool {
!self.strong_eq(other)
}
pub fn weak_ne(&self, other: &EntityTag) -> bool {
!self.weak_eq(other)
}
}
impl fmt::Display for EntityTag {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.weak {
true => write!(f, "W/\"{}\"", self.tag),
false => write!(f, "\"{}\"", self.tag),
}
}
}
#[derive(Debug)]
pub enum ParseError {
InvalidFormat,
NotAscii
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ParseError::InvalidFormat => write!(f, "EntityTag uses invalid format"),
ParseError::NotAscii => write!(f, "EntityTag uses non-ASCII characters")
}
}
}
impl error::Error for ParseError {
fn description(&self) -> &str {
match self {
ParseError::InvalidFormat => "EntityTag uses invalid format",
ParseError::NotAscii => "EntityTag uses non-ASCII characters"
}
}
}
impl std::str::FromStr for EntityTag {
type Err = ParseError;
fn from_str(text: &str) -> Result<EntityTag, ParseError> {
let len = text.len();
let slice = &text[..];
if !slice.ends_with('"') || len < 2 {
return Err(ParseError::InvalidFormat);
}
if slice.starts_with('"') {
let slice = &slice[1..len-1];
match slice.is_ascii() {
true => Ok(EntityTag::strong(slice.to_string())),
false => Err(ParseError::NotAscii)
}
} else if len >= 4 && slice.starts_with("W/\"") {
let slice = &slice[3..len-1];
match slice.is_ascii() {
true => Ok(EntityTag::weak(slice.to_string())),
false => Err(ParseError::NotAscii)
}
} else {
Err(ParseError::InvalidFormat)
}
}
}