Skip to content

Commit 70b584e

Browse files
committed
Add serde(other) to credential protocol enums for future proofing
1 parent 7918c7f commit 70b584e

File tree

1 file changed

+17
-7
lines changed
  • credential/cargo-credential/src

1 file changed

+17
-7
lines changed

credential/cargo-credential/src/lib.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ pub enum Action<'a> {
7777
Get(Operation<'a>),
7878
Login(LoginOptions<'a>),
7979
Logout,
80+
#[serde(other)]
81+
Unknown,
8082
}
8183

8284
impl<'a> Display for Action<'a> {
@@ -85,6 +87,7 @@ impl<'a> Display for Action<'a> {
8587
Action::Get(_) => f.write_str("get"),
8688
Action::Login(_) => f.write_str("login"),
8789
Action::Logout => f.write_str("logout"),
90+
Action::Unknown => f.write_str("<unknown>"),
8891
}
8992
}
9093
}
@@ -133,6 +136,8 @@ pub enum Operation<'a> {
133136
/// The name of the crate
134137
name: &'a str,
135138
},
139+
#[serde(other)]
140+
Unknown,
136141
}
137142

138143
/// Message sent by the credential helper
@@ -147,6 +152,8 @@ pub enum CredentialResponse {
147152
},
148153
Login,
149154
Logout,
155+
#[serde(other)]
156+
Unknown,
150157
}
151158

152159
#[derive(Serialize, Deserialize, Clone, Debug)]
@@ -159,6 +166,8 @@ pub enum CacheControl {
159166
Expires(#[serde(with = "time::serde::timestamp")] OffsetDateTime),
160167
/// Cache this result and use it for all subsequent requests in the current Cargo invocation.
161168
Session,
169+
#[serde(other)]
170+
Unknown,
162171
}
163172

164173
/// Credential process JSON protocol version. Incrementing
@@ -177,36 +186,37 @@ pub trait Credential {
177186

178187
/// Runs the credential interaction
179188
pub fn main(credential: impl Credential) {
180-
let result = doit(credential);
189+
let result = doit(credential).map_err(|e| Error::Other(e));
181190
if result.is_err() {
182191
serde_json::to_writer(std::io::stdout(), &result)
183192
.expect("failed to serialize credential provider error");
184193
println!();
185194
}
186195
}
187196

188-
fn doit(credential: impl Credential) -> Result<(), Error> {
197+
fn doit(
198+
credential: impl Credential,
199+
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
189200
let hello = CredentialHello {
190201
v: vec![PROTOCOL_VERSION_1],
191202
};
192-
serde_json::to_writer(std::io::stdout(), &hello).map_err(Box::new)?;
203+
serde_json::to_writer(std::io::stdout(), &hello)?;
193204
println!();
194205

195206
loop {
196207
let mut buffer = String::new();
197-
let len = std::io::stdin().read_line(&mut buffer).map_err(Box::new)?;
208+
let len = std::io::stdin().read_line(&mut buffer)?;
198209
if len == 0 {
199210
return Ok(());
200211
}
201-
let request: CredentialRequest = serde_json::from_str(&buffer).map_err(Box::new)?;
212+
let request: CredentialRequest = serde_json::from_str(&buffer)?;
202213
if request.v != PROTOCOL_VERSION_1 {
203214
return Err(format!("unsupported protocol version {}", request.v).into());
204215
}
205216
serde_json::to_writer(
206217
std::io::stdout(),
207218
&credential.perform(&request.registry, &request.action, &request.args),
208-
)
209-
.map_err(Box::new)?;
219+
)?;
210220
println!();
211221
}
212222
}

0 commit comments

Comments
 (0)