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 request_base(&self) -> &crate::cli::command::RequestBase {
111        match self {
112            Request::Get(inner) => inner.request_base(),
113            Request::GetRequestSchema(inner) => inner.request_base(),
114            Request::GetResponseSchema(inner) => inner.request_base(),
115            Request::Address(inner) => inner.request_base(),
116            Request::User(inner) => inner.request_base(),
117            Request::Password(inner) => inner.request_base(),
118            Request::Database(inner) => inner.request_base(),
119        }
120    }
121
122    fn request_base_mut(&mut self) -> Option<&mut crate::cli::command::RequestBase> {
123        match self {
124            Request::Get(inner) => inner.request_base_mut(),
125            Request::GetRequestSchema(inner) => inner.request_base_mut(),
126            Request::GetResponseSchema(inner) => inner.request_base_mut(),
127            Request::Address(inner) => inner.request_base_mut(),
128            Request::User(inner) => inner.request_base_mut(),
129            Request::Password(inner) => inner.request_base_mut(),
130            Request::Database(inner) => inner.request_base_mut(),
131        }
132    }
133}
134
135#[cfg(feature = "cli-executor")]
136pub async fn execute<E: crate::cli::command::CommandExecutor>(
137    executor: &E,
138    request: Request,
139
140        agent_arguments: Option<&crate::cli::command::AgentArguments>,
141    ) -> Result<
142    std::pin::Pin<Box<dyn futures::Stream<Item = Result<Response, E::Error>> + Send>>,
143    E::Error,
144> {
145    use futures::StreamExt;
146    let stream: std::pin::Pin<Box<dyn futures::Stream<Item = Result<Response, E::Error>> + Send>> =
147        match request {
148            Request::Get(req) => {
149                let value = get::execute(executor, req, agent_arguments).await?;
150                Box::pin(crate::cli::command::StreamOnce::new(Ok(
151                    Response::Get(value),
152                )))
153            }
154            Request::GetRequestSchema(req) => {
155                let value = get::request_schema::execute(executor, req, agent_arguments).await?;
156                Box::pin(crate::cli::command::StreamOnce::new(Ok(
157                    Response::GetRequestSchema(value),
158                )))
159            }
160            Request::GetResponseSchema(req) => {
161                let value = get::response_schema::execute(executor, req, agent_arguments).await?;
162                Box::pin(crate::cli::command::StreamOnce::new(Ok(
163                    Response::GetResponseSchema(value),
164                )))
165            }
166            Request::Address(req) => {
167                let inner = address::execute(executor, req, agent_arguments).await?;
168                Box::pin(inner.map(|r| r.map(Response::Address)))
169            }
170            Request::User(req) => {
171                let inner = user::execute(executor, req, agent_arguments).await?;
172                Box::pin(inner.map(|r| r.map(Response::User)))
173            }
174            Request::Password(req) => {
175                let inner = password::execute(executor, req, agent_arguments).await?;
176                Box::pin(inner.map(|r| r.map(Response::Password)))
177            }
178            Request::Database(req) => {
179                let inner = database::execute(executor, req, agent_arguments).await?;
180                Box::pin(inner.map(|r| r.map(Response::Database)))
181            }
182        };
183    Ok(stream)
184}
185
186#[cfg(feature = "cli-executor")]
187pub async fn execute_transform<E: crate::cli::command::CommandExecutor>(
188    executor: &E,
189    request: Request,
190    transform: crate::cli::command::Transform,
191
192        agent_arguments: Option<&crate::cli::command::AgentArguments>,
193    ) -> Result<
194    std::pin::Pin<Box<dyn futures::Stream<Item = Result<serde_json::Value, E::Error>> + Send>>,
195    E::Error,
196> {
197    let stream: std::pin::Pin<Box<dyn futures::Stream<Item = Result<serde_json::Value, E::Error>> + Send>> =
198        match request {
199            Request::Get(req) => {
200                let value = get::execute_transform(executor, req, transform, agent_arguments).await?;
201                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
202            }
203            Request::GetRequestSchema(req) => {
204                let value = get::request_schema::execute_transform(executor, req, transform, agent_arguments).await?;
205                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
206            }
207            Request::GetResponseSchema(req) => {
208                let value = get::response_schema::execute_transform(executor, req, transform, agent_arguments).await?;
209                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
210            }
211            Request::Address(req) => {
212                let inner = address::execute_transform(executor, req, transform, agent_arguments).await?;
213                Box::pin(inner)
214            }
215            Request::User(req) => {
216                let inner = user::execute_transform(executor, req, transform, agent_arguments).await?;
217                Box::pin(inner)
218            }
219            Request::Password(req) => {
220                let inner = password::execute_transform(executor, req, transform, agent_arguments).await?;
221                Box::pin(inner)
222            }
223            Request::Database(req) => {
224                let inner = database::execute_transform(executor, req, transform, agent_arguments).await?;
225                Box::pin(inner)
226            }
227        };
228    Ok(stream)
229}