summaryrefslogtreecommitdiffstats
path: root/third_party/rust/uniffi_bindgen/src/backend/filters.rs
blob: 0d2da8cab2ea038951743314c9af54e1a2eba24e (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
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
/* 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/. */

//! Backend-agnostic askama filters

use crate::interface::{
    AsType, CallbackInterface, ComponentInterface, Enum, FfiType, Function, Object, Record,
};
use askama::Result;
use std::fmt;

// Need to define an error that implements std::error::Error, which neither String nor
// anyhow::Error do.
#[derive(Debug)]
struct UniFFIError {
    message: String,
}

impl UniFFIError {
    fn new(message: String) -> Self {
        Self { message }
    }
}

impl fmt::Display for UniFFIError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.message)
    }
}

impl std::error::Error for UniFFIError {}

macro_rules! lookup_error {
    ($($args:tt)*) => {
        askama::Error::Custom(Box::new(UniFFIError::new(format!($($args)*))))
    }
}

/// Get an Enum definition by name
pub fn get_enum_definition<'a>(ci: &'a ComponentInterface, name: &str) -> Result<&'a Enum> {
    ci.get_enum_definition(name)
        .ok_or_else(|| lookup_error!("enum {name} not found"))
}

/// Get a Record definition by name
pub fn get_record_definition<'a>(ci: &'a ComponentInterface, name: &str) -> Result<&'a Record> {
    ci.get_record_definition(name)
        .ok_or_else(|| lookup_error!("record {name} not found"))
}

/// Get a Function definition by name
pub fn get_function_definition<'a>(ci: &'a ComponentInterface, name: &str) -> Result<&'a Function> {
    ci.get_function_definition(name)
        .ok_or_else(|| lookup_error!("function {name} not found"))
}

/// Get an Object definition by name
pub fn get_object_definition<'a>(ci: &'a ComponentInterface, name: &str) -> Result<&'a Object> {
    ci.get_object_definition(name)
        .ok_or_else(|| lookup_error!("object {name} not found"))
}

/// Get an Callback Interface definition by name
pub fn get_callback_interface_definition<'a>(
    ci: &'a ComponentInterface,
    name: &str,
) -> Result<&'a CallbackInterface> {
    ci.get_callback_interface_definition(name)
        .ok_or_else(|| lookup_error!("callback interface {name} not found"))
}

/// Get the FfiType for a Type
pub fn ffi_type(type_: &impl AsType) -> Result<FfiType, askama::Error> {
    Ok(type_.as_type().into())
}