Skip to content

Commit cf78724

Browse files
authored
fix: return error instead of panicing (#83)
* chore: add failing test * fix: return an error instead of panicing in duckdb convertor
1 parent 86b13f0 commit cf78724

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/duckdb.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,40 @@ impl ToDuckSQL for Expr {
195195
{
196196
format!("{} {} {}", a[0], op, a[1])
197197
} else {
198-
unreachable!()
198+
return Err(Error::InvalidOperator(op.to_string()));
199199
}
200200
}
201201
}
202202
}
203203
})
204204
}
205205
}
206+
207+
#[cfg(test)]
208+
mod tests {
209+
use super::ToDuckSQL;
210+
use crate::{Error, Expr};
211+
212+
#[test]
213+
fn unreachable_code() {
214+
// https://github.com/stac-utils/rustac-py/issues/135
215+
let expr: Expr = serde_json::from_value(serde_json::json!({
216+
"op": "and",
217+
"args": [
218+
{
219+
"op": "eq",
220+
"args": [{"property": "forecast:horizon"}, "PT48H"]
221+
},
222+
{
223+
"op": "gte",
224+
"args": [{"property": "forecast:reference_time"}, "2025-05-15T00:00:00Z"]
225+
},
226+
],
227+
}))
228+
.unwrap();
229+
assert!(matches!(
230+
expr.to_ducksql().unwrap_err(),
231+
Error::InvalidOperator(_)
232+
));
233+
}
234+
}

src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ pub enum Error {
8787
#[error("Operator {0} is not implemented for this type.")]
8888
OpNotImplemented(&'static str),
8989

90+
/// Invalid operator
91+
#[error("{0} is not a valid operator.")]
92+
InvalidOperator(String),
93+
9094
/// Expression not reduced to boolean
9195
#[error("Could not reduce expression to boolean")]
9296
NonReduced(),

0 commit comments

Comments
 (0)