//! Parsers and helpers for SQL and related inputs.
//!
//! Submodules contain concrete parsers.
pub mod query_builder;
pub mod sql;

use std::env::var;
use tokio::time::Duration;

pub fn resolve_postgres_uri(uri: &str) -> String {
    if let Some(env_name) = uri.strip_prefix("${").and_then(|v| v.strip_suffix("}")) {
        var(env_name).unwrap_or_else(|_| uri.to_string())
    } else {
        uri.to_string()
    }
}

pub fn parse_secs_or_default(value: Option<&String>, default_secs: u64) -> Duration {
    Duration::from_secs(
        value
            .and_then(|v| v.parse::<u64>().ok())
            .unwrap_or(default_secs),
    )
}

pub fn parse_usize(value: &String) -> Option<usize> {
    value.parse().ok()
}
