Debian Codegraph @

third_party/rust/uniffi_bindgen/src/scaffolding/mod.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 use anyhow::Result;
6 use askama::Template;
7 use std::borrow::Borrow;
8
9 use super::interface::*;
10 use heck::ToShoutySnakeCase;
11
12 #[derive(Template)]
13 #[template(syntax = "rs", escape = "none", path = "scaffolding_template.rs")]
14 pub struct RustScaffolding<'a> {
15 ci: &'a ComponentInterface,
16 udl_base_name: &'a str,
17 }
18 impl<'a> RustScaffolding<'a> {
19 pub fn new(ci: &'a ComponentInterface, udl_base_name: &'a str) -> Self {
20 Self { ci, udl_base_name }
21 }
22 }
23 mod filters {
24 use super::*;
25
26 pub fn type_rs(type_: &Type) -> Result<String, askama::Error> {
27 Ok(match type_ {
28 Type::Int8 => "i8".into(),
29 Type::UInt8 => "u8".into(),
30 Type::Int16 => "i16".into(),
31 Type::UInt16 => "u16".into(),
32 Type::Int32 => "i32".into(),
33 Type::UInt32 => "u32".into(),
34 Type::Int64 => "i64".into(),
35 Type::UInt64 => "u64".into(),
36 Type::Float32 => "f32".into(),
37 Type::Float64 => "f64".into(),
38 Type::Boolean => "bool".into(),
39 Type::String => "::std::string::String".into(),
40 Type::Bytes => "::std::vec::Vec<u8>".into(),
41 Type::Timestamp => "::std::time::SystemTime".into(),
42 Type::Duration => "::std::time::Duration".into(),
43 Type::Enum { name, .. } | Type::Record { name, .. } => format!("r#{name}"),
44 Type::Object { name, imp, .. } => {
45 format!("::std::sync::Arc<{}>", imp.rust_name_for(name))
46 }
47 Type::CallbackInterface { name, .. } => format!("Box<dyn r#{name}>"),
48 Type::Optional { inner_type } => {
49 format!("::std::option::Option<{}>", type_rs(inner_type)?)
50 }
51 Type::Sequence { inner_type } => format!("std::vec::Vec<{}>", type_rs(inner_type)?),
52 Type::Map {
53 key_type,
54 value_type,
55 } => format!(
56 "::std::collections::HashMap<{}, {}>",
57 type_rs(key_type)?,
58 type_rs(value_type)?
59 ),
60 Type::Custom { name, .. } => format!("r#{name}"),
61 })
62 }
63 }