Skip to main content

scientific_workflow/state/
field.rs

1//! Immutable descriptions of fields declared by a state schema.
2
3use serde::Serialize;
4
5/// One validated field in a state template.
6///
7/// A field description is immutable after template loading. Its position is
8/// the corresponding payload slot in every state sharing the schema.
9#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
10pub struct StateFieldSchema {
11    #[serde(skip)]
12    index: usize,
13    name: Box<str>,
14    #[serde(skip_serializing_if = "Option::is_none")]
15    description: Option<Box<str>>,
16}
17
18impl StateFieldSchema {
19    pub(super) fn new(index: usize, name: &str, description: Option<&str>) -> Self {
20        Self {
21            index,
22            name: name.trim().into(),
23            description: description
24                .map(str::trim)
25                .filter(|description| !description.is_empty())
26                .map(Into::into),
27        }
28    }
29
30    /// Returns the zero-based payload-slot position assigned by template order.
31    pub fn position(&self) -> usize {
32        self.index
33    }
34
35    /// Returns the field name used by typed state accessors.
36    pub fn name(&self) -> &str {
37        &self.name
38    }
39
40    /// Returns the optional natural-language payload description.
41    pub fn description(&self) -> Option<&str> {
42        self.description.as_deref()
43    }
44
45    pub(super) fn owned_name(&self) -> Box<str> {
46        self.name.clone()
47    }
48}