Skip to content
Snippets Groups Projects

WIP feat(db+indexer): certifications

Open Pascal Engélibert requested to merge tuxmain/duniter-gva:certifications into master
10 files
+ 745
6
Compare changes
  • Side-by-side
  • Inline
Files
10
+ 66
0
 
// Copyright (C) 2021 Pascal Engélibert
 
//
 
// This program is free software: you can redistribute it and/or modify
 
// it under the terms of the GNU Affero General Public License as
 
// published by the Free Software Foundation, either version 3 of the
 
// License, or (at your option) any later version.
 
//
 
// This program is distributed in the hope that it will be useful,
 
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
// GNU Affero General Public License for more details.
 
//
 
// You should have received a copy of the GNU Affero General Public License
 
// along with this program. If not, see <https://www.gnu.org/licenses/>.
 
 
use crate::*;
 
 
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
 
pub struct GvaCertIdKeyDbV1 {
 
pub issuer: U64BE,
 
pub target: U64BE,
 
}
 
 
impl Default for GvaCertIdKeyDbV1 {
 
fn default() -> Self {
 
Self {
 
issuer: U64BE(0),
 
target: U64BE(0),
 
}
 
}
 
}
 
 
impl std::fmt::Display for GvaCertIdKeyDbV1 {
 
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 
write!(f, "{}:{}", self.issuer.0, self.target.0,)
 
}
 
}
 
 
impl AsBytes for GvaCertIdKeyDbV1 {
 
fn as_bytes<T, F: FnMut(&[u8]) -> T>(&self, mut f: F) -> T {
 
f(&([self.issuer.0.to_be_bytes(), self.target.0.to_be_bytes()].concat()))
 
}
 
}
 
 
impl FromBytes for GvaCertIdKeyDbV1 {
 
type Err = CorruptedBytes;
 
 
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
 
Ok(Self {
 
issuer: U64BE::from_bytes(&bytes[0..8])
 
.map_err(|_| CorruptedBytes("db corrupted".to_owned()))?,
 
target: U64BE::from_bytes(&bytes[8..16])
 
.map_err(|_| CorruptedBytes("db corrupted".to_owned()))?,
 
})
 
}
 
}
 
 
#[cfg(feature = "explorer")]
 
impl ExplorableKey for GvaCertIdKeyDbV1 {
 
fn from_explorer_str(_: &str) -> std::result::Result<Self, FromExplorerKeyErr> {
 
unimplemented!()
 
}
 
fn to_explorer_string(&self) -> KvResult<String> {
 
Ok(self.to_string())
 
}
 
}
Loading