matrix_sdk_qrcode/error.rs
1// Copyright 2021 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use thiserror::Error;
16
17/// Error type describing errors that happen while QR data is being decoded.
18#[derive(Error, Debug)]
19pub enum DecodingError {
20 /// The QR code data is missing the mandatory Matrix header.
21 #[error("the decoded QR code is missing the Matrix header")]
22 Header,
23 /// The QR code data is containing an invalid, non UTF-8, flow id.
24 #[error(transparent)]
25 Utf8(#[from] std::string::FromUtf8Error),
26 /// The QR code data is using an unsupported or invalid verification mode.
27 #[error("the QR code contains an invalid verification mode: {0}")]
28 Mode(u8),
29 #[error(transparent)]
30 /// The QR code data does not contain all the necessary fields.
31 Read(#[from] std::io::Error),
32 /// The QR code data uses an invalid shared secret.
33 #[error("the QR code contains a too short shared secret, length: {0}")]
34 SharedSecret(usize),
35 /// The QR code data uses an invalid or unsupported version.
36 #[error("the QR code contains an invalid or unsupported version: {0}")]
37 Version(u8),
38 /// The QR code data doesn't contain valid ed25519 keys.
39 #[error("the QR code contains invalid ed25519 keys: {0}")]
40 Keys(#[from] vodozemac::KeyError),
41}
42
43/// Error type describing errors that happen while QR data is being encoded.
44#[derive(Error, Debug)]
45pub enum EncodingError {
46 /// Error generating a QR code from the data, likely because the data
47 /// doesn't fit into a QR code.
48 #[error(transparent)]
49 Qr(#[from] qrcode::types::QrError),
50 /// Error encoding the given flow id, the flow id is too large.
51 #[error("The verification flow id length can't be converted into a u16: {0}")]
52 FlowId(#[from] std::num::TryFromIntError),
53}