genpac 0.1.0

Sandbox for Gentoo ebuild development using bubblewrap
// Copyright (C) 2023 Gokul Das B
// SPDX-License-Identifier: GPL-3.0-or-later
//! Backend interface
//!
//! This module defines the basic interfaces for any snapshotting backend (btrfs, plain, etc).

use crate::global::{ChrootUnverified, ChrootVerified};
use crate::GlobalsFinal;
use anyhow::Result as AResult;
use btrfs::BtrfsBackend;
use none::NoneBackend;
use serde::Deserialize;
use std::path::Path;

mod btrfs;
mod none;

// Every snapshot backend should implement this trait
pub(super) trait BackendOps {
    fn make_blank(&mut self) -> AResult<()>;
    fn duplicate(&mut self, source: ChrootVerified) -> AResult<()>;
    fn delete(&mut self) -> AResult<()>;
}

// Marker trait for specific backends
pub(super) trait BackendMarker {}

#[derive(Deserialize, Copy, Clone, Default)]
pub enum BackendTypes {
    #[default]
    #[serde(rename = "none")]
    None,

    #[serde(rename = "btrfs")]
    Btrfs,
}

impl BackendTypes {
    pub(super) fn target(&self, target: &Path, globals: &GlobalsFinal) -> Box<dyn BackendOps> {
        match self {
            Self::None => Box::new(NoneBackend::new(target, globals)),
            Self::Btrfs => Box::new(BtrfsBackend::new(target, globals)),
        }
    }
}

pub(super) struct Snapshot<T: BackendMarker> {
    target: Option<ChrootUnverified>,
    dry_run: bool,
    marker: std::marker::PhantomData<T>,
}

impl<T: BackendMarker> Snapshot<T> {
    pub(super) fn new(target: &Path, globals: &GlobalsFinal) -> Self {
        let target = Some(globals.chroot(target));
        let dry_run = globals.dry_run();
        let marker = std::marker::PhantomData;
        Self {
            target,
            dry_run,
            marker,
        }
    }
}