Skip to content

Commit

Permalink
feat(helpers): Add helpers for reading/parsing JSON
Browse files Browse the repository at this point in the history
This is boilerplate that can easily be provided to users of the library.
  • Loading branch information
dermesser committed Aug 31, 2016
1 parent 85b12dd commit c4231e9
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/helper.rs
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),
}
}

0 comments on commit c4231e9

Please sign in to comment.