use super::Provider;
use crate::config::NativeAddress;
use crate::{Result, SecretSpecError};
use std::borrow::Cow;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum Address<'a> {
Convention {
project: &'a str,
profile: &'a str,
key: &'a str,
},
Native(&'a NativeAddress),
}
impl<'a> Address<'a> {
pub fn convention(project: &'a str, profile: &'a str, key: &'a str) -> Self {
Self::Convention {
project,
profile,
key,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) enum OwnedAddress {
Convention {
project: String,
profile: String,
key: String,
},
Native(NativeAddress),
}
impl OwnedAddress {
pub(crate) fn convention(project: &str, profile: &str, key: &str) -> Self {
Self::Convention {
project: project.to_string(),
profile: profile.to_string(),
key: key.to_string(),
}
}
pub(crate) fn as_address(&self) -> Address<'_> {
match self {
Self::Convention {
project,
profile,
key,
} => Address::Convention {
project,
profile,
key,
},
Self::Native(reference) => Address::Native(reference),
}
}
pub(crate) fn native(&self) -> Option<&NativeAddress> {
match self {
Self::Native(reference) => Some(reference),
Self::Convention { .. } => None,
}
}
}
pub(super) fn reject_unsupported_coords(
provider: &str,
addr: &NativeAddress,
supported: &[&str],
) -> Result<()> {
for (name, value) in addr.coordinates() {
if name == "item" || value.is_none() {
continue;
}
if !supported.contains(&name) {
return Err(SecretSpecError::ProviderOperationFailed(format!(
"the {provider} provider does not support the `{name}` coordinate. \
Drop `{name}` from the ref for `{item}`, or give this provider its \
own address with `refs.<alias>` or an alias `ref` template (0.19+): \
https://secretspec.dev/concepts/references/#different-coordinates-per-provider-019",
item = addr.item
)));
}
}
Ok(())
}
pub(crate) fn flat_item<'a, P: Provider + ?Sized>(
provider: &P,
addr: Address<'a>,
) -> Result<Cow<'a, str>> {
match provider.resolve_coords(addr)? {
Cow::Borrowed(native) => Ok(Cow::Borrowed(native.item.as_str())),
Cow::Owned(native) => Ok(Cow::Owned(native.item)),
}
}