diff --git a/src/types/mod.rs b/src/types/mod.rs index 706be0c..5951747 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -41,22 +41,24 @@ For example, to store datetimes as `i64`s counting the number of seconds since the Unix epoch: ``` -use rusqlite::types::{FromSql, FromSqlResult, ToSql, ToSqlOutput, ValueRef}; +use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef}; use rusqlite::Result; pub struct DateTimeSql(pub time::OffsetDateTime); impl FromSql for DateTimeSql { fn column_result(value: ValueRef) -> FromSqlResult { - i64::column_result(value).map(|as_i64| { - DateTimeSql(time::OffsetDateTime::from_unix_timestamp(as_i64)) + i64::column_result(value).and_then(|as_i64| { + time::OffsetDateTime::from_unix_timestamp(as_i64) + .map(|odt| DateTimeSql(odt)) + .map_err(|err| FromSqlError::Other(Box::new(err))) }) } } impl ToSql for DateTimeSql { fn to_sql(&self) -> Result { - Ok(self.0.timestamp().into()) + Ok(self.0.unix_timestamp().into()) } } ```