aboutsummaryrefslogtreecommitdiffstats
path: root/zone_zfs/src/snapshot.rs
blob: 84db4e3969d2a80503f9b84d73b28b5c0f1266d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use crate::{Error, FileSystem, Result};
use std::{ffi::OsString, path::PathBuf};

#[cfg(feature = "chrono")]
use chrono::{DateTime, Utc, MIN_DATETIME};

#[derive(Debug)]
pub struct Snapshot {
    name: OsString,
    dataset: PathBuf,

    #[cfg(feature = "chrono")]
    timestamp: DateTime<Utc>,
}

impl TryFrom<&str> for Snapshot {
    type Error = Error;

    fn try_from(s: &str) -> Result<Self> {
        s.split_once('@')
            .map(|(d, n)| Snapshot {
                name: n.into(),
                dataset: d.into(),

                #[cfg(feature = "chrono")]
                timestamp: n.parse().unwrap_or(MIN_DATETIME),
            })
            .ok_or_else(|| Error::Snapshot(format!("Failed to parse snapshot: {:?}", s)))
    }
}

impl Snapshot {
    pub fn clone_new(&self, path: String) -> Result<FileSystem> {
        FileSystem::builder()
            .dataset(&self.dataset.join(path))
            .build_from(self.to_owned())
    }

    /// Get a reference to the snapshot's name.
    pub fn name(&self) -> &OsString {
        &self.name
    }

    /// Get a reference to the snapshot's dataset.
    pub fn dataset(&self) -> &PathBuf {
        &self.dataset
    }

    #[cfg(feature = "chrono")]
    /// Get the snapshot's timestamp.
    pub fn timestamp(&self) -> DateTime<Utc> {
        self.timestamp
    }
}