From f38e420c3ec8bba3e92218647e7de46735075c9e Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Mon, 11 Apr 2022 07:59:55 -0700 Subject: [PATCH] Introduce ApiInfo::into_parts method We already support creating an ApiInfo object from its constituent parts. It is only consequent to be able to destructure such an object in an analogous way. To that end, this change introduce the ApiInfo::into_parts methods. --- CHANGELOG.md | 1 + src/api_info.rs | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d7fd5c4b..ce51edd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ Unreleased - Renamed `data::v2::bars::BarReq` to `BarsReq` - Deprecated `data::v2::bars::BarReq` - Introduced `data::v2::bars::BarsReqInit` type +- Introduced `ApiInfo::into_parts` method 0.22.4 diff --git a/src/api_info.rs b/src/api_info.rs index aeb0e4ce..4a39239b 100644 --- a/src/api_info.rs +++ b/src/api_info.rs @@ -1,4 +1,4 @@ -// Copyright (C) 2019-2021 The apca Developers +// Copyright (C) 2019-2022 The apca Developers // SPDX-License-Identifier: GPL-3.0-or-later use std::env::var_os; @@ -87,4 +87,41 @@ impl ApiInfo { secret, }) } + + /// Split this `ApiInfo` object back into its constituent parts. + /// + /// This method is the inverse of the [`from_parts`][Self::from_parts] + /// constructor. It returns a tuple comprising the base URL, key ID, + /// and secret. + pub fn into_parts(self) -> (String, String, String) { + let ApiInfo { + base_url, + key_id, + secret, + } = self; + (base_url.into(), key_id, secret) + } +} + + +#[cfg(test)] +mod tests { + use super::*; + + + /// Check that we can create an [`ApiInfo`] object from its + /// constituent parts and destructure it back into them. + #[test] + fn from_into_parts() { + let base_url = "https://paper-api.alpaca.markets/"; + let key_id = "XXXXXXXXXXXXXXXXXXXX"; + let secret = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"; + + let api_info = ApiInfo::from_parts(base_url, key_id, secret).unwrap(); + let (new_base_url, new_key_id, new_secret) = api_info.into_parts(); + + assert_eq!(new_base_url, base_url); + assert_eq!(new_key_id, key_id); + assert_eq!(new_secret, secret); + } }