Skip to main content

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

1pub mod address;
2pub mod database;
3pub mod get;
4pub mod password;
5pub mod user;
6
7#[derive(clap::Subcommand)]
8pub enum Command {
9    Get(get::Command),
10    Address {
11        #[command(subcommand)]
12        command: address::Command,
13    },
14    User {
15        #[command(subcommand)]
16        command: user::Command,
17    },
18    Password {
19        #[command(subcommand)]
20        command: password::Command,
21    },
22    Database {
23        #[command(subcommand)]
24        command: database::Command,
25    },
26}
27
28#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
29#[serde(untagged)]
30#[schemars(rename = "cli.command.db.config.Request")]
31pub enum Request {
32    #[schemars(title = "Get")]
33    Get(get::Request),
34    #[schemars(title = "GetRequestSchema")]
35    GetRequestSchema(get::request_schema::Request),
36    #[schemars(title = "GetResponseSchema")]
37    GetResponseSchema(get::response_schema::Request),
38    #[schemars(title = "Address")]
39    Address(address::Request),
40    #[schemars(title = "User")]
41    User(user::Request),
42    #[schemars(title = "Password")]
43    Password(password::Request),
44    #[schemars(title = "Database")]
45    Database(database::Request),
46}
47
48// Exempt from json-schema coverage: tier aggregate (see the root
49// `ResponseItem` in command.rs - TS7056).
50#[objectiveai_sdk_macros::json_schema_ignore]
51#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
52#[schemars(rename = "cli.command.db.config.Response")]
53#[serde(untagged)]
54pub enum Response {
55    #[schemars(title = "Get")]
56    Get(get::Response),
57    #[schemars(title = "GetRequestSchema")]
58    GetRequestSchema(get::request_schema::Response),
59    #[schemars(title = "GetResponseSchema")]
60    GetResponseSchema(get::response_schema::Response),
61    #[schemars(title = "Address")]
62    Address(address::Response),
63    #[schemars(title = "User")]
64    User(user::Response),
65    #[schemars(title = "Password")]
66    Password(password::Response),
67    #[schemars(title = "Database")]
68    Database(database::Response),
69}
70
71#[cfg(feature = "mcp")]
72impl crate::cli::command::CommandResponse for Response {
73    fn into_mcp(self) -> crate::cli::command::McpResponseItem {
74        match self {
75            Response::Get(v) => v.into_mcp(),
76            Response::GetRequestSchema(v) => v.into_mcp(),
77            Response::GetResponseSchema(v) => v.into_mcp(),
78            Response::Address(v) => v.into_mcp(),
79            Response::User(v) => v.into_mcp(),
80            Response::Password(v) => v.into_mcp(),
81            Response::Database(v) => v.into_mcp(),
82        }
83    }
84}
85
86impl TryFrom<Command> for Request {
87    type Error = crate::cli::command::FromArgsError;
88    fn try_from(command: Command) -> Result<Self, Self::Error> {
89        match command {
90            Command::Get(cmd) => match cmd.schema {
91                None => Ok(Request::Get(get::Request::try_from(cmd.args)?)),
92                Some(get::Schema::RequestSchema(args)) =>
93                    Ok(Request::GetRequestSchema(get::request_schema::Request::try_from(args)?)),
94                Some(get::Schema::ResponseSchema(args)) =>
95                    Ok(Request::GetResponseSchema(get::response_schema::Request::try_from(args)?)),
96            },
97            Command::Address { command } =>
98                Ok(Request::Address(address::Request::try_from(command)?)),
99            Command::User { command } =>
100                Ok(Request::User(user::Request::try_from(command)?)),
101            Command::Password { command } =>
102                Ok(Request::Password(password::Request::try_from(command)?)),
103            Command::Database { command } =>
104                Ok(Request::Database(database::Request::try_from(command)?)),
105        }
106    }
107}
108
109impl crate::cli::command::CommandRequest for Request {
110    fn into_command(&self) -> Vec<String> {
111        match self {
112            Request::Get(inner) => inner.into_command(),
113            Request::GetRequestSchema(inner) => inner.into_command(),
114            Request::GetResponseSchema(inner) => inner.into_command(),
115            Request::Address(inner) => inner.into_command(),
116            Request::User(inner) => inner.into_command(),
117            Request::Password(inner) => inner.into_command(),
118            Request::Database(inner) => inner.into_command(),
119        }
120    }
121
122    fn request_base(&self) -> &crate::cli::command::RequestBase {
123        match self {
124            Request::Get(inner) => inner.request_base(),
125            Request::GetRequestSchema(inner) => inner.request_base(),
126            Request::GetResponseSchema(inner) => inner.request_base(),
127            Request::Address(inner) => inner.request_base(),
128            Request::User(inner) => inner.request_base(),
129            Request::Password(inner) => inner.request_base(),
130            Request::Database(inner) => inner.request_base(),
131        }
132    }
133
134    fn request_base_mut(&mut self) -> Option<&mut crate::cli::command::RequestBase> {
135        match self {
136            Request::Get(inner) => inner.request_base_mut(),
137            Request::GetRequestSchema(inner) => inner.request_base_mut(),
138            Request::GetResponseSchema(inner) => inner.request_base_mut(),
139            Request::Address(inner) => inner.request_base_mut(),
140            Request::User(inner) => inner.request_base_mut(),
141            Request::Password(inner) => inner.request_base_mut(),
142            Request::Database(inner) => inner.request_base_mut(),
143        }
144    }
145}
146
147#[cfg(feature = "cli-executor")]
148pub async fn execute<E: crate::cli::command::CommandExecutor>(
149    executor: &E,
150    request: Request,
151
152        agent_arguments: Option<&crate::cli::command::AgentArguments>,
153    ) -> Result<
154    std::pin::Pin<Box<dyn futures::Stream<Item = Result<Response, E::Error>> + Send>>,
155    E::Error,
156> {
157    use futures::StreamExt;
158    let stream: std::pin::Pin<Box<dyn futures::Stream<Item = Result<Response, E::Error>> + Send>> =
159        match request {
160            Request::Get(req) => {
161                let value = get::execute(executor, req, agent_arguments).await?;
162                Box::pin(crate::cli::command::StreamOnce::new(Ok(
163                    Response::Get(value),
164                )))
165            }
166            Request::GetRequestSchema(req) => {
167                let value = get::request_schema::execute(executor, req, agent_arguments).await?;
168                Box::pin(crate::cli::command::StreamOnce::new(Ok(
169                    Response::GetRequestSchema(value),
170                )))
171            }
172            Request::GetResponseSchema(req) => {
173                let value = get::response_schema::execute(executor, req, agent_arguments).await?;
174                Box::pin(crate::cli::command::StreamOnce::new(Ok(
175                    Response::GetResponseSchema(value),
176                )))
177            }
178            Request::Address(req) => {
179                let inner = address::execute(executor, req, agent_arguments).await?;
180                Box::pin(inner.map(|r| r.map(Response::Address)))
181            }
182            Request::User(req) => {
183                let inner = user::execute(executor, req, agent_arguments).await?;
184                Box::pin(inner.map(|r| r.map(Response::User)))
185            }
186            Request::Password(req) => {
187                let inner = password::execute(executor, req, agent_arguments).await?;
188                Box::pin(inner.map(|r| r.map(Response::Password)))
189            }
190            Request::Database(req) => {
191                let inner = database::execute(executor, req, agent_arguments).await?;
192                Box::pin(inner.map(|r| r.map(Response::Database)))
193            }
194        };
195    Ok(stream)
196}
197
198#[cfg(feature = "cli-executor")]
199pub async fn execute_transform<E: crate::cli::command::CommandExecutor>(
200    executor: &E,
201    request: Request,
202    transform: crate::cli::command::Transform,
203
204        agent_arguments: Option<&crate::cli::command::AgentArguments>,
205    ) -> Result<
206    std::pin::Pin<Box<dyn futures::Stream<Item = Result<serde_json::Value, E::Error>> + Send>>,
207    E::Error,
208> {
209    let stream: std::pin::Pin<Box<dyn futures::Stream<Item = Result<serde_json::Value, E::Error>> + Send>> =
210        match request {
211            Request::Get(req) => {
212                let value = get::execute_transform(executor, req, transform, agent_arguments).await?;
213                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
214            }
215            Request::GetRequestSchema(req) => {
216                let value = get::request_schema::execute_transform(executor, req, transform, agent_arguments).await?;
217                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
218            }
219            Request::GetResponseSchema(req) => {
220                let value = get::response_schema::execute_transform(executor, req, transform, agent_arguments).await?;
221                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
222            }
223            Request::Address(req) => {
224                let inner = address::execute_transform(executor, req, transform, agent_arguments).await?;
225                Box::pin(inner)
226            }
227            Request::User(req) => {
228                let inner = user::execute_transform(executor, req, transform, agent_arguments).await?;
229                Box::pin(inner)
230            }
231            Request::Password(req) => {
232                let inner = password::execute_transform(executor, req, transform, agent_arguments).await?;
233                Box::pin(inner)
234            }
235            Request::Database(req) => {
236                let inner = database::execute_transform(executor, req, transform, agent_arguments).await?;
237                Box::pin(inner)
238            }
239        };
240    Ok(stream)
241}