Skip to main content

harmos_domain/operations/common/
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 { actual: bytes.len(), maximum: MAX_CACHE_KEY_BYTES });
52        }
53
54        Ok(Self(bytes))
55    }
56
57    pub fn as_bytes(&self) -> &[u8] {
58        &self.0
59    }
60}
61
62/* === Public Functions === */
63
64/* === Private Functions === */
65
66/* === Tests === */