peace_cmd_ctx 0.0.15

Information such as which profile or flow a command is run for for the Peace framework.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use futures::stream::TryStreamExt;
use own::OwnedOrRef;
use peace_flow_model::FlowId;
use peace_flow_rt::Flow;
use peace_params::ParamsSpecs;
use peace_profile_model::Profile;
use peace_resource_rt::{
    internal::{FlowParamsFile, ProfileParamsFile, WorkspaceDirs},
    paths::{
        FlowDir, ParamsSpecsFile, PeaceAppDir, ProfileDir, ProfileHistoryDir, StatesCurrentFile,
    },
    states::{ts::CurrentStored, States, StatesCurrentStored},
};
use peace_rt_model::{
    params::{FlowParamsOpt, ProfileParams, ProfileParamsOpt},
    ParamsSpecsSerializer, StatesTypeReg,
};
use peace_rt_model_core::params::FlowParams;
use peace_state_rt::StatesSerializer;
use std::{collections::BTreeMap, marker::PhantomData};
use type_reg::untagged::TypeReg;

use crate::{CmdCtxBuilderSupport, CmdCtxTypes, ProfileFilterFn};

/// Common code used to build different `CmdCtxM*` types.
pub(crate) struct CmdCtxBuilderSupportMulti<CmdCtxTypesT>(PhantomData<CmdCtxTypesT>);

impl<CmdCtxTypesT> CmdCtxBuilderSupportMulti<CmdCtxTypesT>
where
    CmdCtxTypesT: CmdCtxTypes,
{
    /// Reads the the `Profile` and `ProfileHistory` directory paths into
    /// memory.
    pub(crate) fn profile_and_history_dirs_read(
        profiles_ref: &[Profile],
        workspace_dirs: &WorkspaceDirs,
    ) -> (
        BTreeMap<Profile, ProfileDir>,
        BTreeMap<Profile, ProfileHistoryDir>,
    )
    where
        CmdCtxTypesT: CmdCtxTypes,
    {
        let (profile_dirs, profile_history_dirs) = profiles_ref.iter().fold(
            (
                BTreeMap::<Profile, ProfileDir>::new(),
                BTreeMap::<Profile, ProfileHistoryDir>::new(),
            ),
            |(mut profile_dirs, mut profile_history_dirs), profile| {
                let profile_dir = ProfileDir::from((workspace_dirs.peace_app_dir(), profile));
                let profile_history_dir = ProfileHistoryDir::from(&profile_dir);

                profile_dirs.insert(profile.clone(), profile_dir);
                profile_history_dirs.insert(profile.clone(), profile_history_dir);

                (profile_dirs, profile_history_dirs)
            },
        );
        (profile_dirs, profile_history_dirs)
    }

    /// Reads the the `Flow`directory paths into memory.
    pub(crate) fn flow_dirs_read(
        profile_dirs: &BTreeMap<Profile, ProfileDir>,
        flow: &OwnedOrRef<'_, Flow<CmdCtxTypesT::AppError>>,
    ) -> BTreeMap<Profile, FlowDir> {
        let flow_dirs = profile_dirs.iter().fold(
            BTreeMap::<Profile, FlowDir>::new(),
            |mut flow_dirs, (profile, profile_dir)| {
                let flow_dir = FlowDir::from((profile_dir, flow.flow_id()));

                flow_dirs.insert(profile.clone(), flow_dir);

                flow_dirs
            },
        );
        flow_dirs
    }

    pub(crate) async fn profile_params_deserialize(
        profile_dirs: &BTreeMap<Profile, ProfileDir>,
        mut profile_to_profile_params_provided: BTreeMap<
            Profile,
            ProfileParamsOpt<CmdCtxTypesT::ProfileParamsKey>,
        >,
        storage: &peace_rt_model::Storage,
        profile_params_type_reg_ref: &TypeReg<CmdCtxTypesT::ProfileParamsKey>,
    ) -> Result<
        BTreeMap<Profile, ProfileParams<CmdCtxTypesT::ProfileParamsKey>>,
        CmdCtxTypesT::AppError,
    > {
        let profile_to_profile_params = futures::stream::iter(
            profile_dirs
                .iter()
                .map(Result::<_, peace_rt_model_core::Error>::Ok),
        )
        .and_then(|(profile, profile_dir)| {
            let profile_params_provided = profile_to_profile_params_provided
                .remove(profile)
                .unwrap_or_default();
            async move {
                let profile_params_file = ProfileParamsFile::from(profile_dir);

                let profile_params = CmdCtxBuilderSupport::profile_params_merge(
                    storage,
                    profile_params_type_reg_ref,
                    profile_params_provided,
                    &profile_params_file,
                )
                .await?;

                Ok((profile.clone(), profile_params))
            }
        })
        .try_collect::<BTreeMap<Profile, ProfileParams<CmdCtxTypesT::ProfileParamsKey>>>()
        .await?;
        Ok(profile_to_profile_params)
    }

    pub(crate) async fn flow_params_deserialize(
        flow_dirs: &BTreeMap<Profile, FlowDir>,
        mut profile_to_flow_params_provided: BTreeMap<
            Profile,
            FlowParamsOpt<CmdCtxTypesT::FlowParamsKey>,
        >,
        storage: &peace_rt_model::Storage,
        flow_params_type_reg_ref: &TypeReg<CmdCtxTypesT::FlowParamsKey>,
    ) -> Result<BTreeMap<Profile, FlowParams<CmdCtxTypesT::FlowParamsKey>>, CmdCtxTypesT::AppError>
    {
        let profile_to_flow_params = futures::stream::iter(
            flow_dirs
                .iter()
                .map(Result::<_, peace_rt_model_core::Error>::Ok),
        )
        .and_then(|(profile, flow_dir)| {
            let flow_params_provided = profile_to_flow_params_provided
                .remove(profile)
                .unwrap_or_default();
            async move {
                let flow_params_file = FlowParamsFile::from(flow_dir);

                let flow_params = CmdCtxBuilderSupport::flow_params_merge(
                    storage,
                    flow_params_type_reg_ref,
                    flow_params_provided,
                    &flow_params_file,
                )
                .await?;

                Ok((profile.clone(), flow_params))
            }
        })
        .try_collect::<BTreeMap<Profile, FlowParams<CmdCtxTypesT::FlowParamsKey>>>()
        .await?;
        Ok(profile_to_flow_params)
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) async fn profiles_from_peace_app_dir(
        peace_app_dir: &PeaceAppDir,
        profile_filter_fn: Option<&ProfileFilterFn>,
    ) -> Result<Vec<Profile>, peace_rt_model_core::Error> {
        use std::{ffi::OsStr, str::FromStr};

        let mut profiles = Vec::new();
        let mut peace_app_read_dir = tokio::fs::read_dir(peace_app_dir).await.map_err(
            #[cfg_attr(coverage_nightly, coverage(off))]
            |error| {
                peace_rt_model_core::Error::Native(peace_rt_model::NativeError::PeaceAppDirRead {
                    peace_app_dir: peace_app_dir.to_path_buf(),
                    error,
                })
            },
        )?;
        while let Some(entry) = peace_app_read_dir.next_entry().await.map_err(
            #[cfg_attr(coverage_nightly, coverage(off))]
            |error| {
                peace_rt_model_core::Error::Native(
                    peace_rt_model::NativeError::PeaceAppDirEntryRead {
                        peace_app_dir: peace_app_dir.to_path_buf(),
                        error,
                    },
                )
            },
        )? {
            let file_type = entry.file_type().await.map_err(
                #[cfg_attr(coverage_nightly, coverage(off))]
                |error| {
                    peace_rt_model_core::Error::Native(
                        peace_rt_model::NativeError::PeaceAppDirEntryFileTypeRead {
                            path: entry.path(),
                            error,
                        },
                    )
                },
            )?;

            if file_type.is_dir() {
                let entry_path = entry.path();
                if let Some(dir_name) = entry_path.file_name().and_then(OsStr::to_str) {
                    // Assume this is a profile directory
                    let profile =
                        peace_profile_model::Profile::from_str(dir_name).map_err(|error| {
                            peace_rt_model_core::Error::Native(
                                peace_rt_model::NativeError::ProfileDirInvalidName {
                                    dir_name: dir_name.to_string(),
                                    path: entry_path.to_path_buf(),
                                    error,
                                },
                            )
                        })?;

                    if let Some(profile_filter_fn) = profile_filter_fn {
                        if !profile_filter_fn.call(&profile) {
                            // Exclude any profiles that do not pass the filter
                            continue;
                        }
                    }

                    profiles.push(profile)
                }

                // Assume non-UTF8 file names are not profile directories
            }
        }

        // Ensure profiles are in a consistent, sensible order.
        profiles.sort();

        Ok(profiles)
    }

    #[cfg(target_arch = "wasm32")]
    pub(crate) async fn profiles_from_peace_app_dir(
        _peace_app_dir: &PeaceAppDir,
        _profile_filter_fn: Option<&ProfileFilterFn>,
    ) -> Result<Vec<Profile>, peace_rt_model_core::Error> {
        let profiles = Vec::new();

        // TODO: Not supported yet -- needs a `Storage` abstraction over both native an
        // web assembly.

        Ok(profiles)
    }

    /// Serializes profile params to storage.
    pub(crate) async fn profile_params_serialize(
        profile_to_profile_params: &BTreeMap<
            Profile,
            ProfileParams<CmdCtxTypesT::ProfileParamsKey>,
        >,
        profile_dirs: &BTreeMap<Profile, ProfileDir>,
        storage: &peace_rt_model::Storage,
    ) -> Result<(), CmdCtxTypesT::AppError> {
        futures::stream::iter(
            profile_to_profile_params
                .iter()
                .map(Result::<_, peace_rt_model_core::Error>::Ok),
        )
        .try_for_each(|(profile, profile_params)| {
            let profile_dir = profile_dirs.get(profile).unwrap_or_else(
                #[cfg_attr(coverage_nightly, coverage(off))]
                || {
                    panic!(
                        "`profile_dir` for `{profile}` should exist as it is inserted by \
                        `CmdCtxBuilderSupportMulti::profile_and_history_dirs_read`."
                    )
                },
            );
            let profile_params_file = ProfileParamsFile::from(profile_dir);
            async move {
                CmdCtxBuilderSupport::profile_params_serialize(
                    profile_params,
                    storage,
                    &profile_params_file,
                )
                .await?;
                Ok(())
            }
        })
        .await?;
        Ok(())
    }

    /// Serializes flow params to storage.
    pub(crate) async fn flow_params_serialize(
        profile_to_flow_params: &BTreeMap<Profile, FlowParams<CmdCtxTypesT::FlowParamsKey>>,
        flow_dirs: &BTreeMap<Profile, peace_resource_rt::paths::FlowDir>,
        storage: &peace_rt_model::Storage,
    ) -> Result<(), CmdCtxTypesT::AppError> {
        futures::stream::iter(
            profile_to_flow_params
                .iter()
                .map(Result::<_, peace_rt_model_core::Error>::Ok),
        )
        .try_for_each(|(profile, flow_params)| {
            let flow_dir = flow_dirs.get(profile).unwrap_or_else(
                #[cfg_attr(coverage_nightly, coverage(off))]
                || {
                    panic!(
                        "`flow_dir` for `{profile}` should exist as it is inserted by \
                        `CmdCtxBuilderSupportMulti::flow_dirs_read`."
                    )
                },
            );
            let flow_params_file = FlowParamsFile::from(flow_dir);

            async move {
                CmdCtxBuilderSupport::flow_params_serialize(
                    flow_params,
                    storage,
                    &flow_params_file,
                )
                .await?;
                Ok(())
            }
        })
        .await?;
        Ok(())
    }

    /// Reads `StateCurrent` for each profile.
    pub(crate) async fn states_current_read(
        flow_dirs: &BTreeMap<Profile, FlowDir>,
        flow_id: &FlowId,
        storage: &peace_rt_model::Storage,
        states_type_reg_ref: &StatesTypeReg,
    ) -> Result<BTreeMap<Profile, Option<States<CurrentStored>>>, CmdCtxTypesT::AppError> {
        let profile_to_states_current_stored =
            futures::stream::iter(flow_dirs.iter().map(Result::<_, peace_rt_model::Error>::Ok))
                .and_then(|(profile, flow_dir)| async move {
                    let states_current_file = StatesCurrentFile::from(flow_dir);

                    let states_current_stored =
                        StatesSerializer::<peace_rt_model::Error>::deserialize_stored_opt(
                            flow_id,
                            storage,
                            states_type_reg_ref,
                            &states_current_file,
                        )
                        .await?;

                    Ok((profile.clone(), states_current_stored))
                })
                .try_collect::<BTreeMap<Profile, Option<StatesCurrentStored>>>()
                .await?;
        Ok(profile_to_states_current_stored)
    }

    /// Deserializes previously stored params specs, merges them with the
    /// provided params specs, and stores the merged values.
    pub(crate) async fn params_specs_load_merge_and_store(
        flow_dirs: &BTreeMap<Profile, FlowDir>,
        profile_to_params_specs_provided: BTreeMap<Profile, ParamsSpecs>,
        flow: &Flow<CmdCtxTypesT::AppError>,
        storage: &peace_rt_model::Storage,
        params_specs_type_reg_ref: &peace_rt_model::ParamsSpecsTypeReg,
        app_name: &peace_cfg::AppName,
    ) -> Result<BTreeMap<Profile, ParamsSpecs>, CmdCtxTypesT::AppError> {
        let flow_id = flow.flow_id();
        let profile_to_params_specs = futures::stream::iter(
            flow_dirs
                .iter()
                .map(Result::<_, peace_rt_model_core::Error>::Ok),
        )
        .and_then(|(profile, flow_dir)| {
            let params_specs_provided = profile_to_params_specs_provided.get(profile).cloned();
            async move {
                let params_specs_file = ParamsSpecsFile::from(flow_dir);

                let params_specs_stored =
                    ParamsSpecsSerializer::<peace_rt_model_core::Error>::deserialize_opt(
                        profile,
                        flow_id,
                        storage,
                        params_specs_type_reg_ref,
                        &params_specs_file,
                    )
                    .await?;

                // For mapping fns, we still need the developer to provide the params spec
                // so that multi-profile diffs can be done.
                let profile = profile.clone();
                let params_specs = match (params_specs_stored, params_specs_provided) {
                    (None, None) => {
                        return Err(peace_rt_model_core::Error::ItemParamsSpecsFileNotFound {
                            app_name: app_name.clone(),
                            profile,
                            flow_id: flow_id.clone(),
                        });
                    }
                    (None, Some(params_specs_provided)) => params_specs_provided,
                    (Some(params_specs_stored), None) => params_specs_stored,
                    (Some(params_specs_stored), Some(params_specs_provided)) => {
                        CmdCtxBuilderSupport::params_specs_merge(
                            flow,
                            params_specs_provided,
                            Some(params_specs_stored),
                        )?
                    }
                };

                // Serialize params specs back to disk.
                CmdCtxBuilderSupport::params_specs_serialize(
                    &params_specs,
                    storage,
                    &params_specs_file,
                )
                .await?;

                Ok((profile, params_specs))
            }
        })
        .try_collect::<BTreeMap<Profile, ParamsSpecs>>()
        .await?;
        Ok(profile_to_params_specs)
    }
}