scientific_workflow/observation/
plan.rs1use std::collections::HashSet;
4
5use crate::state::advanced::SystemStateSchema;
6
7use super::error::ObservationError;
8use super::stream::{BoundObservationStream, ObservationStream};
9
10#[derive(Clone, Debug, Eq, PartialEq)]
12pub struct ObservationPlan {
13 streams: Box<[ObservationStream]>,
14 iteration_unit: Option<Box<str>>,
15 physical_time_unit: Option<Box<str>>,
16}
17
18impl ObservationPlan {
19 pub fn all_fields() -> Self {
21 Self {
22 streams: vec![
23 ObservationStream::all_fields("state").expect("the inferred name is valid"),
24 ]
25 .into_boxed_slice(),
26 iteration_unit: None,
27 physical_time_unit: None,
28 }
29 }
30
31 pub fn fields<I, S>(fields: I) -> Result<Self, ObservationError>
33 where
34 I: IntoIterator<Item = S>,
35 S: Into<String>,
36 {
37 Self::streams([ObservationStream::fields("state", fields)?])
38 }
39
40 pub fn streams<I>(streams: I) -> Result<Self, ObservationError>
42 where
43 I: IntoIterator<Item = ObservationStream>,
44 {
45 let streams = streams.into_iter().collect::<Vec<_>>();
46 if streams.is_empty() {
47 return Err(ObservationError::EmptyPlan);
48 }
49 let mut names = HashSet::with_capacity(streams.len());
50 for stream in &streams {
51 if !names.insert(stream.name().to_owned()) {
52 return Err(ObservationError::DuplicateStreamName {
53 stream: stream.name().to_owned(),
54 });
55 }
56 }
57 Ok(Self {
58 streams: streams.into_boxed_slice(),
59 iteration_unit: None,
60 physical_time_unit: None,
61 })
62 }
63
64 pub fn with_iteration_unit(
66 mut self,
67 unit: impl Into<String>,
68 ) -> Result<Self, ObservationError> {
69 self.iteration_unit = Some(normalize_unit("iteration", unit)?);
70 Ok(self)
71 }
72
73 pub fn with_physical_time_unit(
75 mut self,
76 unit: impl Into<String>,
77 ) -> Result<Self, ObservationError> {
78 self.physical_time_unit = Some(normalize_unit("physical_time", unit)?);
79 Ok(self)
80 }
81}
82
83fn normalize_unit(
84 axis: &'static str,
85 unit: impl Into<String>,
86) -> Result<Box<str>, ObservationError> {
87 let unit = unit.into();
88 let unit = unit.trim();
89 if unit.is_empty() {
90 return Err(ObservationError::EmptyAxisUnit { axis });
91 }
92 Ok(unit.into())
93}
94
95#[derive(Clone, Debug)]
97pub(crate) struct BoundObservationPlan {
98 schema: SystemStateSchema,
99 streams: Box<[BoundObservationStream]>,
100 iteration_unit: Option<Box<str>>,
101 physical_time_unit: Option<Box<str>>,
102}
103
104impl BoundObservationPlan {
105 pub(crate) fn bind(
107 plan: ObservationPlan,
108 schema: &SystemStateSchema,
109 ) -> Result<Self, ObservationError> {
110 let streams = plan
111 .streams
112 .into_vec()
113 .into_iter()
114 .map(|stream| stream.bind(schema))
115 .collect::<Result<Vec<_>, _>>()?;
116 Ok(Self {
117 schema: schema.clone(),
118 streams: streams.into_boxed_slice(),
119 iteration_unit: plan.iteration_unit,
120 physical_time_unit: plan.physical_time_unit,
121 })
122 }
123
124 pub(crate) fn schema(&self) -> &SystemStateSchema {
126 &self.schema
127 }
128
129 pub(crate) fn streams(&self) -> &[BoundObservationStream] {
131 &self.streams
132 }
133
134 pub(crate) fn iteration_unit(&self) -> Option<&str> {
136 self.iteration_unit.as_deref()
137 }
138
139 pub(crate) fn physical_time_unit(&self) -> Option<&str> {
141 self.physical_time_unit.as_deref()
142 }
143}