Skip to main content

objectiveai_sdk/cli/command/db/config/set/
mod.rs

1//! `db config set` — async handler stub.
2
3use crate::cli::command::CommandRequest;
4
5#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
6#[schemars(rename = "cli.command.db.config.set.Request")]
7pub struct Request {
8    pub path_type: Path,
9    #[serde(flatten)]
10    pub base: crate::cli::command::RequestBase,
11    pub value: Value,
12}
13
14/// The whole `db` config section as one object — the postgres
15/// connection coordinates are LINKED (an address, the user/password
16/// that authenticate there, the database they open), so they are set
17/// together, atomically. FULL-REPLACE semantics: this object becomes
18/// the section verbatim; omitted fields are cleared.
19#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
20#[schemars(rename = "cli.command.db.config.set.Value")]
21pub struct Value {
22    #[serde(default, skip_serializing_if = "Option::is_none")]
23    #[schemars(extend("omitempty" = true))]
24    pub address: Option<String>,
25    #[serde(default, skip_serializing_if = "Option::is_none")]
26    #[schemars(extend("omitempty" = true))]
27    pub user: Option<String>,
28    #[serde(default, skip_serializing_if = "Option::is_none")]
29    #[schemars(extend("omitempty" = true))]
30    pub password: Option<String>,
31    #[serde(default, skip_serializing_if = "Option::is_none")]
32    #[schemars(extend("omitempty" = true))]
33    pub database: Option<String>,
34}
35
36#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
37#[schemars(rename = "cli.command.db.config.set.Path")]
38pub enum Path {
39    #[serde(rename = "db/config/set")]
40    DbConfigSet,
41}
42
43impl CommandRequest for Request {
44    fn request_base(&self) -> &crate::cli::command::RequestBase {
45        &self.base
46    }
47
48    fn request_base_mut(&mut self) -> Option<&mut crate::cli::command::RequestBase> {
49        Some(&mut self.base)
50    }
51}
52
53pub type Response = crate::cli::command::Ok;
54
55#[derive(clap::Args)]
56#[command(group(clap::ArgGroup::new("value_required").required(true).args(["value"])))]
57pub struct Args {
58    #[command(flatten)]
59    pub base: crate::cli::command::RequestBaseArgs,
60    /// The whole section as inline JSON (full replace — omitted
61    /// fields are cleared).
62    #[arg(long)]
63    pub value: Option<String>,
64}
65
66#[derive(clap::Args)]
67#[command(args_conflicts_with_subcommands = true)]
68pub struct Command {
69    #[command(flatten)]
70    pub args: Args,
71    #[command(subcommand)]
72    pub schema: Option<Schema>,
73}
74
75#[derive(clap::Subcommand)]
76pub enum Schema {
77    /// Emit the JSON Schema for this leaf's `Request` type and exit.
78    RequestSchema(request_schema::Args),
79    /// Emit the JSON Schema for this leaf's `Response` type and exit.
80    ResponseSchema(response_schema::Args),
81}
82
83impl TryFrom<Args> for Request {
84    type Error = crate::cli::command::FromArgsError;
85    fn try_from(args: Args) -> Result<Self, Self::Error> {
86        let raw = args.value.ok_or_else(|| {
87            crate::cli::command::FromArgsError::path_parse(
88                "value",
89                "--value is required".to_string(),
90            )
91        })?;
92        let mut de = serde_json::Deserializer::from_str(&raw);
93        let value = serde_path_to_error::deserialize(&mut de)
94            .map_err(|e| crate::cli::command::FromArgsError::json("value", e))?;
95        Ok(Self {
96            base: args.base.into(),
97            path_type: Path::DbConfigSet,
98            value,
99        })
100    }
101}
102
103#[cfg(feature = "cli-executor")]
104pub async fn execute<E: crate::cli::command::CommandExecutor>(
105    executor: &E,
106    request: Request,
107
108        agent_arguments: Option<&crate::cli::command::AgentArguments>,
109    ) -> Result<Response, E::Error> {
110    executor.execute_one(request, agent_arguments).await
111}
112
113pub mod request_schema;
114
115pub mod response_schema;
116
117#[cfg(feature = "cli-executor")]
118pub async fn execute_transform<E: crate::cli::command::CommandExecutor>(
119    executor: &E,
120    request: Request,
121    _transform: crate::cli::command::Transform,
122
123        agent_arguments: Option<&crate::cli::command::AgentArguments>,
124    ) -> Result<serde_json::Value, E::Error> {
125    let resp: Response = executor.execute_one(request, agent_arguments).await?;
126    Ok(serde_json::to_value(resp).expect("Response serializes"))
127}
128
129/// One `/listen` broadcast run of `db config set`: the actual
130/// [`Request`], the producer's
131/// [`AgentArguments`](crate::cli::command::AgentArguments), and the
132/// unary response future. See [`crate::cli::broadcast_listener`].
133#[cfg(feature = "cli-listener")]
134pub struct ListenerExecution {
135    pub request: Request,
136    pub agent_arguments: crate::cli::command::AgentArguments,
137    pub response: crate::cli::broadcast_listener::UnaryResponse<Response>,
138}