Skip to main content

jellyflow_runtime/runtime/conformance/fixtures/
suite_file.rs

1use std::path::{Path, PathBuf};
2
3use serde::{Deserialize, Serialize};
4
5use super::super::reports::ConformanceSuiteReport;
6use super::super::scenario::ConformanceSuite;
7use super::error::ConformanceFixtureFileError;
8
9impl ConformanceSuite {
10    pub fn load_json(path: impl AsRef<Path>) -> Result<Self, ConformanceFixtureFileError> {
11        let path = path.as_ref();
12        let bytes = std::fs::read(path).map_err(|source| ConformanceFixtureFileError::Read {
13            path: path.display().to_string(),
14            source,
15        })?;
16        serde_json::from_slice(&bytes).map_err(|source| ConformanceFixtureFileError::Parse {
17            path: path.display().to_string(),
18            source,
19        })
20    }
21
22    pub fn load_json_if_exists(
23        path: impl AsRef<Path>,
24    ) -> Result<Option<Self>, ConformanceFixtureFileError> {
25        let path = path.as_ref();
26        if !path.exists() {
27            return Ok(None);
28        }
29        Self::load_json(path).map(Some)
30    }
31
32    pub fn save_json(&self, path: impl AsRef<Path>) -> Result<(), ConformanceFixtureFileError> {
33        let path = path.as_ref();
34        if let Some(parent) = path.parent() {
35            std::fs::create_dir_all(parent).map_err(|source| {
36                ConformanceFixtureFileError::Write {
37                    path: path.display().to_string(),
38                    source,
39                }
40            })?;
41        }
42        let bytes = serde_json::to_vec_pretty(self).map_err(|source| {
43            ConformanceFixtureFileError::Serialize {
44                path: path.display().to_string(),
45                source,
46            }
47        })?;
48        std::fs::write(path, bytes).map_err(|source| ConformanceFixtureFileError::Write {
49            path: path.display().to_string(),
50            source,
51        })
52    }
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize)]
56pub struct ConformanceSuiteFile {
57    pub path: PathBuf,
58    pub suite: ConformanceSuite,
59}
60
61impl ConformanceSuiteFile {
62    pub fn load_json(path: impl AsRef<Path>) -> Result<Self, ConformanceFixtureFileError> {
63        let path = path.as_ref();
64        Ok(Self {
65            path: path.to_path_buf(),
66            suite: ConformanceSuite::load_json(path)?,
67        })
68    }
69
70    pub fn run(&self) -> ConformanceSuiteFileReport {
71        ConformanceSuiteFileReport {
72            path: self.path.clone(),
73            report: self.suite.run(),
74        }
75    }
76}
77
78#[derive(Debug, Clone, Serialize, Deserialize)]
79pub struct ConformanceSuiteFileReport {
80    pub path: PathBuf,
81    pub report: ConformanceSuiteReport,
82}