lockfile: add pakku v0 compatibility and ProjectFile serde defaults

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I48a03feeb6bb2e640b41a85ba3e0e3296a6a6964
This commit is contained in:
raf 2026-04-21 19:26:59 +03:00
commit 3faf4d3ca8
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
2 changed files with 13 additions and 20 deletions

View file

@ -308,7 +308,7 @@ mod tests {
} }
let found = lockfile.get_project("test-id").unwrap(); let found = lockfile.get_project("test-id").unwrap();
assert_eq!(found.redistributable, false); assert!(!found.redistributable);
} }
#[test] #[test]
@ -566,8 +566,7 @@ mod tests {
/// Current lockfile version - bump this when making breaking changes /// Current lockfile version - bump this when making breaking changes
const LOCKFILE_VERSION: u32 = 2; const LOCKFILE_VERSION: u32 = 2;
/// Minimum supported lockfile version for migration
const MIN_SUPPORTED_VERSION: u32 = 1;
const LOCKFILE_NAME: &str = "pakku-lock.json"; const LOCKFILE_NAME: &str = "pakku-lock.json";
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@ -577,6 +576,7 @@ pub struct LockFile {
pub mc_versions: Vec<String>, pub mc_versions: Vec<String>,
pub loaders: HashMap<String, String>, pub loaders: HashMap<String, String>,
pub projects: Vec<Project>, pub projects: Vec<Project>,
#[serde(default)]
pub lockfile_version: u32, pub lockfile_version: u32,
} }
@ -642,7 +642,7 @@ impl LockFile {
// Check if migration is needed // Check if migration is needed
if lockfile.lockfile_version < LOCKFILE_VERSION { if lockfile.lockfile_version < LOCKFILE_VERSION {
lockfile = lockfile.migrate()?; lockfile = lockfile.migrate();
// Save migrated lockfile // Save migrated lockfile
lockfile.save_without_validation(path_ref)?; lockfile.save_without_validation(path_ref)?;
log::info!( log::info!(
@ -661,12 +661,11 @@ impl LockFile {
} }
/// Migrate lockfile from older version to current version /// Migrate lockfile from older version to current version
fn migrate(mut self) -> Result<Self> { fn migrate(mut self) -> Self {
if self.lockfile_version < MIN_SUPPORTED_VERSION { // Migration from v0 (pakku format, no explicit version) to v1
return Err(PakkerError::InvalidLockFile(format!( if self.lockfile_version == 0 {
"Lockfile version {} is too old to migrate. Minimum supported: {}", log::info!("Migrating lockfile from v0 (pakku format) to v1...");
self.lockfile_version, MIN_SUPPORTED_VERSION self.lockfile_version = 1;
)));
} }
// Migration from v1 to v2 // Migration from v1 to v2
@ -693,7 +692,7 @@ impl LockFile {
// self.lockfile_version = 3; // self.lockfile_version = 3;
// } // }
Ok(self) self
} }
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> { pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
@ -719,13 +718,6 @@ impl LockFile {
self.lockfile_version, LOCKFILE_VERSION self.lockfile_version, LOCKFILE_VERSION
))); )));
} }
if self.lockfile_version < MIN_SUPPORTED_VERSION {
return Err(PakkerError::InvalidLockFile(format!(
"Lockfile version {} is too old. Minimum supported: {}",
self.lockfile_version, MIN_SUPPORTED_VERSION
)));
}
if self.mc_versions.is_empty() { if self.mc_versions.is_empty() {
return Err(PakkerError::InvalidLockFile( return Err(PakkerError::InvalidLockFile(
"At least one Minecraft version is required".to_string(), "At least one Minecraft version is required".to_string(),

View file

@ -370,6 +370,7 @@ pub struct ProjectFile {
#[serde(rename = "type")] #[serde(rename = "type")]
pub file_type: String, pub file_type: String,
pub file_name: String, pub file_name: String,
#[serde(default)]
pub mc_versions: Vec<String>, pub mc_versions: Vec<String>,
#[serde(default)] #[serde(default)]
pub loaders: Vec<String>, pub loaders: Vec<String>,
@ -378,6 +379,7 @@ pub struct ProjectFile {
pub id: String, pub id: String,
pub parent_id: String, pub parent_id: String,
pub hashes: HashMap<String, String>, pub hashes: HashMap<String, String>,
#[serde(default)]
pub required_dependencies: Vec<String>, pub required_dependencies: Vec<String>,
pub size: u64, pub size: u64,
pub date_published: String, pub date_published: String,
@ -551,8 +553,7 @@ mod tests {
assert!( assert!(
file.is_compatible(&lockfile_mc, &lockfile_loaders), file.is_compatible(&lockfile_mc, &lockfile_loaders),
"Failed for valid loader: {}", "Failed for valid loader: {loader}"
loader
); );
} }
} }