@@ -35,6 +35,24 @@ pub enum SizeHint {
35
35
Maximum ( usize ) ,
36
36
}
37
37
38
+ impl SizeHint {
39
+ /// This function succeeds if `actual` is allowed according to the [SizeHint]. Otherwise, it
40
+ /// returns an error.
41
+ fn check_size ( & self , actual : usize ) -> anyhow:: Result < ( ) > {
42
+ match * self {
43
+ SizeHint :: Exact ( expected) if actual != expected => {
44
+ anyhow:: bail!( "File size mismatch: expected {expected} bytes, served {actual}" )
45
+ }
46
+ SizeHint :: Maximum ( limit) if actual > limit => {
47
+ anyhow:: bail!(
48
+ "File size exceeds limit: expected at most {limit} bytes, served {actual}"
49
+ )
50
+ }
51
+ _ => Ok ( ( ) ) ,
52
+ }
53
+ }
54
+ }
55
+
38
56
/// Download `url` to `file`. If the file already exists, this appends to it, as long
39
57
/// as the file pointed to by `url` is larger than it.
40
58
///
@@ -84,7 +102,7 @@ pub async fn get_to_writer(
84
102
. get ( CONTENT_LENGTH )
85
103
. context ( "Missing file size" ) ?;
86
104
let total_size: usize = total_size. to_str ( ) ?. parse ( ) . context ( "invalid size" ) ?;
87
- check_size_hint ( size_hint, total_size) ?;
105
+ size_hint. check_size ( total_size) ?;
88
106
89
107
let already_fetched_bytes = writer
90
108
. stream_position ( )
@@ -145,22 +163,6 @@ pub async fn get_to_writer(
145
163
Ok ( ( ) )
146
164
}
147
165
148
- /// This function succeeds if `actual` is allowed according to the [SizeHint]. Otherwise, it
149
- /// returns an error.
150
- fn check_size_hint ( hint : SizeHint , actual : usize ) -> anyhow:: Result < ( ) > {
151
- match hint {
152
- SizeHint :: Exact ( expected) if actual != expected => {
153
- anyhow:: bail!( "File size mismatch: expected {expected} bytes, served {actual}" )
154
- }
155
- SizeHint :: Maximum ( limit) if actual > limit => {
156
- anyhow:: bail!(
157
- "File size exceeds limit: expected at most {limit} bytes, served {actual}"
158
- )
159
- }
160
- _ => Ok ( ( ) ) ,
161
- }
162
- }
163
-
164
166
/// If a file exists, append to it. Otherwise, create a new file
165
167
async fn create_or_append ( path : impl AsRef < Path > ) -> io:: Result < File > {
166
168
match fs:: File :: create_new ( & path) . await {
0 commit comments