-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(helpers): Add helpers for reading/parsing JSON
This is boilerplate that can easily be provided to users of the library.
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#![allow(dead_code)] | ||
|
||
//! Helper functions allowing you to avoid writing boilerplate code for common operations, such as | ||
//! parsing JSON or reading files. | ||
// Copyright (c) 2016 Google Inc ([email protected]). | ||
// | ||
// Refer to the project root for licensing information. | ||
|
||
use serde_json; | ||
use std::io; | ||
use std::fs; | ||
|
||
use types::ApplicationSecret; | ||
|
||
pub fn read_application_secret(file: &String) -> io::Result<ApplicationSecret> { | ||
use std::io::Read; | ||
|
||
let mut secret = String::new(); | ||
let mut file = try!(fs::OpenOptions::new().read(true).open(file)); | ||
try!(file.read_to_string(&mut secret)); | ||
|
||
parse_application_secret(&secret) | ||
} | ||
|
||
pub fn parse_application_secret(secret: &String) -> io::Result<ApplicationSecret> { | ||
match serde_json::from_str(secret) { | ||
Err(e) => { | ||
Err(io::Error::new(io::ErrorKind::InvalidData, | ||
format!("Bad application secret: {}", e))) | ||
} | ||
Ok(decoded) => Ok(decoded), | ||
} | ||
} |