feat(helpers): Add helpers for reading/parsing JSON

This is boilerplate that can easily be provided to users of the library.
This commit is contained in:
Lewin Bormann
2016-08-31 20:01:40 +02:00
parent 85b12dd3e8
commit c4231e9499

34
src/helper.rs Normal file
View File

@@ -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 (lewinb@google.com).
//
// 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),
}
}