Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::error::Error;
- use std::fmt;
- use std::fmt::Display;
- use std::io::Write;
- #[derive(Debug)]
- pub struct BasicError {
- message: String,
- }
- impl BasicError {
- pub fn new(message: String) -> Self {
- Self { message }
- }
- }
- //impl Error for BasicError {
- // fn description(&self) -> &str {
- // &self.message
- // }
- //}
- impl Display for BasicError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "{}", self.message)
- }
- }
- impl From<Box<dyn Error>> for BasicError {
- fn from(err: Box<dyn Error>) -> Self {
- Self::new(err.description().to_string())
- }
- }
- impl From<&str> for BasicError {
- fn from(value: &str) -> Self {
- Self::new(value.to_string())
- }
- }
- impl From<String> for BasicError {
- fn from(value: String) -> Self {
- Self::new(value)
- }
- }
- #[derive(PartialEq)]
- pub enum Environment {
- Development,
- Staging,
- Production,
- }
- pub struct AppEnv {
- pub environment: Environment,
- pub base_url: String,
- }
- impl AppEnv {
- pub fn from_std_env() -> Result<AppEnv, Box<BasicError>> {
- const ENVIRONMENT_KEY: &'static str = "ENVIRONMENT";
- const BASE_URL_KEY: &'static str = "BASE_URL";
- let environment_str = std::env::var(ENVIRONMENT_KEY)?;
- let base_url = std::env::var(BASE_URL_KEY)?;
- let environment = match environment_str.as_ref() {
- "DEVELOPMENT" => Ok(Environment::Development),
- "STAGING" => Ok(Environment::Staging),
- "PRODUCTION" => Ok(Environment::Production),
- key => Err(format!(
- "Unable to parse environment variable '{} -> {}'",
- ENVIRONMENT_KEY, environment_str
- )),
- }?;
- Ok(AppEnv {
- base_url,
- environment,
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement