Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/python/gen_python/enum_.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::{backend::Literal, bail, Result};
7
8 #[derive(Debug)]
9 pub struct EnumCodeType {
10 id: String,
11 }
12
13 impl EnumCodeType {
14 pub fn new(id: String) -> Self {
15 Self { id }
16 }
17 }
18
19 impl CodeType for EnumCodeType {
20 fn type_label(&self) -> String {
21 super::PythonCodeOracle.class_name(&self.id)
22 }
23
24 fn canonical_name(&self) -> String {
25 format!("Type{}", self.type_label())
26 }
27
28 fn literal(&self, literal: &Literal) -> Result<String> {
29 if let Literal::Enum(v, _) = literal {
30 Ok(format!(
31 "{}.{}",
32 self.type_label(),
33 super::PythonCodeOracle.enum_variant_name(v)
34 ))
35 } else {
36 bail!("Invalid literal for enum type: {literal:?}")
37 }
38 }
39 }