nimbus_fml/backends/swift/gen_structs/
feature.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 askama::Template;
8
9use super::filters;
10use super::object::object_literal;
11use crate::{
12    backends::{CodeDeclaration, CodeOracle, LiteralRenderer, TypeIdentifier},
13    intermediate_representation::{FeatureDef, FeatureManifest, Literal},
14};
15
16#[derive(Template)]
17#[template(syntax = "swift", escape = "none", path = "FeatureTemplate.swift")]
18pub(crate) struct FeatureCodeDeclaration {
19    inner: FeatureDef,
20    fm: FeatureManifest,
21}
22
23impl FeatureCodeDeclaration {
24    pub fn new(fm: &FeatureManifest, inner: &FeatureDef) -> Self {
25        Self {
26            inner: inner.clone(),
27            fm: fm.clone(),
28        }
29    }
30    pub fn inner(&self) -> &FeatureDef {
31        &self.inner
32    }
33}
34
35impl CodeDeclaration for FeatureCodeDeclaration {
36    fn definition_code(&self, _oracle: &dyn CodeOracle) -> Option<String> {
37        Some(self.render().unwrap())
38    }
39}
40
41impl LiteralRenderer for FeatureCodeDeclaration {
42    fn literal(
43        &self,
44        oracle: &dyn CodeOracle,
45        typ: &TypeIdentifier,
46        value: &Literal,
47        ctx: &dyn Display,
48    ) -> String {
49        object_literal(&self.fm, &self, oracle, typ, value, ctx)
50    }
51}