Skip to content

Commit

Permalink
Introduce ApiInfo::into_parts method
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
d-e-s-o committed Apr 11, 2022
1 parent 3326417 commit f38e420
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 38 additions & 1 deletion src/api_info.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit f38e420

Please sign in to comment.