Refactor container definitions

This commit is contained in:
GW_MC
2025-12-22 14:32:57 +08:00
parent 7db23b01df
commit 6e85bda13f
12 changed files with 92 additions and 91 deletions

View File

@@ -1,7 +1,5 @@
use std::io::Write;
use shared::db_type::DBType;
#[derive(Clone, Copy)]
pub enum EnvFileType {
DotEnv,
@@ -11,25 +9,16 @@ pub enum EnvFileType {
#[derive(Clone)]
pub struct EnvFile {
pub file_type: EnvFileType,
pub db_type: DBType,
pub db_url: String,
//
buffer: serde_json::Value,
}
impl EnvFile {
pub fn new(file_type: EnvFileType, db_type: DBType, db_url: String) -> Self {
let mut env_file = EnvFile {
pub fn new(file_type: EnvFileType) -> Self {
EnvFile {
file_type,
db_type,
db_url,
buffer: serde_json::Value::Object(serde_json::Map::new()),
};
env_file._write_line_buffer("DATABASE__TYPE", &env_file.db_type.to_string());
env_file._write_line_buffer("DATABASE__URL", &env_file.db_url.to_string());
env_file
}
}
pub fn write_line(&mut self, key: &str, value: &str) {
@@ -131,12 +120,10 @@ mod tests {
#[test]
fn test_env_file_write_yaml() {
let mut env_file_nested = EnvFile::new(
EnvFileType::Yaml,
DBType::SQLite,
"mysql://user:pass@localhost/db".to_string(),
);
let mut env_file_nested = EnvFile::new(EnvFileType::Yaml);
env_file_nested.write_line("DATABASE__TYPE", "SQLite");
env_file_nested.write_line("DATABASE__URL", "mysql://user:pass@localhost/db");
let mut output_stream = Vec::new();
env_file_nested.write(&mut output_stream, false);
let output_string = String::from_utf8(output_stream).unwrap();
@@ -150,11 +137,9 @@ DATABASE:
#[test]
fn test_env_file_write_env() {
let mut env_file_nested = EnvFile::new(
EnvFileType::DotEnv,
DBType::PostgreSQL,
"postgres://user:pass@localhost/db".to_string(),
);
let mut env_file_nested = EnvFile::new(EnvFileType::DotEnv);
env_file_nested.write_line("DATABASE__TYPE", "PostgreSQL");
env_file_nested.write_line("DATABASE__URL", "postgres://user:pass@localhost/db");
let mut output_stream = Vec::new();
env_file_nested.write(&mut output_stream, true);
let output_string = String::from_utf8(output_stream).unwrap();