Skip to content

Commit

Permalink
refactor: scope each config item
Browse files Browse the repository at this point in the history
This way, the configuration will be cleaner.
It doesn't have to use the long variable name for each field,
as each of them shares a similar name, such as `url`, `username`, and so on.

Now, each config is scoped, and only the final object is returned, making the code much cleaner.
  • Loading branch information
azzamsa committed Jan 8, 2024
1 parent 2e1c817 commit f9d8e0b
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,24 @@ impl Config {
Err(_) => None,
};

// http
let http_port = std::env::var(ENV_HTTP_PORT)
.ok()
.map_or(Ok(DEFAULT_HTTP_PORT), |env_val| env_val.parse::<u16>())?;

let http = Http { port: http_port };

// database
let database_url =
std::env::var(ENV_DATABASE_URL).map_err(|_| env_not_found(ENV_DATABASE_URL))?;
let database_pool_size = std::env::var(ENV_DATABASE_POOL_SIZE)
.ok()
.map_or(Ok(DEFAULT_DATABASE_POOL_SIZE), |pool_size_str| {
pool_size_str.parse::<u32>()
})?;

let database = Database {
url: database_url,
pool_size: database_pool_size,
let http = {
let port = std::env::var(ENV_HTTP_PORT)
.ok()
.map_or(Ok(DEFAULT_HTTP_PORT), |env_val| env_val.parse::<u16>())?;

Http { port }
};

let database = {
let url =
std::env::var(ENV_DATABASE_URL).map_err(|_| env_not_found(ENV_DATABASE_URL))?;
let pool_size = std::env::var(ENV_DATABASE_POOL_SIZE)
.ok()
.map_or(Ok(DEFAULT_DATABASE_POOL_SIZE), |pool_size_str| {
pool_size_str.parse::<u32>()
})?;

Database { url, pool_size }
};

let mut config = Self {
Expand Down

0 comments on commit f9d8e0b

Please sign in to comment.