Skip to main content

harmos_domain/operations/
key.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10/* === Modules === */
11
12/* === Re-Exports === */
13
14/* === Imports === */
15
16// Standard Library Imports
17
18// Third Party Imports
19
20// Local Imports
21
22/* === Constants === */
23
24pub const MAX_CACHE_KEY_BYTES: usize = 256;
25
26/* === Macros === */
27
28/* === Type Aliases === */
29
30/* === Structs === */
31
32#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
33pub struct CacheKey(Box<[u8]>);
34
35/* === Enums === */
36
37#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
38pub enum CacheKeyError {
39    #[error("cache key has {actual} bytes; maximum is {maximum}")]
40    TooLarge { actual: usize, maximum: usize },
41}
42
43/* === Traits === */
44
45/* === Implementations === */
46
47impl CacheKey {
48    pub fn new(bytes: impl Into<Box<[u8]>>) -> Result<Self, CacheKeyError> {
49        let bytes = bytes.into();
50        if bytes.len() > MAX_CACHE_KEY_BYTES {
51            return Err(CacheKeyError::TooLarge {
52                actual: bytes.len(),
53                maximum: MAX_CACHE_KEY_BYTES,
54            });
55        }
56
57        Ok(Self(bytes))
58    }
59
60    pub fn as_bytes(&self) -> &[u8] {
61        &self.0
62    }
63}
64
65/* === Public Functions === */
66
67/* === Private Functions === */
68
69/* === Tests === */