nominal_api/conjure/objects/scout/compute/api/rust_udf_source.rs
1/// Rust source code defining the UDF entrypoint and any helper functions.
2#[derive(
3 Debug,
4 Clone,
5 conjure_object::serde::Serialize,
6 conjure_object::serde::Deserialize,
7 PartialEq,
8 Eq,
9 PartialOrd,
10 Ord,
11 Hash
12)]
13#[serde(crate = "conjure_object::serde")]
14#[conjure_object::private::staged_builder::staged_builder]
15#[builder(crate = conjure_object::private::staged_builder, update, inline)]
16pub struct RustUdfSource {
17 #[builder(into)]
18 #[serde(rename = "sourceCode")]
19 source_code: String,
20}
21impl RustUdfSource {
22 /// Constructs a new instance of the type.
23 #[inline]
24 pub fn new(source_code: impl Into<String>) -> Self {
25 Self::builder().source_code(source_code).build()
26 }
27 /// Rust source code to transpiled into WASM. The source may contain a single function as the entrypoint, or if several
28 /// functions appear at the top level, annotate the entrypoint with `#[main]`. Entrypoint must return a scalar value
29 /// matching the containing series type.
30 /// Parameters must be typed as `f64` for numeric inputs or `String` for categorical inputs.
31 ///
32 /// Single function example:
33 /// ```ignore
34 /// fn run(a: f64, b: f64) -> f64 {
35 /// a + b
36 /// }
37 ///
38 /// ```
39 ///
40 /// Main with helper function:
41 /// ```ignore
42 /// #[main]
43 /// fn run(sample: f64, band_size: f64) -> f64 {
44 /// excedance_count(sample, band_size)
45 /// }
46 ///
47 /// fn excedance_count(value: f64, band_size: f64) -> f64 {
48 /// let mut excedances = 0.0;
49 /// for i in 1..=5 {
50 /// if value > band_size * i as f64 {
51 /// excedances += 1.0;
52 /// }
53 /// }
54 /// excedances
55 /// }
56 /// ```
57 #[inline]
58 pub fn source_code(&self) -> &str {
59 &*self.source_code
60 }
61}