use super::connect_options::DescriptionList;
use super::connect_options::DescriptionOption;
use super::connect_string_parser;
use super::defaults;
use crate::error::Error;
use crate::secret_value::SecretValue;
#[derive(Clone, PartialEq, Eq)]
pub struct Config {
user: Option<String>,
password: Option<SecretValue>,
new_password: Option<SecretValue>,
config_dir: Option<String>,
description_list: Option<DescriptionList>,
stmtcachesize: Option<usize>,
auth_mode: Option<u8>,
driver_name: Option<String>,
osuser: Option<String>,
program: Option<String>,
machine: Option<String>,
terminal: Option<String>,
cclass: Option<String>,
wallet_location: Option<String>,
wallet_password: Option<SecretValue>,
}
impl Config {
pub(crate) fn get_new_password_bytes(&self) -> Option<Vec<u8>> {
self.new_password.as_ref().map(|s| s.get_value())
}
pub(crate) fn get_options(&self) -> Result<Vec<DescriptionOption>, Error> {
self.description_list.as_ref().unwrap().get_options(self)
}
pub(crate) fn get_password_bytes(&self) -> Vec<u8> {
self.password.as_ref().unwrap().get_value()
}
pub(crate) fn get_sdu(&self) -> usize {
if let Some(description_list) = &self.description_list {
description_list.sdu()
} else {
0
}
}
pub(crate) fn get_wallet_password_bytes(&self) -> Vec<u8> {
self.wallet_password
.as_ref()
.map(|value| value.get_value())
.unwrap_or_default()
}
pub(crate) fn validate(&self) -> Result<(), Error> {
if self.user.is_none() || self.password.is_none() {
Err(Error::no_credentials())
} else if self.description_list.is_none() {
Err(Error::no_connect_string())
} else {
Ok(())
}
}
pub fn auth_mode(&self) -> u8 {
self.auth_mode.unwrap_or(0)
}
pub fn cclass(&self) -> Option<&str> {
self.cclass.as_deref()
}
pub fn driver_name(&self) -> &str {
self.driver_name
.as_deref()
.unwrap_or(defaults::default_driver_name())
}
pub fn get_connect_descriptor(&self) -> String {
match self.description_list.as_ref() {
Some(description_list) => description_list.build_connect_string(),
None => String::new(),
}
}
pub fn machine(&self) -> &str {
self.machine
.as_deref()
.unwrap_or(defaults::default_machine())
}
pub fn osuser(&self) -> &str {
self.osuser.as_deref().unwrap_or(defaults::default_osuser())
}
pub fn program(&self) -> &str {
self.program
.as_deref()
.unwrap_or(defaults::default_program())
}
pub fn set_auth_mode(mut self, value: u8) -> Self {
self.auth_mode = Some(value);
self
}
pub fn set_cclass(mut self, value: impl Into<String>) -> Self {
self.cclass = Some(value.into());
self
}
pub fn set_config_dir(mut self, value: &str) -> Self {
self.config_dir = Some(value.into());
self
}
pub fn set_connect_string(
mut self,
connect_string: &str,
) -> Result<Self, Error> {
self.description_list =
Some(connect_string_parser::parse_connect_string_or_lookup_alias(
connect_string,
&self.config_dir,
)?);
Ok(self)
}
pub fn set_credentials(self, user: &str, password: &str) -> Self {
self.set_user(user).set_password(password)
}
pub fn set_driver_name(mut self, value: impl Into<String>) -> Self {
self.driver_name = Some(value.into());
self
}
pub fn set_machine(
mut self,
value: impl Into<String>,
) -> Result<Self, Error> {
let machine = value.into();
connect_string_parser::validate_network_name(&machine)?;
self.machine = Some(machine);
Ok(self)
}
pub fn set_new_password(mut self, value: &str) -> Self {
if value.is_empty() {
self.new_password = None;
} else {
self.new_password = Some(SecretValue::new(value.as_bytes()));
}
self
}
pub fn set_osuser(
mut self,
value: impl Into<String>,
) -> Result<Self, Error> {
let osuser = value.into();
connect_string_parser::validate_network_name(&osuser)?;
self.osuser = Some(osuser);
Ok(self)
}
pub fn set_password(mut self, value: &str) -> Self {
if value.is_empty() {
self.password = None;
} else {
self.password = Some(SecretValue::new(value.as_bytes()));
}
self
}
pub fn set_program(
mut self,
value: impl Into<String>,
) -> Result<Self, Error> {
let program = value.into();
connect_string_parser::validate_network_name(&program)?;
self.program = Some(program);
Ok(self)
}
pub fn set_stmtcachesize(mut self, value: usize) -> Self {
self.stmtcachesize = Some(value);
self
}
pub fn set_terminal(mut self, value: impl Into<String>) -> Self {
self.terminal = Some(value.into());
self
}
pub fn set_user(mut self, value: &str) -> Self {
if value.is_empty() {
self.user = None;
} else {
self.user = Some(value.into());
}
self
}
pub fn set_wallet_location(mut self, value: impl Into<String>) -> Self {
self.wallet_location = Some(value.into());
self
}
pub fn set_wallet_password(mut self, value: &str) -> Self {
if value.is_empty() {
self.wallet_password = None;
} else {
self.wallet_password = Some(SecretValue::new(value.as_bytes()));
}
self
}
pub fn stmtcachesize(&self) -> usize {
self.stmtcachesize.unwrap_or(20)
}
pub fn terminal(&self) -> &str {
self.terminal.as_deref().unwrap_or("unknown")
}
pub fn user(&self) -> Option<&str> {
self.user.as_deref()
}
pub fn wallet_location(&self) -> Option<&str> {
self.wallet_location.as_deref()
}
}
impl Default for Config {
fn default() -> Self {
Self {
user: None,
password: None,
new_password: None,
config_dir: defaults::default_config_dir().clone(),
description_list: None,
stmtcachesize: None,
auth_mode: None,
driver_name: None,
osuser: None,
program: None,
machine: None,
terminal: None,
cclass: None,
wallet_location: None,
wallet_password: None,
}
}
}