Skip to content

Instantly share code, notes, and snippets.

@thejefflarson
Created October 25, 2016 18:01
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 thejefflarson/39ca01cdb8b48211abf6eacff595373f to your computer and use it in GitHub Desktop.
Save thejefflarson/39ca01cdb8b48211abf6eacff595373f to your computer and use it in GitHub Desktop.
pub trait HasAcs {
fn valid(&self) -> bool;
fn from_row(row: &HashMap<&str, &str>) -> Self;
fn file() -> &'static str;
}
fn process_acs<T: HasAcs + diesel::query_builder::AsChangeset>() {
use schema::locations::dsl::*;
let conn = establish_connection();
let mut reader = Reader::from_file(T::file())
.unwrap()
.has_headers(true);
let headers = reader.headers().unwrap();
for row in reader.records() {
// ... extra processing elided like populating map, zip and loc_id variables or whatever
let it = T::from_row(&map);
if it.valid() {
let _ = diesel::update(locations.find(loc_id))
.set(&it) // <- here
.returning(id)
.get_result::<i32>(&conn)
.expect(&format!("Couldn't update zip {}", &zip));
} else {
println!("Error on {:?}", &zip);
}
}
}
Compiling import v0.1.0 (file:///Users/jeff/dev/car-insurance/database/import)
error[E0277]: the trait bound `&T: diesel::query_builder::AsChangeset` is not satisfied
--> src/lib.rs:380:18
|
380 | .set(&it)
| ^^^ trait `&T: diesel::query_builder::AsChangeset` not satisfied
|
= help: consider adding a `where &T: diesel::query_builder::AsChangeset` bound
error[E0277]: the trait bound `models::Demographics: diesel::query_builder::AsChangeset` is not satisfied
--> src/lib.rs:391:5
|
391 | process_acs::<Demographics>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait `models::Demographics: diesel::query_builder::AsChangeset` not satisfied
|
= help: the following implementations were found:
= help: <&'update models::Demographics as diesel::query_builder::AsChangeset>
= note: required by `process_acs`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment