blackjack/
config.rs

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
// Copyright 2024 Ole Kliemann
// SPDX-License-Identifier: Apache-2.0

use crate::error::Result;
use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};
use tokio::fs;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestTypeConfig {
    pub parallel: u16,
    pub attempts: u16,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
    pub timeout_scaling: f32,
    pub loglevel: String,
    pub cluster: TestTypeConfig,
    pub user: TestTypeConfig,
}

impl Default for Config {
    fn default() -> Self {
        Config {
            timeout_scaling: 1.0,
            loglevel: "info".to_string(),
            cluster: TestTypeConfig {
                parallel: 1,
                attempts: 1,
            },
            user: TestTypeConfig {
                parallel: 4,
                attempts: 2,
            },
        }
    }
}

static CONFIG: OnceCell<Config> = OnceCell::new();

impl Config {
    pub async fn new(filename: Option<String>) -> Result<Self> {
        if let Some(path) = filename {
            Ok(serde_yaml::from_str(&fs::read_to_string(&path).await?)?)
        } else {
            Ok(Config::default())
        }
    }

    pub fn with_timeout_scaling(self, timeout_scaling: Option<f32>) -> Self {
        if let Some(timeout_scaling) = timeout_scaling {
            Config{
                timeout_scaling,
                ..self
            }
        } else {
            self
        }
    }

    pub fn with_user_parallel(self, parallel: Option<u16>) -> Self {
        if let Some(parallel) = parallel {
            Config{
                user: TestTypeConfig{
                    parallel,
                    ..self.user
                },
                ..self
            }
        } else {
            self
        }
    }

    pub fn with_cluster_parallel(self, parallel: Option<u16>) -> Self {
        if let Some(parallel) = parallel {
            Config{
                cluster: TestTypeConfig{
                    parallel,
                    ..self.user
                },
                ..self
            }
        } else {
            self
        }
    }

    pub fn with_user_attempts(self, attempts: Option<u16>) -> Self {
        if let Some(attempts) = attempts {
            Config{
                user: TestTypeConfig{
                    attempts,
                    ..self.user
                },
                ..self
            }
        } else {
            self
        }
    }

    pub fn with_cluster_attempts(self, attempts: Option<u16>) -> Self {
        if let Some(attempts) = attempts {
            Config{
                cluster: TestTypeConfig{
                    attempts,
                    ..self.user
                },
                ..self
            }
        } else {
            self
        }
    }

    pub fn init(config: Config) {
        CONFIG.set(config).unwrap();
    }

    pub fn get() -> &'static Self {
        CONFIG.get().unwrap()
    }
}