1use std::env;
21use std::io;
22use std::ffi::OsString;
23use serde_json;
24
25use Result;
26use Error;
27
28
29#[derive(Serialize, Deserialize)]
30pub struct Parameters {
31 cc: OsString,
32 cxx: OsString,
33 target: OsString,
34}
35
36impl Parameters {
37 pub fn new(cc: &str, cxx: &str, target: &str) -> Parameters {
38 Parameters {
39 cc: OsString::from(cc),
40 cxx: OsString::from(cxx),
41 target: OsString::from(target)
42 }
43 }
44
45 pub fn get_cc(&self) -> &OsString {
46 &self.cc
47 }
48
49 pub fn get_cxx(&self) -> &OsString {
50 &self.cxx
51 }
52
53 pub fn get_target(&self) -> &OsString {
54 &self.target
55 }
56
57 pub fn write(&self) -> Result<OsString> {
59 let result = serde_json::to_string(self)?;
60 Ok(OsString::from(result))
61 }
62
63 pub fn read(source: &OsString) -> Result<Parameters> {
65 match source.to_str() {
66 Some(string) => {
67 let result = serde_json::from_str(string)?;
68 Ok(result)
69 }
70 None => Err(Error::Io(io::Error::from(io::ErrorKind::InvalidInput)))
71 }
72 }
73}
74
75
76const ENV_KEY: &'static str = "__BEAR";
77
78pub fn to_env(parameters: &Parameters) -> Result<(OsString, OsString)> {
80 let key: OsString = OsString::from(ENV_KEY);
81 parameters.write().map(|value| (key, value))
82}
83
84pub fn from_env() -> Result<Parameters> {
86 let value = env::var(ENV_KEY)?;
87 Parameters::read(&OsString::from(&value))
88}