Backed out 5 changesets (bug 1890092, bug 1888683) for causing build bustages & crash...
[gecko.git] / third_party / rust / uniffi_bindgen / src / bindings / swift / gen_swift / primitives.rs
blob86424658a3b2b82a28ea289d3046f8fbc2aa9bf7
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/. */
5 use super::CodeType;
6 use crate::backend::Literal;
7 use crate::interface::{Radix, Type};
8 use paste::paste;
10 fn render_literal(literal: &Literal) -> String {
11     fn typed_number(type_: &Type, num_str: String) -> String {
12         match type_ {
13             // special case Int32.
14             Type::Int32 => num_str,
15             // otherwise use constructor e.g. UInt8(x)
16             Type::Int8
17             | Type::UInt8
18             | Type::Int16
19             | Type::UInt16
20             | Type::UInt32
21             | Type::Int64
22             | Type::UInt64
23             | Type::Float32
24             | Type::Float64 =>
25             // XXX we should pass in the codetype itself.
26             {
27                 format!(
28                     "{}({num_str})",
29                     super::SwiftCodeOracle.find(type_).type_label()
30                 )
31             }
32             _ => panic!("Unexpected literal: {num_str} is not a number"),
33         }
34     }
36     match literal {
37         Literal::Boolean(v) => format!("{v}"),
38         Literal::String(s) => format!("\"{s}\""),
39         Literal::Int(i, radix, type_) => typed_number(
40             type_,
41             match radix {
42                 Radix::Octal => format!("0o{i:o}"),
43                 Radix::Decimal => format!("{i}"),
44                 Radix::Hexadecimal => format!("{i:#x}"),
45             },
46         ),
47         Literal::UInt(i, radix, type_) => typed_number(
48             type_,
49             match radix {
50                 Radix::Octal => format!("0o{i:o}"),
51                 Radix::Decimal => format!("{i}"),
52                 Radix::Hexadecimal => format!("{i:#x}"),
53             },
54         ),
55         Literal::Float(string, type_) => typed_number(type_, string.clone()),
56         _ => unreachable!("Literal"),
57     }
60 macro_rules! impl_code_type_for_primitive {
61     ($T:ty, $class_name:literal) => {
62         paste! {
63             #[derive(Debug)]
64             pub struct $T;
66             impl CodeType for $T  {
67                 fn type_label(&self) -> String {
68                     $class_name.into()
69                 }
71                 fn literal(&self, literal: &Literal) -> String {
72                     render_literal(&literal)
73                 }
74             }
75         }
76     };
79 impl_code_type_for_primitive!(BooleanCodeType, "Bool");
80 impl_code_type_for_primitive!(StringCodeType, "String");
81 impl_code_type_for_primitive!(BytesCodeType, "Data");
82 impl_code_type_for_primitive!(Int8CodeType, "Int8");
83 impl_code_type_for_primitive!(Int16CodeType, "Int16");
84 impl_code_type_for_primitive!(Int32CodeType, "Int32");
85 impl_code_type_for_primitive!(Int64CodeType, "Int64");
86 impl_code_type_for_primitive!(UInt8CodeType, "UInt8");
87 impl_code_type_for_primitive!(UInt16CodeType, "UInt16");
88 impl_code_type_for_primitive!(UInt32CodeType, "UInt32");
89 impl_code_type_for_primitive!(UInt64CodeType, "UInt64");
90 impl_code_type_for_primitive!(Float32CodeType, "Float");
91 impl_code_type_for_primitive!(Float64CodeType, "Double");