Skip to content

Commit

Permalink
move schema version type to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
webern authored and ecpullen committed Dec 6, 2023
1 parent 48a9414 commit 93b426d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 64 deletions.
1 change: 1 addition & 0 deletions twoliter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod cmd;
mod common;
mod docker;
mod project;
mod schema_version;
mod tools;

/// Test code that should only be compiled when running tests.
Expand Down
66 changes: 2 additions & 64 deletions twoliter/src/project.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::docker::ImageArchUri;
use crate::schema_version::SchemaVersion;
use anyhow::{ensure, Context, Result};
use async_recursion::async_recursion;
use log::{debug, trace};
use non_empty_string::NonEmptyString;
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha512};
use std::fmt;
use std::path::{Path, PathBuf};
use tokio::fs;

Expand Down Expand Up @@ -164,67 +163,6 @@ impl ImageName {
}
}

/// We need to constrain the `Project` struct to a valid version. Unfortunately `serde` does not
/// have an after-deserialization validation hook, so we have this struct to limit the version to a
/// single acceptable value.
#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub(crate) struct SchemaVersion<const N: u32>;

impl<const N: u32> SchemaVersion<N> {
pub(crate) fn get(&self) -> u32 {
N
}

pub(crate) fn get_static() -> u32 {
N
}
}

impl<const N: u32> From<SchemaVersion<N>> for u32 {
fn from(value: SchemaVersion<N>) -> Self {
value.get()
}
}

impl<const N: u32> fmt::Debug for SchemaVersion<N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
fmt::Debug::fmt(&self.get(), f)
}
}

impl<const N: u32> fmt::Display for SchemaVersion<N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
fmt::Display::fmt(&self.get(), f)
}
}

impl<const N: u32> Serialize for SchemaVersion<N> {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u32(self.get())
}
}

impl<'de, const N: u32> Deserialize<'de> for SchemaVersion<N> {
fn deserialize<D>(deserializer: D) -> Result<SchemaVersion<N>, D::Error>
where
D: Deserializer<'de>,
{
let value: u32 = Deserialize::deserialize(deserializer)?;
if value != Self::get_static() {
Err(Error::custom(format!(
"Incorrect project schema_version: got '{}', expected '{}'",
value,
Self::get_static()
)))
} else {
Ok(Self)
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
64 changes: 64 additions & 0 deletions twoliter/src/schema_version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;

/// We need to constrain the `Project` struct to a valid version. Unfortunately `serde` does not
/// have an after-deserialization validation hook, so we have this struct to limit the version to a
/// single acceptable value.
#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub(crate) struct SchemaVersion<const N: u32>;

impl<const N: u32> SchemaVersion<N> {
pub(crate) fn get(&self) -> u32 {
N
}

pub(crate) fn get_static() -> u32 {
N
}
}

impl<const N: u32> From<SchemaVersion<N>> for u32 {
fn from(value: SchemaVersion<N>) -> Self {
value.get()
}
}

impl<const N: u32> fmt::Debug for SchemaVersion<N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
fmt::Debug::fmt(&self.get(), f)
}
}

impl<const N: u32> fmt::Display for SchemaVersion<N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
fmt::Display::fmt(&self.get(), f)
}
}

impl<const N: u32> Serialize for SchemaVersion<N> {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u32(self.get())
}
}

impl<'de, const N: u32> Deserialize<'de> for SchemaVersion<N> {
fn deserialize<D>(deserializer: D) -> Result<SchemaVersion<N>, D::Error>
where
D: Deserializer<'de>,
{
let value: u32 = Deserialize::deserialize(deserializer)?;
if value != Self::get_static() {
Err(Error::custom(format!(
"Incorrect project schema_version: got '{}', expected '{}'",
value,
Self::get_static()
)))
} else {
Ok(Self)
}
}
}

0 comments on commit 93b426d

Please sign in to comment.