Skip to main content

objectiveai_sdk/cli/command/
from_args_error.rs

1//! Error returned by `TryFrom<Args> for Request` impls on cli leaves.
2//!
3//! `field` names the `Args` field whose value failed to convert into
4//! the leaf's typed `Request` form (e.g. `"path"`, `"agent_inline"`,
5//! `"dangerous_advanced"`). `source` carries the specific failure —
6//! either a JSON deserialization error (with `serde_path_to_error`
7//! preserving the in-document location), or a plain `String` for the
8//! docker-style `key=value,...` paths parsed by
9//! [`super::path_ref::PathRef`].
10
11#[derive(Debug)]
12pub struct FromArgsError {
13    pub field: &'static str,
14    pub source: FromArgsErrorSource,
15}
16
17impl FromArgsError {
18    /// Build a `FromArgsError` from a docker-style path parse error
19    /// produced by [`super::path_ref::PathRef::from_str`] or the
20    /// [`FromStr`] impl on [`crate::RemotePathCommitOptional`].
21    ///
22    /// [`FromStr`]: std::str::FromStr
23    pub fn path_parse(field: &'static str, source: String) -> Self {
24        Self {
25            field,
26            source: FromArgsErrorSource::Plain(source),
27        }
28    }
29
30    /// Build a `FromArgsError` from a `serde_path_to_error`-wrapped
31    /// JSON deserialization failure. Existing call sites that built
32    /// `FromArgsError { field, source }` with a `serde_path_to_error`
33    /// error keep working — `source` accepts both shapes via the
34    /// `From<serde_path_to_error::Error<serde_json::Error>>` impl.
35    pub fn json(
36        field: &'static str,
37        source: serde_path_to_error::Error<serde_json::Error>,
38    ) -> Self {
39        Self {
40            field,
41            source: FromArgsErrorSource::Json(source),
42        }
43    }
44}
45
46#[derive(Debug)]
47pub enum FromArgsErrorSource {
48    Json(serde_path_to_error::Error<serde_json::Error>),
49    Plain(String),
50}
51
52impl From<serde_path_to_error::Error<serde_json::Error>> for FromArgsErrorSource {
53    fn from(e: serde_path_to_error::Error<serde_json::Error>) -> Self {
54        Self::Json(e)
55    }
56}
57
58impl From<String> for FromArgsErrorSource {
59    fn from(s: String) -> Self {
60        Self::Plain(s)
61    }
62}
63
64impl std::fmt::Display for FromArgsErrorSource {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        match self {
67            Self::Json(e) => write!(f, "{e}"),
68            Self::Plain(s) => f.write_str(s),
69        }
70    }
71}
72
73impl std::fmt::Display for FromArgsError {
74    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75        write!(f, "argument parse error at `{}`: {}", self.field, self.source)
76    }
77}
78
79impl std::error::Error for FromArgsError {}