summaryrefslogtreecommitdiffstats
path: root/third_party/rust/uniffi_bindgen/src/scaffolding/mod.rs
blob: df170f240f1c8f3e0bde22de42e3b7b0d90b13b4 (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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* 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/. */

use anyhow::Result;
use askama::Template;
use std::borrow::Borrow;

use super::interface::*;
use heck::ToSnakeCase;

#[derive(Template)]
#[template(syntax = "rs", escape = "none", path = "scaffolding_template.rs")]
pub struct RustScaffolding<'a> {
    ci: &'a ComponentInterface,
    uniffi_version: &'static str,
}
impl<'a> RustScaffolding<'a> {
    pub fn new(ci: &'a ComponentInterface) -> Self {
        Self {
            ci,
            uniffi_version: crate::BINDGEN_VERSION,
        }
    }
}
mod filters {
    use super::*;

    pub fn type_rs(type_: &Type) -> Result<String, askama::Error> {
        Ok(match type_ {
            Type::Int8 => "i8".into(),
            Type::UInt8 => "u8".into(),
            Type::Int16 => "i16".into(),
            Type::UInt16 => "u16".into(),
            Type::Int32 => "i32".into(),
            Type::UInt32 => "u32".into(),
            Type::Int64 => "i64".into(),
            Type::UInt64 => "u64".into(),
            Type::Float32 => "f32".into(),
            Type::Float64 => "f64".into(),
            Type::Boolean => "bool".into(),
            Type::String => "String".into(),
            Type::Timestamp => "std::time::SystemTime".into(),
            Type::Duration => "std::time::Duration".into(),
            Type::Enum(name) | Type::Record(name) | Type::Error(name) => format!("r#{}", name),
            Type::Object(name) => format!("std::sync::Arc<r#{}>", name),
            Type::CallbackInterface(name) => format!("Box<dyn r#{}>", name),
            Type::Optional(t) => format!("std::option::Option<{}>", type_rs(t)?),
            Type::Sequence(t) => format!("std::vec::Vec<{}>", type_rs(t)?),
            Type::Map(k, v) => format!(
                "std::collections::HashMap<{}, {}>",
                type_rs(k)?,
                type_rs(v)?
            ),
            Type::Custom { name, .. } => format!("r#{}", name),
            Type::External { .. } => panic!("External types coming to a uniffi near you soon!"),
            Type::Unresolved { .. } => {
                unreachable!("UDL scaffolding code never contains unresolved types")
            }
        })
    }

    pub fn type_ffi(type_: &FFIType) -> Result<String, askama::Error> {
        Ok(match type_ {
            FFIType::Int8 => "i8".into(),
            FFIType::UInt8 => "u8".into(),
            FFIType::Int16 => "i16".into(),
            FFIType::UInt16 => "u16".into(),
            FFIType::Int32 => "i32".into(),
            FFIType::UInt32 => "u32".into(),
            FFIType::Int64 => "i64".into(),
            FFIType::UInt64 => "u64".into(),
            FFIType::Float32 => "f32".into(),
            FFIType::Float64 => "f64".into(),
            FFIType::RustArcPtr(_) => "*const std::os::raw::c_void".into(),
            FFIType::RustBuffer => "uniffi::RustBuffer".into(),
            FFIType::ForeignBytes => "uniffi::ForeignBytes".into(),
            FFIType::ForeignCallback => "uniffi::ForeignCallback".into(),
        })
    }

    /// Get the name of the FfiConverter implementation for this type
    ///
    /// - For primitives / standard types this is the type itself.
    /// - For user-defined types, this is a unique generated name.  We then generate a unit-struct
    ///   in the scaffolding code that implements FfiConverter.
    pub fn ffi_converter_name(type_: &Type) -> askama::Result<String> {
        Ok(match type_ {
            // Timestamp/Duraration are handled by standard types
            Type::Timestamp => "std::time::SystemTime".into(),
            Type::Duration => "std::time::Duration".into(),
            // Object is handled by Arc<T>
            Type::Object(name) => format!("std::sync::Arc<r#{}>", name),
            // Other user-defined types are handled by a unit-struct that we generate.  The
            // FfiConverter implementation for this can be found in one of the scaffolding template code.
            //
            // We generate a unit-struct to sidestep Rust's orphan rules (ADR-0006).
            //
            // CallbackInterface is handled by special case code on both the scaffolding and
            // bindings side.  It's not a unit-struct, but the same name generation code works.
            Type::Enum(_) | Type::Record(_) | Type::Error(_) | Type::CallbackInterface(_) => {
                format!("FfiConverter{}", type_.canonical_name())
            }
            // Wrapper types are implemented by generics that wrap the FfiConverter implementation of the
            // inner type.
            Type::Optional(inner) => {
                format!("std::option::Option<{}>", ffi_converter_name(inner)?)
            }
            Type::Sequence(inner) => format!("std::vec::Vec<{}>", ffi_converter_name(inner)?),
            Type::Map(k, v) => format!(
                "std::collections::HashMap<{}, {}>",
                ffi_converter_name(k)?,
                ffi_converter_name(v)?
            ),
            // External and Wrapped bytes have FfiConverters with a predictable name based on the type name.
            Type::Custom { name, .. } | Type::External { name, .. } => {
                format!("FfiConverterType{}", name)
            }
            // Primitive types / strings are implemented by their rust type
            Type::Int8 => "i8".into(),
            Type::UInt8 => "u8".into(),
            Type::Int16 => "i16".into(),
            Type::UInt16 => "u16".into(),
            Type::Int32 => "i32".into(),
            Type::UInt32 => "u32".into(),
            Type::Int64 => "i64".into(),
            Type::UInt64 => "u64".into(),
            Type::Float32 => "f32".into(),
            Type::Float64 => "f64".into(),
            Type::String => "String".into(),
            Type::Boolean => "bool".into(),
            Type::Unresolved { .. } => {
                unreachable!("UDL scaffolding code never contains unresolved types")
            }
        })
    }

    // Map a type to Rust code that specifies the FfiConverter implementation.
    //
    // This outputs something like `<TheFfiConverterStruct as FfiConverter>`
    pub fn ffi_converter(type_: &Type) -> Result<String, askama::Error> {
        Ok(format!(
            "<{} as uniffi::FfiConverter>",
            ffi_converter_name(type_)?
        ))
    }

    // Turns a `crate-name` into the `crate_name` the .rs code needs to specify.
    pub fn crate_name_rs(nm: &str) -> Result<String, askama::Error> {
        Ok(nm.to_string().to_snake_case())
    }
}