From 99bcc93dc2b1245fb977cb4d04b479652c13f9ac Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 16 Mar 2022 00:43:39 +0100 Subject: select(): Trying to convert a SQLite row to `yaml_rust::Yaml` Still a work in progress. Trying to figure out what makes the most sense for converting between the different types in SQLite and YAML. This code still has some compilation errors. --- src/yaml/sql.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/yaml/sql.rs') diff --git a/src/yaml/sql.rs b/src/yaml/sql.rs index 68488d2..d065718 100644 --- a/src/yaml/sql.rs +++ b/src/yaml/sql.rs @@ -34,3 +34,30 @@ impl<'a> rusqlite::ToSql for Yaml<'a> { Ok(sql_output) } } + +impl<'a> rusqlite::types::FromSql for Yaml<'a> { + fn column_result( + value: rusqlite::types::ValueRef<'_>, + ) -> rusqlite::types::FromSqlResult { + use rusqlite::types::ValueRef; + + match value { + ValueRef::Integer(i) => Ok(Yaml(&yaml_rust::Yaml::Integer(i))), + ValueRef::Real(f) => + Ok(Yaml(&yaml_rust::Yaml::Real(f.to_string()))), + ValueRef::Text(_) => { + let s = value.as_str()?; + + Ok(Yaml(&yaml_rust::Yaml::String(s.to_owned()))) + } + ValueRef::Blob(_) => { + // TODO: How should we handle blobs? Parsing as string might not + // make the most sense. + let b = value.as_str()?; + + Ok(Yaml(&yaml_rust::Yaml::String(b.to_owned()))) + } + ValueRef::Null => Ok(Yaml(&yaml_rust::Yaml::Null)), + } + } +} -- cgit v1.2.3