nimbus_fml/backends/swift/gen_structs/
imports.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::fmt::Display;

use super::{filters, object::object_literal};
use crate::{
    backends::{CodeDeclaration, CodeOracle, LiteralRenderer, TypeIdentifier},
    intermediate_representation::{ImportedModule, Literal, TypeFinder},
};
use askama::Template;

#[derive(Template)]
#[template(
    syntax = "swift",
    escape = "none",
    path = "ImportedModuleInitializationTemplate.swift"
)]
pub(crate) struct ImportedModuleInitialization<'a> {
    pub(crate) inner: ImportedModule<'a>,
}

impl<'a> ImportedModuleInitialization<'a> {
    pub(crate) fn new(inner: &ImportedModule<'a>) -> Self {
        Self {
            inner: inner.clone(),
        }
    }
}

impl CodeDeclaration for ImportedModuleInitialization<'_> {
    fn imports(&self, oracle: &dyn CodeOracle) -> Option<Vec<String>> {
        let p = self.inner.about().nimbus_module_name();
        Some(
            self.inner
                .fm
                .all_types()
                .iter()
                .filter_map(|t| oracle.find(t).imports(oracle))
                .flatten()
                .chain(vec![p])
                .collect::<Vec<_>>(),
        )
    }

    fn initialization_code(&self, _oracle: &dyn CodeOracle) -> Option<String> {
        Some(self.render().unwrap())
    }

    fn definition_code(&self, _oracle: &dyn CodeOracle) -> Option<String> {
        None
    }
}

impl LiteralRenderer for ImportedModuleInitialization<'_> {
    fn literal(
        &self,
        oracle: &dyn CodeOracle,
        typ: &TypeIdentifier,
        value: &Literal,
        ctx: &dyn Display,
    ) -> String {
        object_literal(self.inner.fm, &self, oracle, typ, value, ctx)
    }
}