Skip to content

Instantly share code, notes, and snippets.

@felipesere
Created December 19, 2019 21:55
Show Gist options
  • Save felipesere/57953a1e22eac555bb529f3310861083 to your computer and use it in GitHub Desktop.
Save felipesere/57953a1e22eac555bb529f3310861083 to your computer and use it in GitHub Desktop.
#[derive(Debug, AsExpression)]
#[sql_type = "Text"]
#[derive(serde::Serialize, serde::Deserialize)]
pub enum RepoEvents {
LatestCommitOnMaster(Commit),
}
impl ToSql<Text, 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)
.map(|()| serialize::IsNull::Yes)
.map_err(|e| e.into()),
}
}
}
impl FromSql<Text, Sqlite> for RepoEvents {
fn from_sql(bytes: Option<&<Sqlite as Backend>::RawValue>) -> deserialize::Result<Self> {
if let Some(data) = bytes {
return serde_json::de::from_slice(data.read_blob()).map_err(|e| e.into());
} else {
return deserialize::Result::Err(
anyhow::Error::msg("Unable to read RepoEvents from DB").into(),
);
}
}
}
pub struct NewRepoEvent {
event: RepoEvents,
}
#[derive(Insertable)]
#[table_name = "repo_activity_log"]
pub struct InsertableRepoEvent {
repo_id: i32,
event: RepoEvents,
}
#[derive(Debug, Queryable)]
pub struct StoredRepoEvent {
id: i32,
repo_id: i32,
event: RepoEvents,
created_at: NaiveDateTime,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment