Last active
December 19, 2019 19:45
-
-
Save felipesere/eb67e598caab4769a86521bcd4e0ae1a to your computer and use it in GitHub Desktop.
serde to to turn struct to JSON and insert into text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
table! { | |
repo_activity_log (id) { | |
id -> Integer, | |
repo_id -> Integer, | |
event -> Text, | |
created_at -> Timestamp, | |
} | |
} | |
#[derive(Debug, PartialEq, FromSqlRow, AsExpression)] | |
enum RepoEvents { | |
LatestCommitOnMaster(Commit) | |
} | |
impl ToSql<String, Sqlite> for RepoEvents { | |
fn to_sql<W: Write>(&self, out: &mut Output<W, Sqlite>) -> serialize::Result { | |
use RepoEvents::*; | |
match self { | |
LatestCommitOnMaster(commit) => serde_json::ser::to_writer(out, &commit).into() | |
} | |
} | |
} | |
impl FromSql<String, Sqlite> for RepoEvents { | |
fn from_sql(bytes: Option<&<Sqlite as Backend>::RawValue>) -> deserialize::Result<Self> { | |
if let Some(data) = bytes { | |
serde_json::de::from_slice(data.read_blob()).map_err(|e| e.into()) | |
} else { | |
deserialize::Result::Err(bail!("Unable to read RepoEvents from DB")) | |
} | |
} | |
} | |
#[derive(Insertable)] | |
#[table_name = "repo_activity_log"] | |
pub struct InsertableRepoEvent { | |
repo_id: i32, | |
event: RepoEvents, | |
created_at: NaiveDateTime, | |
} |
Author
felipesere
commented
Dec 19, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment