packium/src/pack.rs

69 lines
1.7 KiB
Rust

use crate::{
config,
modrinth::{
project::{get_multiple_projects, Project},
versions::{get_multiple_versions, ProjectVersion},
},
};
use anyhow::Result;
use std::collections::HashMap;
use std::fmt;
pub struct Modpack {
pub game_version: String,
pub loader: String,
pub mods: HashMap<String, Mod>,
}
pub struct Mod {
pub project_id: String,
pub title: String,
pub description: String,
pub version: Option<ProjectVersion>,
}
impl Mod {
fn from_project(project: Project) -> Self {
Self {
project_id: project.id,
title: project.title,
description: project.description,
version: None,
}
}
}
impl fmt::Display for Mod {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.title)
}
}
impl Modpack {
pub async fn from_config(config: &config::Config) -> Result<Self> {
let projects = get_multiple_projects(config.projects.keys().collect())
.await
.unwrap();
let versions = get_multiple_versions(config.projects.values().collect())
.await
.unwrap();
let mut projects_map: HashMap<String, Mod> = projects
.into_iter()
.map(|p| (p.id.clone(), Mod::from_project(p)))
.collect();
for version in versions {
if let Some(p) = projects_map.get_mut(&version.project_id) {
p.version = Some(version)
}
}
Ok(Modpack {
game_version: config.game_version.clone(),
loader: config.loader.clone(),
mods: projects_map,
})
}
}