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
use blake3::Hash;
use serde::{Deserialize, Serialize};

use std::{
    fmt::{Debug, Display},
    fs::File,
    io::Read,
    path::Path,
};
use serde::{Deserializer, Serializer};

#[cfg(test)]
mod tests;
mod convert;

/// 256 位对象 ID
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct ObjectID {
    hash256: Hash,
}

impl Ord for ObjectID {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.hash256.as_bytes().cmp(other.hash256.as_bytes())
    }
}

impl PartialOrd for ObjectID {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.hash256.as_bytes().partial_cmp(other.hash256.as_bytes())
    }
}

impl Display for ObjectID {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for byte in self.hash256.as_bytes() {
            write!(f, "{:02x}", byte)?;
        }
        Ok(())
    }
}

impl Debug for ObjectID {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(self, f)
    }
}