cull_gmail/
client_config.rs1use std::{fs, path::PathBuf};
2
3use config::Config;
4use google_gmail1::yup_oauth2::{ApplicationSecret, ConsoleApplicationSecret};
5
6use crate::Result;
7
8mod config_root;
9
10use config_root::ConfigRoot;
11
12#[derive(Debug)]
14pub struct ClientConfig {
15 secret: ApplicationSecret,
16 config_root: ConfigRoot,
17 persist_path: String,
18}
19
20impl ClientConfig {
21 pub fn builder() -> ConfigBuilder {
23 ConfigBuilder::default()
24 }
25
26 pub fn new_from_configuration(configs: Config) -> Result<Self> {
28 let root = configs.get_string("config_root")?;
29 let config_root = ConfigRoot::parse(&root);
30
31 let secret = if let Ok(client_id) = configs.get_string("client_id")
32 && let Ok(client_secret) = configs.get_string("client_secret")
33 && let Ok(token_uri) = configs.get_string("token_uri")
34 && let Ok(auth_uri) = configs.get_string("auth_uri")
35 {
36 ApplicationSecret {
37 client_id,
38 client_secret,
39 token_uri,
40 auth_uri,
41 project_id: None,
42 redirect_uris: Vec::new(),
43 client_email: None,
44 auth_provider_x509_cert_url: None,
45 client_x509_cert_url: None,
46 }
47 } else {
48 let credential_file = configs.get_string("credential_file")?;
49 log::info!("root: {config_root}");
50 let path = config_root.full_path().join(credential_file);
51 log::info!("path: {}", path.display());
52 let json_str = fs::read_to_string(path).expect("could not read path");
53
54 let console: ConsoleApplicationSecret =
55 serde_json::from_str(&json_str).expect("could not convert to struct");
56
57 console.installed.unwrap()
58 };
59
60 let persist_path = format!("{}/gmail1", config_root.full_path().display());
61
62 Ok(ClientConfig {
63 config_root,
64 secret,
65 persist_path,
66 })
67 }
68
69 pub fn secret(&self) -> &ApplicationSecret {
71 &self.secret
72 }
73
74 pub fn persist_path(&self) -> &str {
76 &self.persist_path
77 }
78
79 pub fn config_root(&self) -> &ConfigRoot {
81 &self.config_root
82 }
83
84 pub fn full_path(&self) -> String {
86 self.config_root.full_path().display().to_string()
87 }
88}
89
90#[derive(Debug)]
91pub struct ConfigBuilder {
92 secret: ApplicationSecret,
93 config_root: ConfigRoot,
94}
95
96impl Default for ConfigBuilder {
97 fn default() -> Self {
98 let secret = ApplicationSecret {
99 auth_uri: "https;://accounts.google.com/o/oauth2/auth".to_string(),
100 token_uri: "https://oauth2.googleapis.com/token".to_string(),
101 ..Default::default()
102 };
103
104 Self {
105 secret,
106 config_root: Default::default(),
107 }
108 }
109}
110
111impl ConfigBuilder {
112 pub fn with_config_base(&mut self, value: &config_root::RootBase) -> &mut Self {
113 self.config_root.set_root_base(value);
114 self
115 }
116
117 pub fn with_config_path(&mut self, value: &str) -> &mut Self {
118 self.config_root.set_path(value);
119 self
120 }
121
122 pub fn with_credential_file(&mut self, credential_file: &str) -> &mut Self {
123 let path = PathBuf::from(self.config_root.to_string()).join(credential_file);
124 log::info!("path: {}", path.display());
125 let json_str = fs::read_to_string(path).expect("could not read path");
126
127 let console: ConsoleApplicationSecret =
128 serde_json::from_str(&json_str).expect("could not convert to struct");
129
130 self.secret = console.installed.unwrap();
131 self
132 }
133
134 pub fn with_client_id(&mut self, value: &str) -> &mut Self {
135 self.secret.client_id = value.to_string();
136 self
137 }
138
139 pub fn with_client_secret(&mut self, value: &str) -> &mut Self {
140 self.secret.client_secret = value.to_string();
141 self
142 }
143
144 pub fn with_token_uri(&mut self, value: &str) -> &mut Self {
145 self.secret.token_uri = value.to_string();
146 self
147 }
148
149 pub fn with_auth_uri(&mut self, value: &str) -> &mut Self {
150 self.secret.auth_uri = value.to_string();
151 self
152 }
153
154 pub fn add_redirect_uri(&mut self, value: &str) -> &mut Self {
155 self.secret.redirect_uris.push(value.to_string());
156 self
157 }
158
159 pub fn with_project_id(&mut self, value: &str) -> &mut Self {
160 self.secret.project_id = Some(value.to_string());
161 self
162 }
163
164 pub fn with_client_email(&mut self, value: &str) -> &mut Self {
165 self.secret.client_email = Some(value.to_string());
166 self
167 }
168 pub fn with_auth_provider_x509_cert_url(&mut self, value: &str) -> &mut Self {
169 self.secret.auth_provider_x509_cert_url = Some(value.to_string());
170 self
171 }
172 pub fn with_client_x509_cert_url(&mut self, value: &str) -> &mut Self {
173 self.secret.client_x509_cert_url = Some(value.to_string());
174 self
175 }
176
177 fn full_path(&self) -> String {
178 self.config_root.full_path().display().to_string()
179 }
180
181 pub fn build(&self) -> ClientConfig {
182 let persist_path = format!("{}/gmail1", self.full_path());
183
184 ClientConfig {
185 secret: self.secret.clone(),
186 config_root: self.config_root.clone(),
187 persist_path,
188 }
189 }
190}