Skip to content

Instantly share code, notes, and snippets.

@felipesere
Last active December 19, 2019 19:45
Show Gist options
  • Save felipesere/eb67e598caab4769a86521bcd4e0ae1a to your computer and use it in GitHub Desktop.
Save felipesere/eb67e598caab4769a86521bcd4e0ae1a to your computer and use it in GitHub Desktop.
serde to to turn struct to JSON and insert into text
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,
}
@felipesere
Copy link
Author

felipesere commented Dec 19, 2019

error[E0277]: the trait bound `db::RepoEvents: diesel::Expression` is not satisfied
   --> src/db/mod.rs:109:10
    |
109 | #[derive(Insertable)]
    |          ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `db::RepoEvents`
    |
    = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Text>` for `db::RepoEvents`

error[E0277]: the trait bound `db::RepoEvents: diesel::Expression` is not satisfied
   --> src/db/mod.rs:109:10
    |
109 | #[derive(Insertable)]
    |          ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `db::RepoEvents`
    |
    = note: required because of the requirements on the impl of `diesel::Expression` for `&db::RepoEvents`
    = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Text>` for `&db::RepoEvents`

error[E0277]: the trait bound `db::RepoEvents: diesel::Expression` is not satisfied
   --> src/db/mod.rs:109:10
    |
109 | #[derive(Insertable)]
    |          ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `db::RepoEvents`
    |
    = note: required because of the requirements on the impl of `diesel::Expression` for `&'insert db::RepoEvents`
    = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Text>` for `&'insert db::RepoEvents`

error: aborting due to 3 previous errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment