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
//! The error of building an encrypted backend.
use thiserror::Error;
use crate::error::SmbError;
/// Why [`EncryptedBackendBuilder::build`](crate::EncryptedBackendBuilder::build)
/// failed.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum EncryptionError {
/// A setting is outside its range, or cannot go with another setting.
/// `setting` names the builder method.
#[error("invalid setting `{setting}`")]
InvalidSetting { setting: &'static str },
/// A setting the builder was given differs from the share's. `setting`
/// names the builder method.
#[error("setting `{setting}` differs from the share's")]
SettingMismatch { setting: &'static str },
/// The key is not the share's: the marker does not verify under it, or
/// [`restore_marker`](crate::EncryptedBackendBuilder::restore_marker)
/// found nothing in the share that the key authenticates. A marker with a
/// changed byte after its version byte also gives this error.
#[error("the key does not match the share")]
KeyMismatch,
/// The root of the inner backend holds entries and no marker.
/// [`restore_marker`](crate::EncryptedBackendBuilder::restore_marker)
/// writes a new one.
#[error("the share has entries and no marker")]
MarkerMissing,
/// The marker is not a file of the right size, does not start with the
/// marker magic, or holds field values outside their ranges. A crash
/// during the first build can leave a short marker. Deleting a damaged
/// marker lets
/// [`restore_marker`](crate::EncryptedBackendBuilder::restore_marker)
/// write a new one.
#[error("the marker is not valid")]
MarkerInvalid,
/// The marker has a format version this release does not read.
#[error("format version {version} is not supported")]
UnsupportedFormat { version: u8 },
/// The inner backend failed.
#[error("the inner backend failed")]
Backend(#[source] SmbError),
/// The operating system's random generator failed.
#[error("the random generator failed")]
Random(#[source] getrandom::Error),
}
#[cfg(test)]
mod tests {
use std::error::Error;
use super::*;
#[test]
fn display_texts() {
assert_eq!(
EncryptionError::InvalidSetting {
setting: "block_size"
}
.to_string(),
"invalid setting `block_size`"
);
assert_eq!(
EncryptionError::SettingMismatch {
setting: "encrypt_names"
}
.to_string(),
"setting `encrypt_names` differs from the share's"
);
assert_eq!(
EncryptionError::UnsupportedFormat { version: 7 }.to_string(),
"format version 7 is not supported"
);
assert_eq!(
EncryptionError::Backend(SmbError::DiskFull).to_string(),
"the inner backend failed"
);
assert_eq!(
EncryptionError::Random(getrandom::Error::UNEXPECTED).to_string(),
"the random generator failed"
);
}
#[test]
fn backend_and_random_errors_are_sources() {
let backend = EncryptionError::Backend(SmbError::DiskFull);
assert_eq!(backend.source().unwrap().to_string(), "disk full");
let random = EncryptionError::Random(getrandom::Error::UNEXPECTED);
assert!(random.source().is_some(), "the random error has a source");
}
}