aws_config/environment/
mod.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Providers that load configuration from environment variables
7
8use std::error::Error;
9use std::fmt;
10
11/// Load credentials from the environment
12pub mod credentials;
13pub use credentials::EnvironmentVariableCredentialsProvider;
14
15/// Load regions from the environment
16pub mod region;
17pub use region::EnvironmentVariableRegionProvider;
18
19#[derive(Debug)]
20pub(crate) struct InvalidBooleanValue {
21    value: String,
22}
23
24impl fmt::Display for InvalidBooleanValue {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        write!(f, "{} is not a valid boolean", self.value)
27    }
28}
29
30impl Error for InvalidBooleanValue {}
31
32pub(crate) fn parse_bool(value: &str) -> Result<bool, InvalidBooleanValue> {
33    if value.eq_ignore_ascii_case("false") {
34        Ok(false)
35    } else if value.eq_ignore_ascii_case("true") {
36        Ok(true)
37    } else {
38        Err(InvalidBooleanValue {
39            value: value.to_string(),
40        })
41    }
42}
43
44#[derive(Debug)]
45pub(crate) struct InvalidUintValue {
46    value: String,
47}
48
49impl fmt::Display for InvalidUintValue {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        write!(f, "{} is not a valid u32", self.value)
52    }
53}
54
55impl Error for InvalidUintValue {}
56
57pub(crate) fn parse_uint(value: &str) -> Result<u32, InvalidUintValue> {
58    value.parse::<u32>().map_err(|_| InvalidUintValue {
59        value: value.to_string(),
60    })
61}
62
63#[derive(Debug)]
64pub(crate) struct InvalidUrlValue {
65    value: String,
66}
67
68impl fmt::Display for InvalidUrlValue {
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        write!(f, "{} is not a valid URL", self.value)
71    }
72}
73
74impl Error for InvalidUrlValue {}
75
76pub(crate) fn parse_url(value: &str) -> Result<String, InvalidUrlValue> {
77    match url::Url::parse(value) {
78        // We discard the parse result because it includes a trailing slash
79        Ok(_) => Ok(value.to_string()),
80        Err(_) => Err(InvalidUrlValue {
81            value: value.to_string(),
82        }),
83    }
84}