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