Skip to content

Commit 38ba6fd

Browse files
committed
refactor: better syntax for gpt.new method
1 parent 9ff2103 commit 38ba6fd

1 file changed

Lines changed: 11 additions & 13 deletions

File tree

src/gpt.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,23 @@ pub struct GPT {
1111

1212
impl GPT {
1313
pub fn new(api_key: Option<String>) -> Self {
14-
match std::env::var("OPENAI_API_KEY") {
15-
Ok(key) => Self {
16-
client: reqwest::blocking::Client::new(),
17-
openai_api_key: key,
18-
},
19-
Err(_) => match api_key {
20-
Some(key) => Self {
21-
client: reqwest::blocking::Client::new(),
22-
openai_api_key: key,
23-
},
24-
None => {
14+
let openai_api_key = match std::env::var("OPENAI_API_KEY") {
15+
Ok(key) => key,
16+
Err(_) => api_key
17+
.ok_or_else(|| {
2518
eprintln!(
2619
r#"Can not find the openai api key
2720
You need to define one wether in the configuration file or as an environment variable"#
2821
);
2922

3023
std::process::exit(1);
31-
}
32-
},
24+
})
25+
.unwrap(),
26+
};
27+
28+
Self {
29+
client: reqwest::blocking::Client::new(),
30+
openai_api_key,
3331
}
3432
}
3533

0 commit comments

Comments
 (0)