ud 0.1.0

UpDate! — A fast, universal dependency updater.
Documentation
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

crate::string_newtypes! { Coordinate, Constraint, Version, RegistryId }

/// Span — the knowledge of where each dependency physically sits in the manifest.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Span {
    pub start: usize,
    pub end: usize,
}

/// Dependency — one declared requirement.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Dependency {
    pub coordinate: Coordinate,
    pub constraint: Constraint,
    pub section: String, // "dependencies" | "imports" | "target.<t>.dependencies" | ...
    pub source: Option<RegistryId>, // syntax-declared registry; None ⇒ fan-out
    pub span: Option<Span>, // byte range of the REWRITABLE constraint text in the raw manifest
}

/// VersionMeta — a single version with its associated metadata.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct VersionMeta {
    pub version: Version,
    pub yanked: bool,
}

/// Availability — what a source returns for a coordinate.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Availability {
    pub versions: Vec<VersionMeta>,
}

/// Edit carries the target replacement constraint.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Edit<'a> {
    pub dep: &'a Dependency,
    pub replacement: Constraint,
}

/// Verdict — the core's judgment per dependency.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Verdict {
    Current {
        latest: Version,
        latest_pre: Option<Version>,
    },
    Outdated {
        /// The latest version that still satisfies the declared constraint (compatible bump).
        compatible: Option<Version>,
        /// The absolute latest stable version (may be a breaking jump).
        latest: Version,
        /// The latest prerelease version (if newer than `latest`).
        latest_pre: Option<Version>,
    },
    Yanked,
    Unsatisfiable {
        constraint: Constraint,
        #[serde(default)]
        prerelease_available: bool,
    },
    Errored(String),
}

/// ManifestReport — grouped report per manifest.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ManifestReport {
    pub path: PathBuf,
    pub ecosystem: String,                    // "rust" | "web"
    pub verdicts: Vec<(Dependency, Verdict)>, // manifest read order preserved
}

/// Report — the collected verdicts grouped by manifest.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Report {
    pub manifests: Vec<ManifestReport>,
}

impl Default for Report {
    fn default() -> Self {
        Self::new()
    }
}

impl Report {
    pub fn new() -> Self {
        Self {
            manifests: Vec::new(),
        }
    }

    pub fn push(&mut self, manifest_report: ManifestReport) {
        self.manifests.push(manifest_report);
    }
}

/// UdError is the central error type of the application.
#[derive(Debug)]
pub enum UdError {
    UnknownRegistry { id: RegistryId, ecosystem: String },
    UnsupportedManifest(PathBuf),
    NoManifestFound(PathBuf),
    Io(std::io::Error),
    Http(String),
    Parse(String),
    Message(String),
}

impl std::fmt::Display for UdError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::UnknownRegistry { id, ecosystem } => {
                write!(f, "Unknown registry '{}' in ecosystem '{}'", id, ecosystem)
            }
            Self::UnsupportedManifest(path) => {
                write!(f, "Unsupported manifest format: {}", path.display())
            }
            Self::NoManifestFound(path) => {
                write!(
                    f,
                    "No supported manifest found in directory: {}",
                    path.display()
                )
            }
            Self::Io(err) => write!(f, "IO error: {}", err),
            Self::Http(msg) => write!(f, "HTTP/network error: {}", msg),
            Self::Parse(msg) => write!(f, "Parse error: {}", msg),
            Self::Message(msg) => write!(f, "{}", msg),
        }
    }
}

impl std::error::Error for UdError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io(err) => Some(err),
            _ => None,
        }
    }
}

impl From<std::io::Error> for UdError {
    fn from(err: std::io::Error) -> Self {
        Self::Io(err)
    }
}