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
// Copyright 2020 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Errors that can happen in LibVDA.

use std::error;
use std::fmt;
use std::fmt::Display;

use super::format;
use crate::decode;
use crate::encode;

#[derive(Debug)]
pub enum Error {
    // Encode session error. The error code provided is specific
    // to the implementation type (`VeaImplType`).
    EncodeSessionFailure(i32),
    EncodeSessionInitFailure(encode::Config),
    GetCapabilitiesFailure,
    InstanceInitFailure,
    InvalidCapabilities(String),
    LibVdaFailure(decode::Response),
    ReadEventFailure(std::io::Error),
    SessionIdAlreadyUsed(u32),
    SessionInitFailure(format::Profile),
    SessionNotFound(u32),
    UnknownPixelFormat(u32),
    UnknownProfile(i32),
}

pub type Result<T> = std::result::Result<T, Error>;

impl error::Error for Error {}

impl Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use self::Error::*;

        match self {
            EncodeSessionFailure(e) => write!(f, "encode session error: {}", e),
            EncodeSessionInitFailure(c) => {
                write!(f, "failed to initialize encode session with config {:?}", c)
            }
            GetCapabilitiesFailure => write!(f, "failed to get capabilities"),
            InstanceInitFailure => write!(f, "failed to initialize VDA instance"),
            InvalidCapabilities(e) => write!(f, "obtained capabilities are invalid: {}", e),
            LibVdaFailure(e) => write!(f, "error happened in libvda: {}", e),
            ReadEventFailure(e) => write!(f, "failed to read event: {}", e),
            SessionInitFailure(p) => write!(f, "failed to initialize decode session with {:?}", p),
            SessionIdAlreadyUsed(id) => write!(f, "session_id {} is already used", id),
            SessionNotFound(id) => write!(f, "no session has session_id {}", id),
            UnknownPixelFormat(p) => write!(f, "unknown pixel format: {}", p),
            UnknownProfile(p) => write!(f, "unknown profile: {}", p),
        }
    }
}