payjp_client_core/
config.rs1use std::fmt;
2use std::fmt::{Display, Formatter};
3
4
5use crate::RequestStrategy;
6
7#[derive(Clone)]
14pub struct SharedConfigBuilder {
15 pub app_info_str: Option<String>,
17 pub request_strategy: Option<RequestStrategy>,
19 pub secret: String,
21 pub api_base: Option<String>,
23}
24
25impl SharedConfigBuilder {
26 pub fn new(secret: impl Into<String>) -> Self {
28 let secret = secret.into();
29
30 if secret.trim() != secret || !secret.starts_with("sk_") {
33 tracing::warn!("suspiciously formatted secret key")
34 }
35
36 Self {
37 app_info_str: None,
38 request_strategy: None,
39 secret,
40 api_base: None,
41 }
42 }
43
44 pub fn request_strategy(mut self, strategy: RequestStrategy) -> Self {
46 self.request_strategy = Some(strategy);
47 self
48 }
49
50 pub fn url(mut self, url: impl Into<String>) -> Self {
52 self.api_base = Some(url.into());
53 self
54 }
55
56 pub fn app_info(
58 mut self,
59 name: impl Into<String>,
60 version: Option<String>,
61 url: Option<String>,
62 ) -> Self {
63 self.app_info_str = Some(AppInfo { name: name.into(), url, version }.to_string());
64 self
65 }
66}
67
68struct AppInfo {
69 name: String,
70 url: Option<String>,
71 version: Option<String>,
72}
73
74impl Display for AppInfo {
75 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
79 let name = &self.name;
80 match (&self.version, &self.url) {
81 (Some(a), Some(b)) => write!(f, "{name}/{a} ({b})"),
82 (Some(a), None) => write!(f, "{name}/{a}"),
83 (None, Some(b)) => write!(f, "{name} ({b})"),
84 _ => f.write_str(name),
85 }
86 }
87}
88
89impl fmt::Debug for SharedConfigBuilder {
91 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
92 let mut builder = f.debug_struct("SharedConfigBuilder");
93 builder.field("request_strategy", &self.request_strategy);
94 builder.field("app_info_str", &self.app_info_str);
95 if let Some(api_base) = &self.api_base {
96 builder.field("api_base", api_base);
97 }
98 builder.finish()
99 }
100}
101
102#[derive(Debug)]
104#[non_exhaustive]
105pub struct ConfigOverride {
106 pub request_strategy: Option<RequestStrategy>,
108}
109
110impl ConfigOverride {
111 pub(crate) fn new() -> Self {
112 Self { request_strategy: None }
113 }
114}