Use a specific error type for each invalid version error

This commit is contained in:
Rodolphe Bréard 2024-06-19 17:15:53 +02:00
parent 44cc9179ca
commit 4889d6b9b2
2 changed files with 6 additions and 4 deletions

View file

@ -32,8 +32,10 @@ pub enum Error {
ParsingEncodedDataInvalidPartLen(usize, usize), ParsingEncodedDataInvalidPartLen(usize, usize),
#[error("parsing error: encoded data: invalid timestamp: {0:?}")] #[error("parsing error: encoded data: invalid timestamp: {0:?}")]
ParsingEncodedDataInvalidTimestamp(Vec<u8>), ParsingEncodedDataInvalidTimestamp(Vec<u8>),
#[error("parsing error: encoded data: invalid version")] #[error("parsing error: encoded data: invalid IKML version")]
ParsingEncodedDataInvalidVersion, ParsingEncodedDataInvalidIkmlVersion,
#[error("parsing error: encoded data: invalid encrypted data version")]
ParsingEncodedDataInvalidEncVersion,
#[error("parsing error: scheme: {0}: unknown scheme")] #[error("parsing error: scheme: {0}: unknown scheme")]
ParsingSchemeUnknownScheme(crate::scheme::SchemeSerializeType), ParsingSchemeUnknownScheme(crate::scheme::SchemeSerializeType),
#[error("unable to generate random values: {0}")] #[error("unable to generate random values: {0}")]

View file

@ -59,7 +59,7 @@ pub(crate) fn encode_cipher(
pub(crate) fn decode_ikm_list(data: &str) -> Result<InputKeyMaterialList> { pub(crate) fn decode_ikm_list(data: &str) -> Result<InputKeyMaterialList> {
let data = data let data = data
.strip_prefix(crate::STORAGE_IKML_VERSION) .strip_prefix(crate::STORAGE_IKML_VERSION)
.ok_or(Error::ParsingEncodedDataInvalidVersion)?; .ok_or(Error::ParsingEncodedDataInvalidIkmlVersion)?;
let v: Vec<&str> = data.split(STORAGE_SEPARATOR).collect(); let v: Vec<&str> = data.split(STORAGE_SEPARATOR).collect();
if v.is_empty() { if v.is_empty() {
return Err(Error::ParsingEncodedDataInvalidIkmListLen(v.len())); return Err(Error::ParsingEncodedDataInvalidIkmListLen(v.len()));
@ -85,7 +85,7 @@ pub(crate) fn decode_ikm_list(data: &str) -> Result<InputKeyMaterialList> {
pub(crate) fn decode_cipher(data: &str) -> Result<(IkmId, EncryptedData, Option<u64>)> { pub(crate) fn decode_cipher(data: &str) -> Result<(IkmId, EncryptedData, Option<u64>)> {
let data = data let data = data
.strip_prefix(crate::STORAGE_ENC_VERSION) .strip_prefix(crate::STORAGE_ENC_VERSION)
.ok_or(Error::ParsingEncodedDataInvalidVersion)?; .ok_or(Error::ParsingEncodedDataInvalidEncVersion)?;
let mut v: Vec<&str> = data.split(STORAGE_SEPARATOR).collect(); let mut v: Vec<&str> = data.split(STORAGE_SEPARATOR).collect();
let time_period = if v.len() == NB_PARTS + 1 { let time_period = if v.len() == NB_PARTS + 1 {
match v.pop() { match v.pop() {