Skip to main content

objectiveai_cli/filesystem/config/
db.rs

1use serde::{Deserialize, Serialize};
2
3/// Defaults applied wherever a `DbConfig` field is unset. They match
4/// objectiveai-db's own env defaults, so a fresh install can
5/// `objectiveai db spawn` and connect with zero config.
6pub const DB_DEFAULT_ADDRESS: &str = "127.0.0.1";
7pub const DB_DEFAULT_USER: &str = "postgres";
8pub const DB_DEFAULT_PASSWORD: &str = "objectiveai";
9pub const DB_DEFAULT_DATABASE: &str = "objectiveai";
10
11#[derive(
12    Debug, Clone, Default, Serialize, Deserialize, schemars::JsonSchema,
13)]
14#[schemars(rename = "filesystem.config.DbConfig")]
15pub struct DbConfig {
16    #[serde(default, skip_serializing_if = "Option::is_none")]
17    #[schemars(extend("omitempty" = true))]
18    pub address: Option<String>,
19    #[serde(default, skip_serializing_if = "Option::is_none")]
20    #[schemars(extend("omitempty" = true))]
21    pub user: Option<String>,
22    #[serde(default, skip_serializing_if = "Option::is_none")]
23    #[schemars(extend("omitempty" = true))]
24    pub password: Option<String>,
25    #[serde(default, skip_serializing_if = "Option::is_none")]
26    #[schemars(extend("omitempty" = true))]
27    pub database: Option<String>,
28}
29
30impl DbConfig {
31    pub fn is_empty(&self) -> bool {
32        self.address.is_none()
33            && self.user.is_none()
34            && self.password.is_none()
35            && self.database.is_none()
36    }
37
38    pub fn is_none(this: &Option<Self>) -> bool {
39        this.as_ref().is_none_or(|cfg| cfg.is_empty())
40    }
41
42    pub fn get_address(&self) -> Option<&str> {
43        self.address.as_deref()
44    }
45
46    pub fn set_address(&mut self, value: impl Into<String>) {
47        self.address = Some(value.into());
48    }
49
50
51    pub fn get_user(&self) -> Option<&str> {
52        self.user.as_deref()
53    }
54
55    pub fn set_user(&mut self, value: impl Into<String>) {
56        self.user = Some(value.into());
57    }
58
59    pub fn get_password(&self) -> Option<&str> {
60        self.password.as_deref()
61    }
62
63    pub fn set_password(&mut self, value: impl Into<String>) {
64        self.password = Some(value.into());
65    }
66
67    pub fn get_database(&self) -> Option<&str> {
68        self.database.as_deref()
69    }
70
71    pub fn set_database(&mut self, value: impl Into<String>) {
72        self.database = Some(value.into());
73    }
74
75    pub fn jq(
76        &self,
77        filter: &str,
78    ) -> Result<Vec<serde_json::Value>, super::super::Error> {
79        super::super::run_jq(self, filter)
80    }
81}