From ec627458d246210024755cf08f4c27886af549e3 Mon Sep 17 00:00:00 2001 From: naskya Date: Fri, 21 Jun 2024 10:36:54 +0900 Subject: [PATCH] ensure that revision is latest in validation --- src/config/client.rs | 1 + src/config/mod.rs | 8 ++++++++ src/config/server.rs | 1 + 3 files changed, 10 insertions(+) diff --git a/src/config/client.rs b/src/config/client.rs index 23f98f7..ad8426d 100644 --- a/src/config/client.rs +++ b/src/config/client.rs @@ -11,6 +11,7 @@ use validator::{Validate, ValidationError}; #[derive(Deserialize, Serialize, Validate, Debug)] pub struct Config { + #[validate(custom(function = "super::ensure_latest_revision"))] pub config_revision: Revision, #[validate(nested)] pub theme: Option, diff --git a/src/config/mod.rs b/src/config/mod.rs index 065dd60..c9a4a73 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -4,6 +4,7 @@ pub mod server; use clap::ValueEnum; use enum_iterator::Sequence; use serde_repr::{Deserialize_repr, Serialize_repr}; +use validator::ValidationError; pub const SERVER_CONFIG_PATH: &str = "config/server.toml"; pub const CLIENT_CONFIG_PATH: &str = "config/client.toml"; @@ -19,3 +20,10 @@ pub enum Revision { /// The first revision number for `config/{server,client}.toml` V1, } + +fn ensure_latest_revision(revision: &Revision) -> Result<(), ValidationError> { + match revision.next() { + Some(_) => Err(ValidationError::new("not the latest revision")), + None => Ok(()), + } +} diff --git a/src/config/server.rs b/src/config/server.rs index b4b4d59..b180560 100644 --- a/src/config/server.rs +++ b/src/config/server.rs @@ -11,6 +11,7 @@ use validator::Validate; #[derive(Deserialize, Serialize, Validate, Debug)] pub struct Config { + #[validate(custom(function = "super::ensure_latest_revision"))] pub config_revision: Revision, #[validate(nested)] pub info: Option,