Files
vibetorrent/backend/src/diff.rs
spinline fddc81365b
All checks were successful
Build MIPS Binary / build (push) Successful in 5m20s
feat: complete modernization with shadcn, stateless auth, and performance optimizations
2026-02-10 22:16:36 +03:00

69 lines
2.8 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use std::collections::HashMap;
use shared::{AppEvent, NotificationLevel, SystemNotification, Torrent, TorrentUpdate};
#[derive(Debug)]
pub enum DiffResult {
NoChange,
FullUpdate,
Partial(Vec<AppEvent>),
}
pub fn diff_torrents(old: &[Torrent], new: &[Torrent]) -> DiffResult {
if old.len() != new.len() {
return DiffResult::FullUpdate;
}
let old_map: HashMap<&str, &Torrent> = old.iter().map(|t| (t.hash.as_str(), t)).collect();
for new_t in new {
if !old_map.contains_key(new_t.hash.as_str()) {
return DiffResult::FullUpdate;
}
}
let mut events = Vec::new();
for new_t in new {
let old_t = old_map.get(new_t.hash.as_str()).unwrap();
// Manuel diff creating TorrentUpdate (which is the Patch struct)
let mut patch = TorrentUpdate::default();
let mut has_changes = false;
if old_t.name != new_t.name { patch.name = Some(new_t.name.clone()); has_changes = true; }
if old_t.size != new_t.size { patch.size = Some(new_t.size); has_changes = true; }
if old_t.down_rate != new_t.down_rate { patch.down_rate = Some(new_t.down_rate); has_changes = true; }
if old_t.up_rate != new_t.up_rate { patch.up_rate = Some(new_t.up_rate); has_changes = true; }
if old_t.completed != new_t.completed { patch.completed = Some(new_t.completed); has_changes = true; }
if old_t.eta != new_t.eta { patch.eta = Some(new_t.eta); has_changes = true; }
if (old_t.percent_complete - new_t.percent_complete).abs() > 0.01 {
patch.percent_complete = Some(new_t.percent_complete);
has_changes = true;
if old_t.percent_complete < 100.0 && new_t.percent_complete >= 100.0 {
events.push(AppEvent::Notification(SystemNotification {
level: NotificationLevel::Success,
message: format!("Torrent tamamlandı: {}", new_t.name),
}));
}
}
if old_t.status != new_t.status { patch.status = Some(new_t.status.clone()); has_changes = true; }
if old_t.error_message != new_t.error_message { patch.error_message = Some(new_t.error_message.clone()); has_changes = true; }
if old_t.label != new_t.label { patch.label = Some(new_t.label.clone()); has_changes = true; }
if has_changes {
// Set the hash (not an Option in Patch usually, but check shared/src/lib.rs)
// Wait, TorrentUpdate is a Patch, does it have 'hash' field?
// Yes, because Torrent has 'hash' field.
patch.hash = Some(new_t.hash.clone());
events.push(AppEvent::Update(patch));
}
}
if events.is_empty() {
DiffResult::NoChange
} else {
tracing::debug!("Generated {} partial updates", events.len());
DiffResult::Partial(events)
}
}