| 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::{AsCodeType, CodeType}; |
| 6 |
use crate::{ |
| 7 |
backend::{Literal, Type}, |
| 8 |
bail, ComponentInterface, 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 |
fn inner(&self) -> &Type { |
| 21 |
&self.inner |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
impl CodeType for OptionalCodeType { |
| 26 |
fn type_label(&self, ci: &ComponentInterface) -> String { |
| 27 |
format!( |
| 28 |
"{}?", |
| 29 |
super::KotlinCodeOracle.find(self.inner()).type_label(ci) |
| 30 |
) |
| 31 |
} |
| 32 |
|
| 33 |
fn canonical_name(&self) -> String { |
| 34 |
format!( |
| 35 |
"Optional{}", |
| 36 |
super::KotlinCodeOracle.find(self.inner()).canonical_name() |
| 37 |
) |
| 38 |
} |
| 39 |
|
| 40 |
fn literal(&self, literal: &Literal, ci: &ComponentInterface) -> Result<String> { |
| 41 |
match literal { |
| 42 |
Literal::None => Ok("null".into()), |
| 43 |
Literal::Some { inner } => super::KotlinCodeOracle.find(&self.inner).literal(inner, ci), |
| 44 |
_ => bail!("Invalid literal for Optional type: {literal:?}"), |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
#[derive(Debug)] |
| 50 |
pub struct SequenceCodeType { |
| 51 |
inner: Type, |
| 52 |
} |
| 53 |
|
| 54 |
impl SequenceCodeType { |
| 55 |
pub fn new(inner: Type) -> Self { |
| 56 |
Self { inner } |
| 57 |
} |
| 58 |
fn inner(&self) -> &Type { |
| 59 |
&self.inner |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
impl CodeType for SequenceCodeType { |
| 64 |
fn type_label(&self, ci: &ComponentInterface) -> String { |
| 65 |
format!( |
| 66 |
"List<{}>", |
| 67 |
super::KotlinCodeOracle.find(self.inner()).type_label(ci) |
| 68 |
) |
| 69 |
} |
| 70 |
|
| 71 |
fn canonical_name(&self) -> String { |
| 72 |
format!( |
| 73 |
"Sequence{}", |
| 74 |
super::KotlinCodeOracle.find(self.inner()).canonical_name() |
| 75 |
) |
| 76 |
} |
| 77 |
|
| 78 |
fn literal(&self, literal: &Literal, _ci: &ComponentInterface) -> Result<String> { |
| 79 |
match literal { |
| 80 |
Literal::EmptySequence => Ok("listOf()".into()), |
| 81 |
_ => bail!("Invalid literal for List type: {literal:?}"), |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
#[derive(Debug)] |
| 87 |
pub struct MapCodeType { |
| 88 |
key: Type, |
| 89 |
value: Type, |
| 90 |
} |
| 91 |
|
| 92 |
impl MapCodeType { |
| 93 |
pub fn new(key: Type, value: Type) -> Self { |
| 94 |
Self { key, value } |
| 95 |
} |
| 96 |
|
| 97 |
fn key(&self) -> &Type { |
| 98 |
&self.key |
| 99 |
} |
| 100 |
|
| 101 |
fn value(&self) -> &Type { |
| 102 |
&self.value |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
impl CodeType for MapCodeType { |
| 107 |
fn type_label(&self, ci: &ComponentInterface) -> String { |
| 108 |
format!( |
| 109 |
"Map<{}, {}>", |
| 110 |
super::KotlinCodeOracle.find(self.key()).type_label(ci), |
| 111 |
super::KotlinCodeOracle.find(self.value()).type_label(ci), |
| 112 |
) |
| 113 |
} |
| 114 |
|
| 115 |
fn canonical_name(&self) -> String { |
| 116 |
format!( |
| 117 |
"Map{}{}", |
| 118 |
self.key().as_codetype().canonical_name(), |
| 119 |
self.value().as_codetype().canonical_name(), |
| 120 |
) |
| 121 |
} |
| 122 |
|
| 123 |
fn literal(&self, literal: &Literal, _ci: &ComponentInterface) -> Result<String> { |
| 124 |
match literal { |
| 125 |
Literal::EmptyMap => Ok("mapOf()".into()), |
| 126 |
_ => bail!("Invalid literal for Map type: {literal:?}"), |
| 127 |
} |
| 128 |
} |
| 129 |
} |