nimbus_fml/backends/kotlin/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 = "kt",
17    escape = "none",
18    path = "ImportedModuleInitializationTemplate.kt"
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 { inner }
27    }
28}
29
30impl CodeDeclaration for ImportedModuleInitialization<'_> {
31    fn imports(&self, oracle: &dyn CodeOracle) -> Option<Vec<String>> {
32        let p = self.inner.about().nimbus_package_name()?;
33        Some(
34            self.inner
35                .fm
36                .all_types()
37                .iter()
38                .filter_map(|t| oracle.find(t).imports(oracle))
39                .flatten()
40                .chain(vec![format!("{}.*", p)])
41                .collect::<Vec<_>>(),
42        )
43    }
44
45    fn initialization_code(&self, _oracle: &dyn CodeOracle) -> Option<String> {
46        Some(self.render().unwrap())
47    }
48
49    fn definition_code(&self, _oracle: &dyn CodeOracle) -> Option<String> {
50        None
51    }
52}
53
54impl LiteralRenderer for ImportedModuleInitialization<'_> {
55    fn literal(
56        &self,
57        oracle: &dyn CodeOracle,
58        typ: &TypeIdentifier,
59        value: &Literal,
60        ctx: &dyn Display,
61    ) -> String {
62        object_literal(self.inner.fm, ctx, &self, oracle, typ, value)
63    }
64}