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 / compounds.rs
blob8e6dddf3f91818f3439dc4fbb693b476b3c0aa25
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, Type};
8 #[derive(Debug)]
9 pub struct OptionalCodeType {
10     inner: Type,
13 impl OptionalCodeType {
14     pub fn new(inner: Type) -> Self {
15         Self { inner }
16     }
19 impl CodeType for OptionalCodeType {
20     fn type_label(&self) -> String {
21         format!("{}?", super::SwiftCodeOracle.find(&self.inner).type_label())
22     }
24     fn canonical_name(&self) -> String {
25         format!(
26             "Option{}",
27             super::SwiftCodeOracle.find(&self.inner).canonical_name()
28         )
29     }
31     fn literal(&self, literal: &Literal) -> String {
32         match literal {
33             Literal::Null => "nil".into(),
34             _ => super::SwiftCodeOracle.find(&self.inner).literal(literal),
35         }
36     }
39 #[derive(Debug)]
40 pub struct SequenceCodeType {
41     inner: Type,
44 impl SequenceCodeType {
45     pub fn new(inner: Type) -> Self {
46         Self { inner }
47     }
50 impl CodeType for SequenceCodeType {
51     fn type_label(&self) -> String {
52         format!(
53             "[{}]",
54             super::SwiftCodeOracle.find(&self.inner).type_label()
55         )
56     }
58     fn canonical_name(&self) -> String {
59         format!(
60             "Sequence{}",
61             super::SwiftCodeOracle.find(&self.inner).canonical_name()
62         )
63     }
65     fn literal(&self, literal: &Literal) -> String {
66         match literal {
67             Literal::EmptySequence => "[]".into(),
68             _ => unreachable!(),
69         }
70     }
73 #[derive(Debug)]
74 pub struct MapCodeType {
75     key: Type,
76     value: Type,
79 impl MapCodeType {
80     pub fn new(key: Type, value: Type) -> Self {
81         Self { key, value }
82     }
85 impl CodeType for MapCodeType {
86     fn type_label(&self) -> String {
87         format!(
88             "[{}: {}]",
89             super::SwiftCodeOracle.find(&self.key).type_label(),
90             super::SwiftCodeOracle.find(&self.value).type_label()
91         )
92     }
94     fn canonical_name(&self) -> String {
95         format!(
96             "Dictionary{}{}",
97             super::SwiftCodeOracle.find(&self.key).canonical_name(),
98             super::SwiftCodeOracle.find(&self.value).canonical_name()
99         )
100     }
102     fn literal(&self, literal: &Literal) -> String {
103         match literal {
104             Literal::EmptyMap => "[:]".into(),
105             _ => unreachable!(),
106         }
107     }