Skip to content

Instantly share code, notes, and snippets.

@adamscott
Forked from anonymous/playground.rs
Last active March 10, 2017 19:21
Show Gist options
  • Save adamscott/199593baed3c660196ceee9cbd3f0885 to your computer and use it in GitHub Desktop.
Save adamscott/199593baed3c660196ceee9cbd3f0885 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
extern crate serde;
#[macro_use]
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
#[derive(Debug, Serialize, Deserialize)]
struct SearchResponse {
#[serde(rename = "resultCount")]
result_count: u32,
results: Vec<Result>
}
#[derive(Debug, Serialize, Deserialize)]
struct Result {
#[serde(skip_serializing_if="Option::is_none")]
legislation: Option<LegislationResult>,
#[serde(skip_serializing_if="Option::is_none")]
case: Option<CaseResult>
}
#[derive(Debug, Serialize, Deserialize)]
struct LegislationResult {
#[serde(rename = "databaseId")]
database_id: String,
#[serde(rename = "legislationId")]
legislation_id: String,
title: String,
citation: String,
#[serde(rename = "type")]
legislation_type: String
}
#[derive(Debug, Serialize, Deserialize)]
struct CaseResult {
#[serde(rename = "databaseId")]
database_id: String,
#[serde(rename = "caseId")]
case_id: CaseId,
title: String,
citation: String
}
#[derive(Debug, Serialize, Deserialize)]
struct CaseId {
#[serde(skip_serializing_if="Option::is_none")]
fr: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
en: Option<String>
}
fn main() {
let data = r#"
{"resultCount": 1681318, "results": [{"case": {"citation": "2014 QCCS 985 (CanLII)", "databaseId": "qccs", "title": "Dupuis c. Québec (Procureur général)", "caseId": {"fr": "2014qccs985"}}}, {"case": {"citation": "2004 CanLII 49463 (QC CS)", "databaseId": "qccs", "title": "C. O. c. Ch. L.", "caseId": {"fr": "2004canlii49463"}}}, {"case": {"citation": "2013 QCCLP 5554 (CanLII)", "databaseId": "qcclp", "title": "Laliberté et Micmacs of Gesgapegiag", "caseId": {"fr": "2013qcclp5554"}}}, {"case": {"citation": "2011 QCCSJ 613 (CanLII)", "databaseId": "qccsj", "title": "Anonyme — 11613", "caseId": {"fr": "2011qccsj613"}}}, {"case": {"citation": "2006 CanLII 73436 (QC TAQ)", "databaseId": "qctaq", "title": "M.R. c. Québec (Procureur général)", "caseId": {"fr": "2006canlii73436"}}}, {"case": {"citation": "2006 QCCRT 333 (CanLII)", "databaseId": "qccrt", "title": "Martel c. Bois d'Énergie inc.", "caseId": {"fr": "2006qccrt333"}}}, {"case": {"citation": "2015 CanLII 77281 (QC SAT)", "databaseId": "qcdag", "title": "Syndicat des cols bleus regroupés de Montréal, section locale 301 c Montréal (ville)", "caseId": {"fr": "2015canlii77281"}}}, {"case": {"citation": "2014 QCCS 2218 (CanLII)", "databaseId": "qccs", "title": "Grenier c. Régie des marchés agricoles et alimentaires du Québec", "caseId": {"fr": "2014qccs2218"}}}, {"case": {"citation": "2006 QCCQ 4301 (CanLII)", "databaseId": "qccq", "title": "Mathieu, Carrier, s.e.n.c. c. Tardif", "caseId": {"fr": "2006qccq4301"}}}, {"case": {"citation": "2016 QCCSJ 975 (CanLII)", "databaseId": "qccsj", "title": "Anonyme — 16978", "caseId": {"fr": "2016qccsj975"}}}]}
"#;
let sr: SearchResponse = serde_json::from_str(data).unwrap();
println!("SearchResponse: {}", sr.result_count);
let data_case = r#"
{
"databaseId": "qccsj",
"caseId": {
"fr": "2016qccsj975"
},
"title": "Anonyme — 16978",
"citation": "2016 QCCSJ 975 (CanLII)"
}
"#;
let case_result: CaseResult = serde_json::from_str(data_case).unwrap();
println!("CaseResult: {}", case_result.database_id);
let data_legislation = r#"
{
"databaseId": "qca",
"legislationId": "lq-2002-c-28",
"title": "Loi modifiant la Charte de la langue française",
"citation": "LQ 2002, c 28",
"type": "ANNUAL_STATUTE"
}
"#;
let legislation_result: LegislationResult = serde_json::from_str(data_legislation).unwrap();
println!("LegislationResult: {}", legislation_result.legislation_type);
let data_result = r#"
{
"case": {
"databaseId": "qccs",
"caseId": {
"fr": "2007qccs472"
},
"title": "9074-3527 Québec inc. c. R.",
"citation": "2007 QCCS 472 (CanLII)"
}
}
"#;
let data_result: Result = serde_json::from_str(data_result).unwrap();
println!("Result: {:?}", data_result.case);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment