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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
// MIT License
//
// Copyright (c) 2022-2024 Robin Doer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#[cfg(test)]
mod tests;
use nuts_backend::{Binary, IdSize};
use std::convert::TryInto;
use std::fmt;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use crate::error::{Error, Result};
#[cfg(test)]
fn rand_bytes() -> [u8; SIZE] {
[
0xdb, 0x3d, 0x05, 0x23, 0xd4, 0x50, 0x75, 0x30, 0xe8, 0x6d, 0xf9, 0x6a, 0x1b, 0x76, 0xaa,
0x0c,
]
}
#[cfg(not(test))]
fn rand_bytes() -> [u8; SIZE] {
let mut buf = [0; SIZE];
getrandom::getrandom(&mut buf).unwrap();
buf
}
const SIZE: usize = 16;
const HEX: [char; SIZE] = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
];
/// The [id](nuts_backend::Backend::Id) of the backend.
///
/// This id as an 16 byte random number.
///
/// When storing a block to disks the path to the file is derived from the id:
/// * The id is converted into a hex string.
/// * The path then would be: `<first two chars>/<next two chars>/<remaining chars>`
#[derive(Clone, PartialEq)]
pub struct Id([u8; SIZE]);
impl Binary for Id {
fn from_bytes(bytes: &[u8]) -> Option<Id> {
match bytes.try_into() {
Ok(buf) => Some(Id(buf)),
Err(_) => None,
}
}
fn as_bytes(&self) -> Vec<u8> {
self.0.to_vec()
}
}
impl IdSize for Id {
fn size() -> usize {
SIZE
}
}
impl Id {
pub(crate) fn generate() -> Id {
Id(rand_bytes())
}
pub(crate) fn min() -> Id {
Id([u8::MIN; SIZE])
}
fn as_hex(&self) -> String {
let mut target = String::with_capacity(2 * SIZE);
for b in self.0.iter() {
target.push(HEX[(*b as usize >> 4) & 0x0f]);
target.push(HEX[(*b as usize) & 0x0f]);
}
target
}
pub(crate) fn to_pathbuf(&self, parent: &Path) -> PathBuf {
let hex = self.as_hex();
let mut path = PathBuf::new();
let mut pos = 0;
path.push(parent);
for _ in 0..2 {
path.push(&hex[pos..pos + 2]);
pos += 2;
}
path.push(&hex[pos..]);
path
}
}
impl fmt::Display for Id {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str(&self.as_hex())
}
}
impl fmt::Debug for Id {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_tuple("Id").field(&self.to_string()).finish()
}
}
impl FromStr for Id {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
if s.len() != 2 * SIZE {
return Err(Error::InvalidId(s.to_string()));
}
let mut id = Id::min();
let mut iter = s.chars();
for n in id.0.iter_mut() {
let b1 = iter
.next()
.unwrap()
.to_digit(16)
.map_or_else(|| Err(Error::InvalidId(s.to_string())), |n| Ok(n as u8))?;
let b2 = iter
.next()
.unwrap()
.to_digit(16)
.map_or_else(|| Err(Error::InvalidId(s.to_string())), |n| Ok(n as u8))?;
*n = (b1 << 4) | b2;
}
Ok(id)
}
}