use serde::{
Deserialize,
Serialize,
};
use strum::{
AsRefStr,
Display,
EnumString,
};
/// The version of encryption used to encrypt a secret.
#[remain::sorted]
#[derive(
AsRefStr, Clone, Copy, Debug, Deserialize, Display, EnumString, Eq, PartialEq, Serialize,
)]
#[serde(rename_all = "camelCase")]
#[strum(serialize_all = "camelCase")]
pub enum SecretVersion {
/// Version 1 of the encryption
V1,
}
impl Default for SecretVersion {
fn default() -> Self {
Self::V1
}
}
/// The algorithm used to encrypt a secret.
#[remain::sorted]
#[derive(
AsRefStr, Clone, Copy, Debug, Deserialize, Display, EnumString, Eq, PartialEq, Serialize,
)]
#[serde(rename_all = "camelCase")]
#[strum(serialize_all = "camelCase")]
pub enum SecretAlgorithm {
/// The "sealedbox" encryption algorithm, provided by libsodium
Sealedbox,
}
impl Default for SecretAlgorithm {
fn default() -> Self {
Self::Sealedbox
}
}
#[cfg(test)]
mod tests {
use super::*;
mod secret_version {
use super::*;
#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
struct Object {
version: SecretVersion,
}
fn str() -> &'static str {
r#"{"version":"v1"}"#
}
fn invalid() -> &'static str {
r#"{"version":"nope"}"#
}
fn object() -> Object {
Object {
version: SecretVersion::V1,
}
}
#[test]
fn serialize() {
assert_eq!(
str(),
serde_json::to_string(&object()).expect("failed to serialize")
);
}
#[test]
fn deserialize() {
assert_eq!(
object(),
serde_json::from_str(str()).expect("failed to deserialize")
);
}
#[allow(clippy::panic)]
#[test]
fn deserialize_invalid() {
if serde_json::from_str::<Object>(invalid()).is_ok() {
panic!("deserialize should not succeed")
}
}
#[test]
fn default() {
// This test is intended to catch if and when we update the default variant for this
// type
assert_eq!(SecretAlgorithm::Sealedbox, SecretAlgorithm::default())
}
}
mod secret_algorithm {
use super::*;
#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
struct Object {
algorithm: SecretAlgorithm,
}
fn str() -> &'static str {
r#"{"algorithm":"sealedbox"}"#
}
fn invalid() -> &'static str {
r#"{"algorithm":"nope"}"#
}
fn object() -> Object {
Object {
algorithm: SecretAlgorithm::Sealedbox,
}
}
#[test]
fn serialize() {
assert_eq!(
str(),
serde_json::to_string(&object()).expect("failed to serialize")
);
}
#[test]
fn deserialize() {
assert_eq!(
object(),
serde_json::from_str(str()).expect("failed to deserialize")
);
}
#[allow(clippy::panic)]
#[test]
fn deserialize_invalid() {
if serde_json::from_str::<Object>(invalid()).is_ok() {
panic!("deserialize should not succeed")
}
}
#[test]
fn default() {
// This test is intended to catch if and when we update the default variant for this
// type
assert_eq!(SecretAlgorithm::Sealedbox, SecretAlgorithm::default())
}
}
}