From ff23de0e9d6cae3aea6944b6ef5056dd9785d813 Mon Sep 17 00:00:00 2001 From: bouzuya Date: Sat, 30 Mar 2024 09:50:26 +0900 Subject: [PATCH] v4-sign: Extract service mod --- v4-sign/src/lib.rs | 7 +++++-- v4-sign/src/service.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 v4-sign/src/service.rs diff --git a/v4-sign/src/lib.rs b/v4-sign/src/lib.rs index 381dd74..19f97d7 100644 --- a/v4-sign/src/lib.rs +++ b/v4-sign/src/lib.rs @@ -1,4 +1,5 @@ mod request_type; +mod service; mod signing_algorithm; use std::{ @@ -9,6 +10,8 @@ use std::{ use request_type::RequestType; use signing_algorithm::SigningAlgorithm; +use crate::service::Service; + #[derive(Debug, thiserror::Error)] enum Error { #[error("FIXME: {0}")] @@ -41,7 +44,7 @@ fn construct_credential_scope( ) -> String { let date = request_timestamp.format("%Y%m%d").to_string(); let location = region; // e.g. "us-central1"; - let service = "storage"; + let service = Service::Storage.as_str(); let request_type = RequestType::Goog4Request.as_str(); format!("{date}/{location}/{service}/{request_type}") } @@ -344,7 +347,7 @@ UNSIGNED-PAYLOAD let credential_scope = { let date = date_time.format("%Y%m%d").to_string(); let location = "us-central1"; - let service = "storage"; + let service = Service::Storage.as_str(); let request_type = RequestType::Goog4Request.as_str(); format!("{date}/{location}/{service}/{request_type}") }; diff --git a/v4-sign/src/service.rs b/v4-sign/src/service.rs new file mode 100644 index 0000000..5666b3b --- /dev/null +++ b/v4-sign/src/service.rs @@ -0,0 +1,27 @@ +/// +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub(crate) enum Service { + Storage, + S3, +} + +impl Service { + pub(crate) fn as_str(&self) -> &'static str { + match self { + Self::Storage => "storage", + Self::S3 => "s3", + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test() { + use Service::*; + assert_eq!(Storage.as_str(), "storage"); + assert_eq!(S3.as_str(), "s3"); + } +}