aws-sdk-datasync 1.115.0

AWS SDK for AWS DataSync
Documentation
// Code generated by software.amazon.smithy.rust.codegen.smithy-rs. DO NOT EDIT.
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

#![allow(dead_code)]

use aws_smithy_http::header::set_request_header_if_absent;
use aws_smithy_types::config_bag::{Storable, StoreReplace};
use http_1x::header::{HeaderName, CONTENT_LENGTH, CONTENT_TYPE};

/// Configuration for how default protocol headers are serialized
#[derive(Clone, Debug, Default)]
pub(crate) struct HeaderSerializationSettings {
    omit_default_content_length: bool,
    omit_default_content_type: bool,
}

impl HeaderSerializationSettings {
    /// Creates new [`HeaderSerializationSettings`]
    pub(crate) fn new() -> Self {
        Default::default()
    }

    /// Omit the default `Content-Length` header during serialization
    pub(crate) fn omit_default_content_length(self) -> Self {
        Self {
            omit_default_content_length: true,
            ..self
        }
    }

    /// Omit the default `Content-Type` header during serialization
    pub(crate) fn omit_default_content_type(self) -> Self {
        Self {
            omit_default_content_type: true,
            ..self
        }
    }

    /// Returns whether the default `Content-Length` header should be omitted.
    /// Used by schema-serde-generated request serializers, which insert the
    /// header directly on `aws_smithy_runtime_api::http::Request` rather than
    /// going through `set_default_header`.
    pub(crate) fn should_omit_default_content_length(&self) -> bool {
        self.omit_default_content_length
    }

    /// Returns whether the default `Content-Type` header should be omitted.
    /// Used by schema-serde-generated request serializers, which insert the
    /// header directly on `aws_smithy_runtime_api::http::Request` rather than
    /// going through `set_default_header`.
    pub(crate) fn should_omit_default_content_type(&self) -> bool {
        self.omit_default_content_type
    }

    /// Returns true if the given default header name should be serialized
    fn include_header(&self, header: &HeaderName) -> bool {
        (!self.omit_default_content_length || header != CONTENT_LENGTH) && (!self.omit_default_content_type || header != CONTENT_TYPE)
    }

    /// Sets a default header on the given request builder if it should be serialized
    pub(crate) fn set_default_header(
        &self,
        mut request: http_1x::request::Builder,
        header_name: HeaderName,
        value: &str,
    ) -> http_1x::request::Builder {
        if self.include_header(&header_name) {
            request = set_request_header_if_absent(request, header_name, value);
        }
        request
    }
}

impl Storable for HeaderSerializationSettings {
    type Storer = StoreReplace<Self>;
}

// Bridge to the schema-serde runtime guard. The schema-serde runtime in
// `aws-smithy-schema` reads omit flags from the config bag via the abstract
// `HeaderOmitSettings` trait so it doesn't have to depend on this concrete
// inlineable type. The presigning interceptor stores the same settings under
// both `HeaderSerializationSettings` (read by codegen-emitted streaming/
// payload paths) and `SharedHeaderOmitSettings` (read by the runtime's
// standard-body path); this trait impl is what lets the same concrete value
// satisfy both lookups.
impl aws_smithy_schema::header_omit_settings::HeaderOmitSettings for HeaderSerializationSettings {
    fn should_omit_default_content_type(&self) -> bool {
        self.omit_default_content_type
    }

    fn should_omit_default_content_length(&self) -> bool {
        self.omit_default_content_length
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_include_header() {
        let settings = HeaderSerializationSettings::default();
        assert!(settings.include_header(&CONTENT_LENGTH));
        assert!(settings.include_header(&CONTENT_TYPE));

        let settings = HeaderSerializationSettings::default().omit_default_content_length();
        assert!(!settings.include_header(&CONTENT_LENGTH));
        assert!(settings.include_header(&CONTENT_TYPE));

        let settings = HeaderSerializationSettings::default().omit_default_content_type();
        assert!(settings.include_header(&CONTENT_LENGTH));
        assert!(!settings.include_header(&CONTENT_TYPE));

        let settings = HeaderSerializationSettings::default()
            .omit_default_content_type()
            .omit_default_content_length();
        assert!(!settings.include_header(&CONTENT_LENGTH));
        assert!(!settings.include_header(&CONTENT_TYPE));
    }
}