ud 0.1.0

UpDate! — A fast, universal dependency updater.
Documentation
use crate::core::contract::Ecosystem;
use crate::core::model::{Report, Verdict};
use owo_colors::OwoColorize;

pub struct HumanReporter;

impl HumanReporter {
    pub fn render(
        report: &Report,
        is_tree: bool,
        should_update: bool,
        ecosystems: &[Ecosystem],
    ) -> String {
        let mut buf = String::new();
        for manifest in &report.manifests {
            // Find the ecosystem for this manifest
            let eco = ecosystems.iter().find(|e| e.name == manifest.ecosystem);
            let scheme = eco.map(|e| &*e.scheme);

            // Print path (ecosystem) header
            buf.push_str(&format!(
                "{} ({})\n",
                manifest.path.display().bold(),
                manifest.ecosystem.bright_black()
            ));

            for (dep, verdict) in &manifest.verdicts {
                let coord_name = if dep.coordinate.0.is_empty() {
                    "(manifest)"
                } else {
                    &dep.coordinate.0
                };

                match verdict {
                    Verdict::Current { latest, latest_pre } => {
                        if is_tree {
                            let version_str = latest.0.bright_black().to_string();
                            let pre_suffix = if let Some(pre) = latest_pre {
                                format!(" ({})", pre.0)
                                    .magenta()
                                    .dimmed()
                                    .italic()
                                    .to_string()
                            } else {
                                String::new()
                            };
                            buf.push_str(&format!(
                                "  {} {}{}\n",
                                coord_name, version_str, pre_suffix
                            ));
                        }
                    }
                    Verdict::Outdated {
                        compatible,
                        latest,
                        latest_pre,
                    } => {
                        let effective_target = compatible.as_ref().unwrap_or(latest);
                        let old_str = format_old_constraint(&dep.constraint.0, &effective_target.0);

                        let version_start = dep
                            .constraint
                            .0
                            .find(|c: char| c.is_ascii_digit())
                            .unwrap_or(0);
                        let operator = &dep.constraint.0[..version_start];

                        let target_is_pre = match scheme {
                            Some(s) => s.is_prerelease(effective_target),
                            None => is_prerelease(&effective_target.0),
                        };

                        let target_ver_str = if target_is_pre {
                            effective_target.0.magenta().to_string()
                        } else {
                            effective_target.0.green().to_string()
                        };

                        let target_str = format!("{}{}", operator.cyan(), target_ver_str);

                        let breaking_suffix = match compatible {
                            Some(compat) if compat.0 != latest.0 => {
                                format!(" (latest: {})", latest.0)
                                    .bright_black()
                                    .dimmed()
                                    .italic()
                                    .to_string()
                            }
                            None => format!(" (breaking: {})", latest.0)
                                .yellow()
                                .dimmed()
                                .italic()
                                .to_string(),
                            _ => String::new(),
                        };

                        let pre_suffix = if let Some(pre) = latest_pre {
                            format!(" (pre: {})", pre.0)
                                .magenta()
                                .dimmed()
                                .italic()
                                .to_string()
                        } else {
                            String::new()
                        };

                        buf.push_str(&format!(
                            "  {} {} {} {}{}{}\n",
                            coord_name,
                            old_str,
                            "→".bright_black(),
                            target_str,
                            breaking_suffix,
                            pre_suffix,
                        ));

                        if should_update {
                            let can_rewrite = scheme.map_or(true, |s| {
                                s.rewrite(&dep.constraint, effective_target).is_some()
                            });
                            if can_rewrite {
                                buf.push_str(&format!("    {}\n", "Updated!".green().italic()));
                            } else {
                                buf.push_str(&format!(
                                    "    {}\n",
                                    "skipped: complex range".yellow().italic()
                                ));
                            }
                        }
                    }
                    Verdict::Yanked => {
                        buf.push_str(&format!("  {} {}\n", coord_name, "! yanked".red().bold()));
                    }
                    Verdict::Unsatisfiable {
                        constraint,
                        prerelease_available,
                    } => {
                        let hint = if *prerelease_available {
                            format!(" (only prereleases available — retry with --pre)")
                                .dimmed()
                                .to_string()
                        } else {
                            String::new()
                        };
                        buf.push_str(&format!(
                            "  {} {} {}{}\n",
                            coord_name,
                            constraint.0.bright_black(),
                            "✗ no versions found".red(),
                            hint
                        ));
                    }
                    Verdict::Errored(e) => {
                        buf.push_str(&format!("  {} {} {}\n", coord_name, "✗ error:".red(), e));
                    }
                }
            }
        }
        buf
    }
}

fn format_old_constraint(constraint: &str, target: &str) -> String {
    let version_start = constraint.find(|c: char| c.is_ascii_digit()).unwrap_or(0);
    let prefix_symbols = &constraint[..version_start];
    let version_part = &constraint[version_start..];

    let target_version_start = target.find(|c: char| c.is_ascii_digit()).unwrap_or(0);
    let target_version_part = &target[target_version_start..];

    let mut common_len = 0;
    for (c1, c2) in version_part.chars().zip(target_version_part.chars()) {
        if c1 == c2 {
            common_len += c1.len_utf8();
        } else {
            break;
        }
    }

    let (same_part, diff_part) = version_part.split_at(common_len);

    format!(
        "{}{}{}",
        prefix_symbols.cyan(),
        same_part.bright_black(),
        diff_part.yellow(),
    )
}

pub struct JsonReporter;

impl JsonReporter {
    pub fn render(report: &Report) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(report)
    }
}

fn is_prerelease(version: &str) -> bool {
    version.contains('-')
}