Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/swift/gen_swift/compounds.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 super::CodeType;
6 use crate::{
7 backend::{Literal, Type},
8 bail, Result,
9 };
10
11 #[derive(Debug)]
12 pub struct OptionalCodeType {
13 inner: Type,
14 }
15
16 impl OptionalCodeType {
17 pub fn new(inner: Type) -> Self {
18 Self { inner }
19 }
20 }
21
22 impl CodeType for OptionalCodeType {
23 fn type_label(&self) -> String {
24 format!("{}?", super::SwiftCodeOracle.find(&self.inner).type_label())
25 }
26
27 fn canonical_name(&self) -> String {
28 format!(
29 "Option{}",
30 super::SwiftCodeOracle.find(&self.inner).canonical_name()
31 )
32 }
33
34 fn literal(&self, literal: &Literal) -> Result<String> {
35 match literal {
36 Literal::None => Ok("nil".into()),
37 Literal::Some { inner } => super::SwiftCodeOracle.find(&self.inner).literal(inner),
38 _ => bail!("Invalid literal for Optional type: {literal:?}"),
39 }
40 }
41 }
42
43 #[derive(Debug)]
44 pub struct SequenceCodeType {
45 inner: Type,
46 }
47
48 impl SequenceCodeType {
49 pub fn new(inner: Type) -> Self {
50 Self { inner }
51 }
52 }
53
54 impl CodeType for SequenceCodeType {
55 fn type_label(&self) -> String {
56 format!(
57 "[{}]",
58 super::SwiftCodeOracle.find(&self.inner).type_label()
59 )
60 }
61
62 fn canonical_name(&self) -> String {
63 format!(
64 "Sequence{}",
65 super::SwiftCodeOracle.find(&self.inner).canonical_name()
66 )
67 }
68
69 fn literal(&self, literal: &Literal) -> Result<String> {
70 match literal {
71 Literal::EmptySequence => Ok("[]".into()),
72 _ => bail!("Invalid literal for sequence type: {literal:?}"),
73 }
74 }
75 }
76
77 #[derive(Debug)]
78 pub struct MapCodeType {
79 key: Type,
80 value: Type,
81 }
82
83 impl MapCodeType {
84 pub fn new(key: Type, value: Type) -> Self {
85 Self { key, value }
86 }
87 }
88
89 impl CodeType for MapCodeType {
90 fn type_label(&self) -> String {
91 format!(
92 "[{}: {}]",
93 super::SwiftCodeOracle.find(&self.key).type_label(),
94 super::SwiftCodeOracle.find(&self.value).type_label()
95 )
96 }
97
98 fn canonical_name(&self) -> String {
99 format!(
100 "Dictionary{}{}",
101 super::SwiftCodeOracle.find(&self.key).canonical_name(),
102 super::SwiftCodeOracle.find(&self.value).canonical_name()
103 )
104 }
105
106 fn literal(&self, literal: &Literal) -> Result<String> {
107 match literal {
108 Literal::EmptyMap => Ok("[:]".into()),
109 _ => bail!("Invalid literal for map type: {literal:?}"),
110 }
111 }
112 }