// Copyright © 2023 Kim Altintop <kim@eagain.io>
// SPDX-License-Identifier: GPL-2.0-only

use autosurgeon::{
    reconcile::NoKey,
    Hydrate,
    HydrateError,
    Reconcile,
    Reconciler,
};

#[derive(Clone, Debug)]
pub struct Patch {
    hash: libpijul::Hash,
}

impl Hydrate for Patch {
    fn hydrate_bytes(bytes: &[u8]) -> Result<Self, HydrateError> {
        libpijul::Hash::from_bytes(bytes).map_or_else(
            || Err(HydrateError::unexpected("invalid hash", String::new())),
            |hash| Ok(Self { hash }),
        )
    }
}

impl Reconcile for Patch {
    type Key<'a> = NoKey;

    fn reconcile<R: Reconciler>(&self, mut reconciler: R) -> Result<(), R::Error> {
        reconciler.bytes(self.hash.to_bytes())
    }
}