-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
155 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::*; | ||
|
||
// TODO: either use typestate or newtype for plaintext and encrypted file differentiation. | ||
|
||
#[derive(Debug, PartialEq, Serialize, Deserialize)] | ||
pub struct RopsFile<F: FileFormat> { | ||
#[serde(flatten)] | ||
pub map: F::Map, | ||
#[serde(rename = "sops")] | ||
pub metadata: RopsFileAgeMetadata, | ||
} | ||
|
||
#[cfg(feature = "test-utils")] | ||
mod mock { | ||
use super::*; | ||
|
||
impl<F: FileFormat> MockTestUtil for RopsFile<F> | ||
where | ||
F::Map: MockTestUtil, | ||
{ | ||
fn mock() -> Self { | ||
Self { | ||
map: F::Map::mock(), | ||
metadata: MockTestUtil::mock(), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "yaml")] | ||
mod yaml { | ||
use super::*; | ||
|
||
impl MockFileFormatUtil<YamlFileFormat> for RopsFile<YamlFileFormat> { | ||
fn mock_format_display() -> String { | ||
indoc::formatdoc! {" | ||
{} | ||
sops: | ||
{}", | ||
<YamlFileFormat as FileFormat>::Map::mock_format_display(), | ||
textwrap::indent(&RopsFileAgeMetadata::mock_format_display()," ") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[cfg(feature = "yaml")] | ||
mod yaml { | ||
use crate::*; | ||
|
||
#[test] | ||
fn serializes_rops_file() { | ||
// TEMP: | ||
println!("{}", RopsFile::<YamlFileFormat>::mock_format_display()); | ||
println!("{}", serde_yaml::to_string(&RopsFile::<YamlFileFormat>::mock()).unwrap()); | ||
|
||
FileFormatTestUtils::assert_serialization::<YamlFileFormat, RopsFile<YamlFileFormat>>() | ||
} | ||
|
||
#[test] | ||
fn deserializes_rops_file() { | ||
FileFormatTestUtils::assert_deserialization::<YamlFileFormat, RopsFile<YamlFileFormat>>() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use serde::{de::DeserializeOwned, Serialize}; | ||
|
||
pub trait FileFormat { | ||
type Map; | ||
type SerializeError: std::error::Error + Send + Sync + 'static; | ||
type DeserializeError: std::error::Error + Send + Sync + 'static; | ||
|
||
fn serialize_to_string<T: Serialize>(t: &T) -> Result<String, Self::SerializeError>; | ||
fn deserialize_from_str<T: DeserializeOwned>(str: &str) -> Result<T, Self::DeserializeError>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
mod core; | ||
pub use core::FileFormat; | ||
|
||
#[cfg(feature = "yaml")] | ||
mod yaml; | ||
#[cfg(feature = "yaml")] | ||
pub use yaml::YamlFileFormat; | ||
|
||
#[cfg(feature = "test-utils")] | ||
mod test_utils; | ||
#[cfg(feature = "test-utils")] | ||
pub use test_utils::{FileFormatTestUtils, MockFileFormatUtil}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use std::fmt::Debug; | ||
|
||
use serde::{de::DeserializeOwned, Serialize}; | ||
|
||
use crate::*; | ||
|
||
pub trait MockFileFormatUtil<F: FileFormat> { | ||
fn mock_format_display() -> String; | ||
} | ||
|
||
pub struct FileFormatTestUtils; | ||
|
||
impl FileFormatTestUtils { | ||
pub fn assert_serialization<F: FileFormat, T: MockTestUtil + MockFileFormatUtil<F> + Serialize>() { | ||
assert_eq!(T::mock_format_display(), F::serialize_to_string(&T::mock()).unwrap()) | ||
} | ||
|
||
pub fn assert_deserialization<F: FileFormat, T: MockTestUtil + MockFileFormatUtil<F> + DeserializeOwned + Debug + PartialEq>() { | ||
assert_eq!(T::mock(), F::deserialize_from_str(&T::mock_format_display()).unwrap()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use super::*; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub struct YamlFileFormat; | ||
|
||
impl FileFormat for YamlFileFormat { | ||
type Map = serde_yaml::Mapping; | ||
type SerializeError = serde_yaml::Error; | ||
type DeserializeError = serde_yaml::Error; | ||
|
||
fn serialize_to_string<T: serde::Serialize>(t: &T) -> Result<String, Self::SerializeError> { | ||
serde_yaml::to_string(t) | ||
} | ||
|
||
fn deserialize_from_str<T: serde::de::DeserializeOwned>(str: &str) -> Result<T, Self::DeserializeError> { | ||
serde_yaml::from_str(str) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.