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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use inspect_format::{BlockIndex, BlockType, Error as FormatError};

/// Errors that Inspect API functions can return.
#[derive(Clone, Debug, thiserror::Error)]
pub enum Error {
    #[error("FIDL error: {0}")]
    Fidl(String),

    #[error("Failed to allocate vmo")]
    #[cfg(target_os = "fuchsia")]
    AllocateVmo(#[source] zx::Status),

    #[error("Failed to get vmo size")]
    #[cfg(target_os = "fuchsia")]
    VmoSize(#[source] zx::Status),

    #[error("Failed to free {value_type} index={index}")]
    Free {
        value_type: &'static str,
        index: BlockIndex,
        #[source]
        error: Box<Error>,
    },

    #[error("Failed to create {value_type}")]
    Create {
        value_type: &'static str,
        #[source]
        error: Box<Error>,
    },

    #[error("{0} is no-op")]
    NoOp(&'static str),

    #[error("Failed to create the internal heap")]
    CreateHeap(#[source] Box<Error>),

    #[error("Failed to create the internal state")]
    CreateState(#[source] Box<Error>),

    #[error("Attempted to free a FREE block at index {0}")]
    BlockAlreadyFree(BlockIndex),

    #[error("Invalid index {0}: {1}")]
    InvalidIndex(BlockIndex, &'static str),

    #[error("Heap already at its maximum size")]
    HeapMaxSizeReached,

    #[error("Cannot allocate block of size {0}. Exceeds maximum.")]
    BlockSizeTooBig(usize),

    #[error("Invalid block type at index {0}: {1:?}")]
    InvalidBlockType(BlockIndex, BlockType),

    #[error("Invalid block type at index {0}: {1}")]
    InvalidBlockTypeNumber(BlockIndex, u8),

    #[error("Invalid block type. Expected: {0}, actual: {1}")]
    UnexpectedBlockType(BlockType, BlockType),

    #[error("Invalid block type. Expected: {0}, got: {1}")]
    UnexpectedBlockTypeRepr(&'static str, BlockType),

    #[error("Expected lock state locked={0}")]
    ExpectedLockState(bool),

    #[error("Invalid order {0}")]
    InvalidBlockOrder(usize),

    #[error("Invalid order {0} at index {1}")]
    InvalidBlockOrderAtIndex(u8, BlockIndex),

    #[error("Cannot swap blocks of different order or container")]
    InvalidBlockSwap,

    #[error("Expected a valid entry type for the array at index {0}")]
    InvalidArrayType(BlockIndex),

    #[error("{slots} exceeds the maximum number of slots for order {order}: {max_capacity}")]
    ArrayCapacityExceeded { slots: usize, order: u8, max_capacity: usize },

    #[error("Invalid {value_type} flags={flags} at index {index}")]
    InvalidFlags { value_type: &'static str, flags: u8, index: BlockIndex },

    #[error("Name is not utf8")]
    NameNotUtf8,

    #[error("Failed to convert array slots to usize")]
    FailedToConvertArraySlotsToUsize,

    #[error("Format error")]
    VmoFormat(#[source] FormatError),

    #[error("Cannot adopt into different VMO")]
    AdoptionIntoWrongVmo,

    #[error("Cannot adopt ancestor")]
    AdoptAncestor,
}

impl From<FormatError> for Error {
    fn from(error: FormatError) -> Self {
        Self::VmoFormat(error)
    }
}

impl Error {
    pub fn fidl(err: anyhow::Error) -> Self {
        Self::Fidl(format!("{}", err))
    }

    pub fn free(value_type: &'static str, index: BlockIndex, error: Error) -> Self {
        Self::Free { value_type, index, error: Box::new(error) }
    }

    pub fn create(value_type: &'static str, error: Error) -> Self {
        Self::Create { value_type, error: Box::new(error) }
    }

    pub fn invalid_index(index: BlockIndex, reason: &'static str) -> Self {
        Self::InvalidIndex(index, reason)
    }

    pub fn invalid_flags(value_type: &'static str, flags: u8, index: BlockIndex) -> Self {
        Self::InvalidFlags { value_type, flags, index }
    }

    pub fn array_capacity_exceeded(slots: usize, order: u8, max_capacity: usize) -> Self {
        Self::ArrayCapacityExceeded { slots, order, max_capacity }
    }
}