use crate::error::Result;
use std::collections::HashMap;
use std::path::Path;
#[derive(Debug, Clone, Copy, PartialEq)]
#[allow(dead_code)]
pub enum UpdateType {
VersionsWithContext,
Libraries,
Plugins,
Targeted,
Check,
}
#[derive(Debug, Clone)]
pub struct UpdateContext<'a> {
pub catalog_path: &'a Path,
pub _update_type: UpdateType,
pub _stable_only: bool,
pub _interactive: bool,
}
impl<'a> UpdateContext<'a> {
pub fn new(
catalog_path: &'a Path,
update_type: UpdateType,
stable_only: bool,
interactive: bool,
) -> Self {
Self {
catalog_path,
_update_type: update_type,
_stable_only: stable_only,
_interactive: interactive,
}
}
pub fn load_document(&self) -> Result<toml_edit::DocumentMut> {
let content = std::fs::read_to_string(self.catalog_path).map_err(|e| {
crate::error::GvcError::TomlParsing(format!("Failed to read catalog: {}", e))
})?;
content.parse::<toml_edit::DocumentMut>().map_err(|e| {
crate::error::GvcError::TomlParsing(format!("Failed to parse TOML: {}", e))
})
}
pub fn save_document(&self, doc: &toml_edit::DocumentMut) -> Result<()> {
std::fs::write(self.catalog_path, doc.to_string()).map_err(|e| {
crate::error::GvcError::TomlParsing(format!("Failed to write catalog: {}", e))
})
}
}
#[derive(Debug, Clone, Default)]
pub struct UpdateReport {
pub version_updates: HashMap<String, (String, String)>,
pub library_updates: HashMap<String, (String, String)>,
pub plugin_updates: HashMap<String, (String, String)>,
}
impl UpdateReport {
pub fn new() -> Self {
Self {
version_updates: HashMap::new(),
library_updates: HashMap::new(),
plugin_updates: HashMap::new(),
}
}
pub fn add_version_update(&mut self, name: String, old_version: String, new_version: String) {
self.version_updates
.insert(name, (old_version, new_version));
}
pub fn add_library_update(&mut self, name: String, old: String, new: String) {
self.library_updates.insert(name, (old, new));
}
pub fn add_plugin_update(&mut self, name: String, old: String, new: String) {
self.plugin_updates.insert(name, (old, new));
}
pub fn is_empty(&self) -> bool {
self.version_updates.is_empty()
&& self.library_updates.is_empty()
&& self.plugin_updates.is_empty()
}
pub fn total_updates(&self) -> usize {
self.version_updates.len() + self.library_updates.len() + self.plugin_updates.len()
}
}