use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Variable {
pub key: String,
pub value: String,
pub variable_type: VariableType,
pub protected: bool,
pub masked: bool,
#[serde(default)]
pub hidden: bool,
#[serde(default = "default_raw")]
pub raw: bool,
#[serde(default = "default_environment_scope")]
pub environment_scope: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
}
fn default_raw() -> bool {
true
}
fn default_environment_scope() -> String {
"*".to_string()
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "snake_case")]
pub enum VariableType {
#[default]
EnvVar,
File,
}
#[derive(Debug, Clone, Default)]
pub struct VariableOptions {
pub protected: bool,
pub masked: bool,
pub raw: bool,
pub variable_type: VariableType,
pub environment_scope: Option<String>,
pub description: Option<String>,
}
impl VariableOptions {
pub fn new() -> Self {
Self {
protected: false,
masked: false,
raw: true,
variable_type: VariableType::EnvVar,
environment_scope: None,
description: None,
}
}
pub fn protected(mut self, protected: bool) -> Self {
self.protected = protected;
self
}
pub fn masked(mut self, masked: bool) -> Self {
self.masked = masked;
self
}
pub fn raw(mut self, raw: bool) -> Self {
self.raw = raw;
self
}
pub fn variable_type(mut self, variable_type: VariableType) -> Self {
self.variable_type = variable_type;
self
}
pub fn environment_scope(mut self, scope: impl Into<String>) -> Self {
self.environment_scope = Some(scope.into());
self
}
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_variable_type_default() {
assert_eq!(VariableType::default(), VariableType::EnvVar);
}
#[test]
fn test_variable_options_builder() {
let opts = VariableOptions::new()
.protected(true)
.masked(true)
.variable_type(VariableType::File)
.description("Test variable");
assert!(opts.protected);
assert!(opts.masked);
assert_eq!(opts.variable_type, VariableType::File);
assert_eq!(opts.description, Some("Test variable".to_string()));
}
}