Skip to content

Commit

Permalink
feat: soft version
Browse files Browse the repository at this point in the history
Signed-off-by: usamoi <[email protected]>
  • Loading branch information
usamoi committed Dec 27, 2023
1 parent 2d6b55f commit aa54b0c
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions crates/service/src/worker/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ use std::error::Error;
use std::path::Path;
use thiserror::Error;

#[repr(u64)]
enum Version {
V1 = 1,
}

const VERSION: Version = Version::V1;

#[derive(Debug, Error)]
pub enum MetadataError {
#[error("Invalid version.")]
Expand All @@ -18,22 +11,35 @@ pub enum MetadataError {

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Metadata {
#[serde(default)]
pub version: Option<u64>,
#[serde(default)]
pub soft_version: Option<u64>,
}

impl Metadata {
const VERSION: u64 = 1;
const SOFT_VERSION: u64 = 1;
}

impl Metadata {
pub fn write(path: impl AsRef<Path>) {
let metadata = Metadata {
version: Some(VERSION as u64),
version: Some(Self::VERSION),
soft_version: Some(Self::SOFT_VERSION),
};
let contents = serde_json::to_string(&metadata).unwrap();
std::fs::write(path, contents).unwrap();
}
pub fn read(path: impl AsRef<Path>) -> Result<(), Box<dyn Error>> {
use MetadataError::*;
let contents = std::fs::read_to_string(path)?;
let metadata = serde_json::from_str::<Metadata>(&contents)?;
if metadata.version != Some(VERSION as u64) {
return Err(Box::new(MetadataError::InvalidVersion));
if Self::VERSION != metadata.version.ok_or(InvalidVersion)? {
return Err(Box::new(InvalidVersion));
}
if Self::SOFT_VERSION <= metadata.soft_version.ok_or(InvalidVersion)? {
return Err(Box::new(InvalidVersion));
}
Ok(())
}
Expand Down

0 comments on commit aa54b0c

Please sign in to comment.