nimbus_fml/backends/swift/gen_structs/
imports.rs

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
5use std::fmt::Display;
6
7use super::{filters, object::object_literal};
8use crate::{
9    backends::{CodeDeclaration, CodeOracle, LiteralRenderer, TypeIdentifier},
10    intermediate_representation::{ImportedModule, Literal, TypeFinder},
11};
12use askama::Template;
13
14#[derive(Template)]
15#[template(
16    syntax = "swift",
17    escape = "none",
18    path = "ImportedModuleInitializationTemplate.swift"
19)]
20pub(crate) struct ImportedModuleInitialization<'a> {
21    pub(crate) inner: ImportedModule<'a>,
22}
23
24impl<'a> ImportedModuleInitialization<'a> {
25    pub(crate) fn new(inner: &ImportedModule<'a>) -> Self {
26        Self {
27            inner: inner.clone(),
28        }
29    }
30}
31
32impl CodeDeclaration for ImportedModuleInitialization<'_> {
33    fn imports(&self, oracle: &dyn CodeOracle) -> Option<Vec<String>> {
34        let p = self.inner.about().nimbus_module_name();
35        Some(
36            self.inner
37                .fm
38                .all_types()
39                .iter()
40                .filter_map(|t| oracle.find(t).imports(oracle))
41                .flatten()
42                .chain(vec![p])
43                .collect::<Vec<_>>(),
44        )
45    }
46
47    fn initialization_code(&self, _oracle: &dyn CodeOracle) -> Option<String> {
48        Some(self.render().unwrap())
49    }
50
51    fn definition_code(&self, _oracle: &dyn CodeOracle) -> Option<String> {
52        None
53    }
54}
55
56impl LiteralRenderer for ImportedModuleInitialization<'_> {
57    fn literal(
58        &self,
59        oracle: &dyn CodeOracle,
60        typ: &TypeIdentifier,
61        value: &Literal,
62        ctx: &dyn Display,
63    ) -> String {
64        object_literal(self.inner.fm, &self, oracle, typ, value, ctx)
65    }
66}