libplasmoid-updater 0.2.0

Library for updating KDE Plasma 6 components from the KDE Store. Meant for use in topgrade.
Documentation
// SPDX-License-Identifier: GPL-3.0-or-later
//
// KNewStuff registry format based on KDE Discover (https://invent.kde.org/plasma/discover) -
// GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL

use std::path::{Path, PathBuf};

/// Extracts the component directory or file name from an installed path.
/// For paths ending with metadata.json: returns parent directory name.
/// For other files or directories: returns the last path component.
pub(super) fn extract_directory_name(path: &Path) -> Option<String> {
    let name = path.file_name().and_then(|n| n.to_str())?;

    if name == "metadata.json" || name == "metadata.desktop" {
        return path
            .parent()
            .and_then(|p| p.file_name())
            .and_then(|n| n.to_str())
            .map(|s| s.to_string());
    }

    Some(name.to_string())
}

/// Resolves the installed_path from a registry entry to the actual component path.
/// KNewStuff stores metadata.json/metadata.desktop file paths; this returns the parent
/// directory for those, so InstalledComponent.path always points to the component root.
pub(super) fn resolve_component_path(path: PathBuf) -> PathBuf {
    let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
    if (name == "metadata.json" || name == "metadata.desktop")
        && let Some(parent) = path.parent()
    {
        return parent.to_path_buf();
    }
    path
}

/// Determines the correct registry path for the installedfile element.
/// For directories: appends /metadata.json.
/// For files: returns the file path as-is.
pub(super) fn registry_installed_file_path(installed_path: &Path) -> String {
    if installed_path.is_file() {
        installed_path.to_string_lossy().to_string()
    } else {
        format!("{}/metadata.json", installed_path.to_string_lossy())
    }
}

/// Extracts date part from ISO timestamp.
pub(super) fn extract_date_from_iso(iso: &str) -> String {
    iso.split('T').next().unwrap_or(iso).to_string()
}