summaryrefslogtreecommitdiffstats
path: root/third_party/rust/webext-storage/src/ffi.rs
blob: dc4af8ed6e11325be33a1cf4be828754dea2c308 (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/// This module contains glue for the FFI, including error codes and a
/// `From<Error>` implementation for `ffi_support::ExternError`. These must be
/// implemented in this crate, and not `webext_storage_ffi`, because of Rust's
/// orphan rule for implementing traits (`webext_storage_ffi` can't implement
/// a trait in `ffi_support` for a type in `webext_storage`).
use ffi_support::{ErrorCode, ExternError};

use crate::error::{Error, ErrorKind, QuotaReason};

mod error_codes {
    /// An unexpected error occurred which likely cannot be meaningfully handled
    /// by the application.
    pub const UNEXPECTED: i32 = 1;

    /// The application passed an invalid JSON string for a storage key or value.
    pub const INVALID_JSON: i32 = 2;

    /// The total number of bytes stored in the database for this extension,
    /// counting all key-value pairs serialized to JSON, exceeds the allowed limit.
    pub const QUOTA_TOTAL_BYTES_EXCEEDED: i32 = 32;

    /// A single key-value pair exceeds the allowed byte limit when serialized
    /// to JSON.
    pub const QUOTA_ITEM_BYTES_EXCEEDED: i32 = 32 + 1;

    /// The total number of key-value pairs stored for this extension exceeded the
    /// allowed limit.
    pub const QUOTA_MAX_ITEMS_EXCEEDED: i32 = 32 + 2;
}

impl From<Error> for ExternError {
    fn from(err: Error) -> ExternError {
        let code = ErrorCode::new(match err.kind() {
            ErrorKind::JsonError(_) => error_codes::INVALID_JSON,
            ErrorKind::QuotaError(QuotaReason::TotalBytes) => {
                error_codes::QUOTA_TOTAL_BYTES_EXCEEDED
            }
            ErrorKind::QuotaError(QuotaReason::ItemBytes) => error_codes::QUOTA_ITEM_BYTES_EXCEEDED,
            ErrorKind::QuotaError(QuotaReason::MaxItems) => error_codes::QUOTA_MAX_ITEMS_EXCEEDED,
            _ => error_codes::UNEXPECTED,
        });
        ExternError::new_error(code, err.to_string())
    }
}