Skip to main content

opencode/
builder.rs

1use std::{collections::BTreeMap, path::PathBuf, time::Duration};
2
3use crate::OpencodeClient;
4
5#[derive(Clone, Debug)]
6pub struct OpencodeClientBuilder {
7    binary: PathBuf,
8    env: BTreeMap<String, String>,
9    timeout: Option<Duration>,
10}
11
12impl Default for OpencodeClientBuilder {
13    fn default() -> Self {
14        Self {
15            binary: std::env::var_os("OPENCODE_BINARY")
16                .map(PathBuf::from)
17                .unwrap_or_else(|| PathBuf::from("opencode")),
18            env: BTreeMap::new(),
19            timeout: None,
20        }
21    }
22}
23
24impl OpencodeClientBuilder {
25    pub fn binary(mut self, path: impl Into<PathBuf>) -> Self {
26        self.binary = path.into();
27        self
28    }
29
30    pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
31        self.env.insert(key.into(), value.into());
32        self
33    }
34
35    pub fn timeout(mut self, timeout: Duration) -> Self {
36        self.timeout = Some(timeout);
37        self
38    }
39
40    pub fn build(self) -> OpencodeClient {
41        OpencodeClient {
42            binary: self.binary,
43            env: self.env,
44            timeout: self.timeout,
45        }
46    }
47}