1use crate::error::Result;
5use once_cell::sync::OnceCell;
6use serde::{Deserialize, Serialize};
7use tokio::fs;
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct TestTypeConfig {
11 pub parallel: u16,
12 pub attempts: u16,
13}
14
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct Config {
17 pub timeout_scaling: f32,
18 pub loglevel: String,
19 pub cluster: TestTypeConfig,
20 pub user: TestTypeConfig,
21}
22
23impl Default for Config {
24 fn default() -> Self {
25 Config {
26 timeout_scaling: 1.0,
27 loglevel: "info".to_string(),
28 cluster: TestTypeConfig {
29 parallel: 1,
30 attempts: 1,
31 },
32 user: TestTypeConfig {
33 parallel: 4,
34 attempts: 2,
35 },
36 }
37 }
38}
39
40static CONFIG: OnceCell<Config> = OnceCell::new();
41
42impl Config {
43 pub async fn new(filename: Option<String>) -> Result<Self> {
44 if let Some(path) = filename {
45 Ok(serde_yaml::from_str(&fs::read_to_string(&path).await?)?)
46 } else {
47 Ok(Config::default())
48 }
49 }
50
51 pub fn with_timeout_scaling(self, timeout_scaling: Option<f32>) -> Self {
52 if let Some(timeout_scaling) = timeout_scaling {
53 Config{
54 timeout_scaling,
55 ..self
56 }
57 } else {
58 self
59 }
60 }
61
62 pub fn with_user_parallel(self, parallel: Option<u16>) -> Self {
63 if let Some(parallel) = parallel {
64 Config{
65 user: TestTypeConfig{
66 parallel,
67 ..self.user
68 },
69 ..self
70 }
71 } else {
72 self
73 }
74 }
75
76 pub fn with_cluster_parallel(self, parallel: Option<u16>) -> Self {
77 if let Some(parallel) = parallel {
78 Config{
79 cluster: TestTypeConfig{
80 parallel,
81 ..self.cluster
82 },
83 ..self
84 }
85 } else {
86 self
87 }
88 }
89
90 pub fn with_user_attempts(self, attempts: Option<u16>) -> Self {
91 if let Some(attempts) = attempts {
92 Config{
93 user: TestTypeConfig{
94 attempts,
95 ..self.user
96 },
97 ..self
98 }
99 } else {
100 self
101 }
102 }
103
104 pub fn with_cluster_attempts(self, attempts: Option<u16>) -> Self {
105 if let Some(attempts) = attempts {
106 Config{
107 cluster: TestTypeConfig{
108 attempts,
109 ..self.cluster
110 },
111 ..self
112 }
113 } else {
114 self
115 }
116 }
117
118 pub fn init(config: Config) {
119 CONFIG.set(config).unwrap();
120 }
121
122 pub fn get() -> &'static Self {
123 CONFIG.get().unwrap()
124 }
125}