Skip to main content

harmos_domain/
config.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
19use serde::{Serialize, de::DeserializeOwned};
20// Local Imports
21
22/* === Constants === */
23
24/* === Macros === */
25
26/* === Type Aliases === */
27
28/* === Structs === */
29
30#[derive(Clone, Copy, Debug)]
31pub struct SchemaSettings {
32    pub preparation_bytes: std::num::NonZeroUsize,
33    pub preparation_work: std::num::NonZeroUsize,
34}
35
36#[derive(Clone, Copy, Debug)]
37pub struct StreamSettings {
38    pub items: std::num::NonZeroUsize,
39    pub bytes: std::num::NonZeroUsize,
40}
41
42/* === Enums === */
43
44#[derive(Debug, thiserror::Error)]
45pub enum ConfigError {
46    #[error("artifact configuration could not be encoded: {0}")]
47    Encode(serde_json::Error),
48
49    #[error("artifact configuration could not be decoded: {0}")]
50    Decode(serde_json::Error),
51}
52
53/* === Traits === */
54
55/* === Implementations === */
56
57impl Default for SchemaSettings {
58    fn default() -> Self {
59        Self {
60            // Complete Checkpoint Schemas Fit Below 128 KiB; Retain Headroom For Larger Catalogs.
61            preparation_bytes: std::num::NonZeroUsize::new(4 * 1024 * 1024).unwrap(),
62            preparation_work: std::num::NonZeroUsize::new(1024 * 1024).unwrap(),
63        }
64    }
65}
66
67/* === Public Functions === */
68
69pub fn decode_config<C: DeserializeOwned>(bytes: &[u8]) -> Result<C, ConfigError> {
70    serde_json::from_slice(bytes).map_err(ConfigError::Decode)
71}
72
73pub fn validate_config(bytes: &[u8]) -> Result<(), ConfigError> {
74    decode_config::<serde::de::IgnoredAny>(bytes).map(|_| ())
75}
76
77pub fn encode_config<C: Serialize>(config: &C) -> Result<Vec<u8>, ConfigError> {
78    serde_json::to_vec(config).map_err(ConfigError::Encode)
79}
80
81/* === Private Functions === */
82
83/* === Tests === */