config.rs 941 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. use std::convert::TryFrom;
  2. pub struct Config {
  3. pub http_port: u16,
  4. }
  5. impl Config {
  6. pub fn new() -> Self { Config { http_port: 3030 } }
  7. pub fn server_addr(&self) -> String { format!("0.0.0.0:{}", self.http_port) }
  8. }
  9. pub enum Environment {
  10. Local,
  11. Production,
  12. }
  13. impl Environment {
  14. #[allow(dead_code)]
  15. pub fn as_str(&self) -> &'static str {
  16. match self {
  17. Environment::Local => "local",
  18. Environment::Production => "production",
  19. }
  20. }
  21. }
  22. impl TryFrom<String> for Environment {
  23. type Error = String;
  24. fn try_from(s: String) -> Result<Self, Self::Error> {
  25. match s.to_lowercase().as_str() {
  26. "local" => Ok(Self::Local),
  27. "production" => Ok(Self::Production),
  28. other => Err(format!(
  29. "{} is not a supported environment. Use either `local` or `production`.",
  30. other
  31. )),
  32. }
  33. }
  34. }