Skip to content

Instantly share code, notes, and snippets.

@thomcc
Created February 10, 2020 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomcc/eab6736729ee5ee21aee5b96229ae488 to your computer and use it in GitHub Desktop.
Save thomcc/eab6736729ee5ee21aee5b96229ae488 to your computer and use it in GitHub Desktop.
use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ValueRef};
#[derive(Debug, Clone, PartialEq)]
pub struct LossyString(pub String);
impl FromSql for LossyString {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
if let ValueRef::Text(bytes) = value {
let fixed = String::from_utf8_lossy(bytes).to_string();
Ok(Self(fixed))
} else {
Err(FromSqlError::InvalidType)
}
}
}
// then use `row.get::<_, LossyString>()` or `row.get::<_, Option<LossyString>>()`...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment