Skip to main content

fiftyone_javascript_builder/
data.rs

1/* *********************************************************************
2 * This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3 * Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House,
4 * Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
5 *
6 * This Original Work is licensed under the European Union Public Licence
7 * (EUPL) v.1.2 and is subject to its terms as set out below.
8 *
9 * If a copy of the EUPL was not distributed with this file, You can obtain
10 * one at https://opensource.org/licenses/EUPL-1.2.
11 *
12 * The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
13 * amended by the European Commission) shall be deemed incompatible for
14 * the purposes of the Work and the provisions of the compatibility
15 * clause in Article 5 of the EUPL shall not apply.
16 *
17 * If using the Work as, or as part of, a network application, by
18 * including the attribution notice(s) required under Article 5 of the EUPL
19 * in the end user terms of the application under an appropriate heading,
20 * such notice(s) shall fulfill the requirements of that article.
21 * ********************************************************************* */
22
23//! The element data produced by the JavaScript builder.
24
25use std::any::Any;
26
27use fiftyone_pipeline_core::{ElementData, NoValueError, PropertyValue, TypedKey};
28
29use crate::constants::{JAVASCRIPT_BUILDER_ELEMENT_DATA_KEY, JAVASCRIPT_PROPERTY_KEY};
30
31/// The typed key for retrieving [`JavaScriptBuilderElementData`] from a flow
32/// data.
33///
34/// Its name is the JavaScript builder's element data key, so a caller can do
35/// `flow_data.get(JAVASCRIPT_BUILDER_DATA_KEY)` to recover the strongly-typed
36/// data.
37pub const JAVASCRIPT_BUILDER_DATA_KEY: TypedKey<JavaScriptBuilderElementData> =
38    TypedKey::new(JAVASCRIPT_BUILDER_ELEMENT_DATA_KEY);
39
40/// The element data the JavaScript builder writes into the flow data.
41///
42/// It carries exactly one value, the generated JavaScript, accessible through
43/// [`JavaScriptBuilderElementData::javascript`] or by the property name
44/// [`crate::JAVASCRIPT_PROPERTY_KEY`].
45#[derive(Debug, Clone, Default)]
46pub struct JavaScriptBuilderElementData {
47    javascript: String,
48}
49
50impl JavaScriptBuilderElementData {
51    /// Create empty element data. The element fills it in during processing.
52    pub fn new() -> Self {
53        JavaScriptBuilderElementData {
54            javascript: String::new(),
55        }
56    }
57
58    /// The generated JavaScript.
59    pub fn javascript(&self) -> &str {
60        &self.javascript
61    }
62
63    /// Replace the generated JavaScript. Used by the element once it has
64    /// rendered (and optionally minified) the content.
65    pub fn set_javascript(&mut self, javascript: impl Into<String>) {
66        self.javascript = javascript.into();
67    }
68}
69
70impl ElementData for JavaScriptBuilderElementData {
71    fn get(&self, name: &str) -> Result<PropertyValue, NoValueError> {
72        // Only the `javascript` property is owned by this data. Per the
73        // coordination rules, any other name must report no value so that
74        // FlowData::get_evidence_or_property stays unambiguous.
75        if name.eq_ignore_ascii_case(JAVASCRIPT_PROPERTY_KEY) {
76            Ok(PropertyValue::String(self.javascript.clone()))
77        } else {
78            Err(NoValueError::new(format!(
79                "No value for property '{name}'."
80            )))
81        }
82    }
83
84    fn keys(&self) -> Vec<String> {
85        vec![JAVASCRIPT_PROPERTY_KEY.to_owned()]
86    }
87
88    fn as_any(&self) -> &dyn Any {
89        self
90    }
91
92    fn as_any_mut(&mut self) -> &mut dyn Any {
93        self
94    }
95}