1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//! Load and parse DDMW application configuration file.

use std::path::{Path, PathBuf};

use serde::Deserialize;

use crate::auth::Auth;

use figment::{
  providers::{Format, Toml},
  Figment
};

use crate::conn::ProtAddr;
use crate::err::Error;
use crate::types::AppChannel;


#[derive(Debug, Default, Deserialize)]
pub struct Config {
  pub channel: Option<String>,
  pub auth: Option<Auth>,
  pub sender: Option<Sender>,
  pub receiver: Option<Receiver>
}

impl Config {
  pub fn set_appch(&mut self, appch: AppChannel) -> &mut Self {
    self.channel = Some(appch.to_string());
    self
  }

  pub fn get_appch(&self) -> Result<Option<AppChannel>, Error> {
    if let Some(appch) = &self.channel {
      match appch.parse::<AppChannel>() {
        Ok(appch) => {
          return Ok(Some(appch));
        }
        Err(e) => {
          let err = format!("AppChannel, {}", e);
          return Err(Error::parse(err));
        }
      }
    }
    Ok(None)
  }

  pub fn set_sender_msgif(&mut self, pa: ProtAddr) -> &mut Self {
    if self.sender.is_none() {
      self.sender = Some(Sender::default());
    }
    if let Some(ref mut sender) = self.sender {
      sender.msgif = Some(pa.to_string());
    }
    self
  }

  pub fn get_sender_msgif(&self) -> Result<Option<ProtAddr>, Error> {
    if let Some(sender) = &self.sender {
      if let Some(addr) = &sender.msgif {
        match addr.parse::<ProtAddr>() {
          Ok(addr) => {
            return Ok(Some(addr));
          }
          Err(e) => {
            let err = format!("ProtAddr, {}", e);
            return Err(Error::parse(err));
          }
        }
      }
    }
    Ok(None)
  }

  pub fn set_auth_account(&mut self, name: &str) -> &mut Self {
    if self.auth.is_none() {
      self.auth = Some(Auth::default());
    }
    if let Some(ref mut auth) = self.auth {
      auth.name = Some(name.to_string());
    }
    self
  }
  pub fn set_auth_pass(&mut self, pass: &str) -> &mut Self {
    if self.auth.is_none() {
      self.auth = Some(Auth::default());
    }
    if let Some(ref mut auth) = self.auth {
      auth.pass = Some(pass.to_string());
    }
    self
  }
  pub fn set_auth_pass_file(&mut self, passfile: &str) -> &mut Self {
    if self.auth.is_none() {
      self.auth = Some(Auth::default());
    }
    if let Some(ref mut auth) = self.auth {
      auth.pass_file = Some(passfile.to_string());
    }
    self
  }
  pub fn set_auth_token(&mut self, tkn: &str) -> &mut Self {
    if self.auth.is_none() {
      self.auth = Some(Auth::default());
    }
    if let Some(ref mut auth) = self.auth {
      auth.token = Some(tkn.to_string());
    }
    self
  }
  pub fn set_auth_token_file(&mut self, tknfile: &str) -> &mut Self {
    if self.auth.is_none() {
      self.auth = Some(Auth::default());
    }
    if let Some(ref mut auth) = self.auth {
      auth.token_file = Some(tknfile.to_string());
    }
    self
  }
}

#[derive(Debug, Default, Deserialize)]
pub struct Sender {
  pub mgmtif: Option<String>,
  pub msgif: Option<String>
}


#[derive(Debug, Default, Deserialize)]
pub struct Receiver {
  pub mgmtif: Option<String>,
  pub subif: Option<String>,
  #[serde(rename = "sub-retries")]
  pub sub_retries: Option<u32>,
  #[serde(rename = "sub-retry-delay")]
  pub sub_retry_delay: Option<String>,
  #[serde(rename = "push-listenif")]
  pub push_listenif: Option<String>
}


/// Load a DDMW application configuration file.
///
/// Attempt to load a configuration file in the following order:
/// 1. If `fname` has `Some` value, its value will be used.  Otherwise:
/// 2. If the environment variable `DDMW_APPCONF` is set, its value will be
///    used.  Otherwise:
/// 3. The filename `ddmwapp.toml`, in the current working directory, will be
///    used.
///
/// If none of these could be be found, `Ok(None)` will be returned.
///
/// # Example
/// Attempt to load a "hello.toml", and return a default `Config` buffer if
/// unsuccessful.
///
/// ```no_run
/// use std::path::Path;
/// use ddmw_client::conf::{Config, load};
/// use ddmw_client::Error;
/// fn get_conf() -> Result<Config, Error> {
///   let fname = Path::new("hello.toml");
///   Ok(load(Some(&fname))?.unwrap_or_default())
/// }
/// ```
pub fn load(fname: Option<&Path>) -> Result<Option<Config>, Error> {
  let f = match fname {
    Some(p) => p.to_path_buf(),
    None => match std::env::var_os("DDMW_APPCONF") {
      Some(val) => PathBuf::from(val),
      None => PathBuf::from("ddmwapp.toml")
    }
  };

  if !f.exists() {
    Ok(None)
  } else {
    let conf = Figment::new().merge(Toml::file(f)).extract()?;
    Ok(conf)
  }
}

// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :