Skip to main content

scientific_workflow/config/
error.rs

1//! Configuration-owned project loading and parameter resolution failures.
2
3use std::io;
4use std::path::PathBuf;
5
6use thiserror::Error;
7
8/// A failure while compiling project documents into a specification.
9#[derive(Debug, Error)]
10#[non_exhaustive]
11pub enum ConfigError {
12    /// A required project document could not be read.
13    #[error("failed to read project document `{path}`")]
14    Read {
15        /// Document or directory path involved in the failed operation.
16        path: PathBuf,
17        /// Underlying filesystem failure.
18        #[source]
19        source: io::Error,
20    },
21
22    /// A project document was not valid JSON.
23    #[error("failed to parse project document `{path}`")]
24    Parse {
25        /// Source document containing invalid JSON.
26        path: PathBuf,
27        /// Underlying JSON syntax failure.
28        #[source]
29        source: serde_json::Error,
30    },
31
32    /// A JSON object repeated a key.
33    #[error("project document `{path}` repeats key `{key}`")]
34    DuplicateKey {
35        /// Source document containing the duplicate.
36        path: PathBuf,
37        /// Repeated object key.
38        key: String,
39    },
40
41    /// Valid JSON did not satisfy its document grammar.
42    #[error("invalid project document `{path}` at `{pointer}`: {reason}")]
43    InvalidDocument {
44        /// Rejected source document.
45        path: PathBuf,
46        /// Nearest meaningful JSON Pointer, or `/` for the root.
47        pointer: String,
48        /// Contextual grammar violation.
49        reason: String,
50    },
51
52    /// A discovered document escaped the project configuration directory.
53    #[error("document path `{path}` is outside configuration root `{config_root}`")]
54    PathOutsideConfig {
55        /// Rejected authored or resolved path.
56        path: PathBuf,
57        /// Canonical project configuration root.
58        config_root: PathBuf,
59    },
60
61    /// A path that must cross a language-neutral JSON boundary is not UTF-8.
62    #[error("{context} path `{path}` must be valid UTF-8")]
63    NonUtf8Path {
64        /// Rejected canonical filesystem path.
65        path: PathBuf,
66        /// Stable role the path would have served.
67        context: &'static str,
68    },
69
70    /// A program or Python task did not resolve to a valid executable, script,
71    /// or environment.
72    #[error("invalid program task path `{path}`: {reason}")]
73    InvalidProgram {
74        /// Authored program path or command name.
75        path: PathBuf,
76        /// Stable resolution or validation failure.
77        reason: String,
78    },
79
80    /// A phase dependency does not name a declared phase.
81    #[error("phase `{phase}` depends on unknown phase `{dependency}`")]
82    UnknownDependency {
83        /// Referring phase.
84        phase: String,
85        /// Missing dependency.
86        dependency: String,
87    },
88
89    /// An execution-unit task selects no state schema declared by the study.
90    #[error("execution unit `{execution_unit}` in phase `{phase}` selects unknown state `{state}`")]
91    UnknownState {
92        /// Containing phase key.
93        phase: String,
94        /// Compiled execution-unit key.
95        execution_unit: String,
96        /// Missing state-schema key.
97        state: String,
98    },
99
100    /// An execution-unit parameter expansion cannot be represented safely.
101    #[error("execution-unit parameter expansion in `{path}` exceeds supported combination counts")]
102    ExpansionOverflow {
103        /// Parameters document whose selection product overflowed.
104        path: PathBuf,
105    },
106
107    /// One resolved parameter combination could not decode as execution-unit constants.
108    #[error(
109        "failed to decode resolved parameter combination {ordinal} for execution unit `{execution_unit}` from `{path}`"
110    )]
111    DecodeExecutionUnitConstants {
112        /// Compiled execution-unit key from the study manifest.
113        execution_unit: String,
114        /// Central parameters document path.
115        path: PathBuf,
116        /// Zero-based deterministic combination ordinal.
117        ordinal: u64,
118        /// Underlying Serde conversion failure.
119        #[source]
120        source: serde_json::Error,
121    },
122}
123
124impl ConfigError {
125    pub(crate) fn invalid(
126        path: &std::path::Path,
127        pointer: impl Into<String>,
128        reason: impl Into<String>,
129    ) -> Self {
130        Self::InvalidDocument {
131            path: path.to_path_buf(),
132            pointer: pointer.into(),
133            reason: reason.into(),
134        }
135    }
136}