rttp_client/
config.rs

1//use std::{collections::HashMap, sync::Mutex};
2//
3//use once_cell::sync::Lazy;
4//
5//static DEFAULT_CONFIG: Lazy<Mutex<Config>> = Lazy::new(|| {
6//  let mut config = ;
7//  Mutex::new(config)
8//});
9
10
11#[derive(Clone, Debug)]
12pub struct Config {
13  read_timeout: u64,
14  write_timeout: u64,
15  auto_redirect: bool,
16  max_redirect: u32,
17}
18
19impl Default for Config {
20  fn default() -> Self {
21    Config::builder()
22      .read_timeout(10000)
23      .write_timeout(10000)
24      .auto_redirect(false)
25      .max_redirect(0)
26      .build()
27  }
28}
29
30impl Config {
31  pub fn builder() -> ConfigBuilder {
32    ConfigBuilder::new()
33  }
34}
35
36impl Config {
37  pub fn read_timeout(&self) -> u64 { self.read_timeout }
38  pub fn write_timeout(&self) -> u64 { self.write_timeout }
39  pub fn auto_redirect(&self) -> bool { self.auto_redirect }
40  pub fn max_redirect(&self) -> u32 { self.max_redirect }
41}
42
43
44#[derive(Clone, Debug)]
45pub struct ConfigBuilder {
46  config: Config
47}
48
49impl ConfigBuilder {
50  pub fn new() -> Self {
51    Self {
52      config: Config {
53        read_timeout: 5000,
54        write_timeout: 5000,
55        auto_redirect: false,
56        max_redirect: 3,
57      }
58    }
59  }
60
61  pub fn build(&self) -> Config {
62    self.config.clone()
63  }
64
65  pub fn read_timeout(&mut self, read_timeout: u64) -> &mut Self {
66    self.config.read_timeout = read_timeout;
67    self
68  }
69  pub fn write_timeout(&mut self, write_timeout: u64) -> &mut Self {
70    self.config.write_timeout = write_timeout;
71    self
72  }
73  pub fn auto_redirect(&mut self, auto_redirect: bool) -> &mut Self {
74    self.config.auto_redirect = auto_redirect;
75    self
76  }
77  pub fn max_redirect(&mut self, max_redirect: u32) -> &mut Self {
78    self.config.max_redirect = max_redirect;
79    self
80  }
81}
82
83impl AsRef<Config> for Config {
84  fn as_ref(&self) -> &Config {
85    self
86  }
87}
88
89impl AsRef<Config> for ConfigBuilder {
90  fn as_ref(&self) -> &Config {
91    &self.config
92  }
93}