Created
July 12, 2017 14:03
-
-
Save lawliet89/8af69314b11f7ccda2ef31d8b0397198 to your computer and use it in GitHub Desktop.
Lifetime confusion
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
| #[macro_use] | |
| extern crate serde_derive; | |
| extern crate csv; | |
| extern crate serde; | |
| use serde::{Serialize, Deserialize}; | |
| pub trait Record<'de>: Serialize + Deserialize<'de> { | |
| fn is_valid(&self) -> Result<(), String>; | |
| } | |
| #[derive(Serialize, Deserialize)] | |
| struct TestRecord<'a> { | |
| field_one: &'a str, | |
| field_two: &'a str, | |
| } | |
| impl<'a, 'de: 'a> Record<'de> for TestRecord<'a> { | |
| fn is_valid(&self) -> Result<(), String> { | |
| Ok(()) | |
| } | |
| } | |
| fn validate<'de, R>(record: Result<csv::ByteRecord, csv::Error>, index: usize) -> Result<csv::ByteRecord, String> | |
| where | |
| R: Record<'de>, | |
| { | |
| let record = record.map_err(|err| { | |
| format!("[decoding.row.invalid] {}, {:?}\n", index, err) | |
| })?; | |
| let deserialized_record = record.deserialize::<R>(None).map_err(|err| { | |
| format!("[deserializing.row.invalid] {}, {:?}\n", index, err) | |
| })?; | |
| match deserialized_record.is_valid() { | |
| Ok(()) => Ok(record), | |
| Err(err) => { | |
| Err(format!( | |
| "[validation.row.invalid] {}, {:?}\n", | |
| index, | |
| err | |
| )) | |
| } | |
| } | |
| } | |
| fn main() { | |
| println!("Hello, world!"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment