summaryrefslogtreecommitdiffstats
path: root/src/db/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/db/mod.rs')
-rw-r--r--src/db/mod.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/db/mod.rs b/src/db/mod.rs
index 2cf766b..403ced2 100644
--- a/src/db/mod.rs
+++ b/src/db/mod.rs
@@ -17,6 +17,25 @@ pub mod queries;
pub use migrations::MIGRATIONS;
pub use model::*;
+
+/// Parse a state column into one of the state enums.
+///
+/// A value the code does not recognise fails the row instead of becoming a
+/// default: a build in a state we cannot name must not be listed, claimed, or
+/// rendered as if it were in one we could.
+pub(crate) fn state_col<T: StateEnum>(col: usize, text: &str) -> rusqlite::Result<T> {
+ T::parse(text).ok_or_else(|| {
+ rusqlite::Error::FromSqlConversionFailure(
+ col,
+ rusqlite::types::Type::Text,
+ Box::new(std::io::Error::new(
+ std::io::ErrorKind::InvalidData,
+ format!("{} has a state outside the state machine: {text:?}", T::KIND),
+ )),
+ )
+ })
+}
+
pub use queries::*;
use std::path::{Path, PathBuf};