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
//! Error types for JSON model parsing and code generation
use thiserror::Error;
/// Errors that can occur when working with JSON models
#[derive(Debug, Error)]
pub enum JsonModelError {
// ─────────────────────────────────────────────────────────────────────────
// Parsing Errors
// ─────────────────────────────────────────────────────────────────────────
/// Failed to parse JSON
#[error("Failed to parse JSON: {0}")]
ParseError(#[from] serde_json::Error),
/// Unsupported schema version
#[error("Unsupported schema version '{version}'. Supported versions: {supported}")]
UnsupportedSchema { version: String, supported: String },
// ─────────────────────────────────────────────────────────────────────────
// Structural Errors
// ─────────────────────────────────────────────────────────────────────────
/// Missing required field for model type
#[error("Missing required field '{field}' for {model_type} models")]
MissingField { field: String, model_type: String },
/// Invalid field for model type
#[error("Field '{field}' is not valid for {model_type} models")]
InvalidFieldForType { field: String, model_type: String },
/// Missing output definitions
#[error("Model must have an 'outputs' field")]
MissingOutput,
/// Missing parameters
#[error("Model must have 'parameters' field (unless using 'extends')")]
MissingParameters,
// ─────────────────────────────────────────────────────────────────────────
// Semantic Errors
// ─────────────────────────────────────────────────────────────────────────
/// Undefined parameter used in expression
#[error("Undefined parameter '{name}' used in {context}")]
UndefinedParameter { name: String, context: String },
/// Undefined compartment
#[error("Undefined compartment '{name}'")]
UndefinedCompartment { name: String },
/// Undefined covariate
#[error("Undefined covariate '{name}' referenced in covariate effect")]
UndefinedCovariate { name: String },
/// Parameter order mismatch for analytical function
#[error(
"Parameter order warning for '{function}': expected parameters in order {expected:?}, \
but got {actual:?}. This may cause incorrect model behavior."
)]
ParameterOrderWarning {
function: String,
expected: Vec<String>,
actual: Vec<String>,
},
/// Duplicate parameter name
#[error("Duplicate parameter name: '{name}'")]
DuplicateParameter { name: String },
/// Duplicate compartment name
#[error("Duplicate compartment name: '{name}'")]
DuplicateCompartment { name: String },
/// Duplicate covariate name
#[error("Duplicate covariate name: '{name}'")]
DuplicateCovariate { name: String },
/// Duplicate output identifier
#[error("Duplicate output identifier: '{id}'")]
DuplicateOutput { id: String },
/// Invalid neqs specification
#[error("Invalid neqs: expected [num_states, num_outputs], got {0:?}")]
InvalidNeqs(Vec<usize>),
/// Schema-specific rule violation
#[error("Field '{field}' violates schema {schema}: {message}")]
SchemaRuleViolation {
field: String,
schema: String,
message: String,
},
// ─────────────────────────────────────────────────────────────────────────
// Expression Errors
// ─────────────────────────────────────────────────────────────────────────
/// Invalid expression syntax
#[error("Invalid expression in {context}: {message}")]
InvalidExpression { context: String, message: String },
/// Empty expression
#[error("Empty expression in {context}")]
EmptyExpression { context: String },
/// Expression parse error
#[error("Failed to parse expression in {context}: {message}")]
ExpressionParseError { context: String, message: String },
// ─────────────────────────────────────────────────────────────────────────
// Library Errors
// ─────────────────────────────────────────────────────────────────────────
/// Model not found in library
#[error("Model '{0}' not found in library")]
ModelNotFound(String),
/// Circular inheritance detected
#[error("Circular inheritance detected: {0}")]
CircularInheritance(String),
/// General library error (file I/O, etc.)
#[error("Library error: {0}")]
LibraryError(String),
// ─────────────────────────────────────────────────────────────────────────
// Code Generation Errors
// ─────────────────────────────────────────────────────────────────────────
/// Code generation failed
#[error("Code generation failed: {0}")]
CodeGenError(String),
/// Compilation failed
#[error("Compilation failed: {0}")]
CompilationError(String),
}
impl JsonModelError {
/// Create a missing field error
pub fn missing_field(field: impl Into<String>, model_type: impl Into<String>) -> Self {
Self::MissingField {
field: field.into(),
model_type: model_type.into(),
}
}
/// Create an invalid field error
pub fn invalid_field(field: impl Into<String>, model_type: impl Into<String>) -> Self {
Self::InvalidFieldForType {
field: field.into(),
model_type: model_type.into(),
}
}
/// Create an undefined parameter error
pub fn undefined_param(name: impl Into<String>, context: impl Into<String>) -> Self {
Self::UndefinedParameter {
name: name.into(),
context: context.into(),
}
}
/// Create an invalid expression error
pub fn invalid_expr(context: impl Into<String>, message: impl Into<String>) -> Self {
Self::InvalidExpression {
context: context.into(),
message: message.into(),
}
}
/// Create a schema rule violation error.
pub fn schema_rule(
field: impl Into<String>,
schema: impl Into<String>,
message: impl Into<String>,
) -> Self {
Self::SchemaRuleViolation {
field: field.into(),
schema: schema.into(),
message: message.into(),
}
}
}