在我的 Rust 应用程序中,我需要对重复键(代码 11000)错误进行特殊处理。我想出了这个解决方案,但它看起来很复杂。有没有更好的方法?
let res = collection.insert_one(entry).await;
match res {
Ok(_) => Ok(()),
Err(e) => {
// println!("{:?}", e);
match *e.kind {
ErrorKind::Write(failure) => {
match failure {
WriteFailure::WriteError(we) => {
if we.code == 11000 { Ok(()) }
else { Err("other write failure".into()) }
},
_ => Err("other write error".into()),
}
},
_ => Err("other error".into()),
}
},
}