aws_runtime/env_config/
property.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

//! Sections within an AWS config profile.

use std::collections::HashMap;
use std::fmt;

/// Key-Value property pair
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Property {
    key: String,
    value: String,
}

impl Property {
    /// Value of this property
    pub fn value(&self) -> &str {
        &self.value
    }

    /// Name of this property
    pub fn key(&self) -> &str {
        &self.key
    }

    /// Creates a new property
    pub fn new(key: String, value: String) -> Self {
        Property { key, value }
    }
}

type SectionKey = String;
type SectionName = String;
type PropertyName = String;
type SubPropertyName = String;
type PropertyValue = String;

/// A key for to a property value.
///
/// ```txt
/// # An example AWS profile config section with properties and sub-properties
/// [section-key section-name]
/// property-name = property-value
/// property-name =
///   sub-property-name = property-value
/// ```
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct PropertiesKey {
    section_key: SectionKey,
    section_name: SectionName,
    property_name: PropertyName,
    sub_property_name: Option<SubPropertyName>,
}

impl PropertiesKey {
    /// Create a new [`PropertiesKeyBuilder`].
    pub fn builder() -> PropertiesKeyBuilder {
        Default::default()
    }
}

impl fmt::Display for PropertiesKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let PropertiesKey {
            section_key,
            section_name,
            property_name,
            sub_property_name,
        } = self;
        match sub_property_name {
            Some(sub_property_name) => {
                write!(
                    f,
                    "[{section_key} {section_name}].{property_name}.{sub_property_name}"
                )
            }
            None => {
                write!(f, "[{section_key} {section_name}].{property_name}")
            }
        }
    }
}

/// Builder for [`PropertiesKey`]s.
#[derive(Debug, Default)]
pub struct PropertiesKeyBuilder {
    section_key: Option<SectionKey>,
    section_name: Option<SectionName>,
    property_name: Option<PropertyName>,
    sub_property_name: Option<SubPropertyName>,
}

impl PropertiesKeyBuilder {
    /// Set the section key for this builder.
    pub fn section_key(mut self, section_key: impl Into<String>) -> Self {
        self.section_key = Some(section_key.into());
        self
    }

    /// Set the section name for this builder.
    pub fn section_name(mut self, section_name: impl Into<String>) -> Self {
        self.section_name = Some(section_name.into());
        self
    }

    /// Set the property name for this builder.
    pub fn property_name(mut self, property_name: impl Into<String>) -> Self {
        self.property_name = Some(property_name.into());
        self
    }

    /// Set the sub-property name for this builder.
    pub fn sub_property_name(mut self, sub_property_name: impl Into<String>) -> Self {
        self.sub_property_name = Some(sub_property_name.into());
        self
    }

    /// Build this builder. If all required fields are set,
    /// `Ok(PropertiesKey)` is returned. Otherwise, an error is returned.
    pub fn build(self) -> Result<PropertiesKey, String> {
        Ok(PropertiesKey {
            section_key: self
                .section_key
                .ok_or("A section_key is required".to_owned())?,
            section_name: self
                .section_name
                .ok_or("A section_name is required".to_owned())?,
            property_name: self
                .property_name
                .ok_or("A property_name is required".to_owned())?,
            sub_property_name: self.sub_property_name,
        })
    }
}

/// A map of [`PropertiesKey`]s to property values.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Properties {
    inner: HashMap<PropertiesKey, PropertyValue>,
}

#[allow(dead_code)]
impl Properties {
    /// Create a new empty [`Properties`].
    pub fn new() -> Self {
        Default::default()
    }

    #[cfg(test)]
    pub(crate) fn new_from_slice(slice: &[(PropertiesKey, PropertyValue)]) -> Self {
        let mut properties = Self::new();
        for (key, value) in slice {
            properties.insert(key.clone(), value.clone());
        }
        properties
    }

    /// Insert a new key/value pair into this map.
    pub fn insert(&mut self, properties_key: PropertiesKey, value: PropertyValue) {
        let _ = self
            .inner
            // If we don't clone then we don't get to log a useful warning for a value getting overwritten.
            .entry(properties_key.clone())
            .and_modify(|v| {
                tracing::trace!("overwriting {properties_key}: was {v}, now {value}");
                *v = value.clone();
            })
            .or_insert(value);
    }

    /// Given a [`PropertiesKey`], return the corresponding value, if any.
    pub fn get(&self, properties_key: &PropertiesKey) -> Option<&PropertyValue> {
        self.inner.get(properties_key)
    }
}