From f81444ccadae161ef11c8e64520d1467da08f58e Mon Sep 17 00:00:00 2001 From: Jacob Reger Date: Tue, 25 Feb 2025 09:17:19 -0800 Subject: [PATCH] libguestfs: Rust binding build error and warning fixes Internal error type did not implement the necessary traits to be treated as a proper Rust error. This would cause ergonomic issues with manual error handling and with error handling crates like anyhow. Implement the Display and Error trait for internal Error type. This also fixes the build warnings. Signed-off-by: Jacob Reger --- rust/src/error.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/rust/src/error.rs b/rust/src/error.rs index ce444e199..f3709516c 100644 --- a/rust/src/error.rs +++ b/rust/src/error.rs @@ -46,6 +46,42 @@ pub enum Error { Create, } +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::API(err) => { + write!( + f, + "API Error:\n\tOperation: {}\n\tMessage: {}\n\tError Number: {}", + err.operation, err.message, err.errno + ) + } + Error::IllegalString(err) => { + write!( + f, + "Illegal string Error:\nNull byte found\n\tDetails: {}", + err + ) + } + Error::Utf8Error(err) => { + write!( + f, + "Utf8 Error:\nFailed to interpret string as utf-8\n\tDetails: {}", + err + ) + } + Error::UnixError(err, op) => { + write!(f, "Unix Error:\n\tError: {}\n\tOperation: {}", err, op) + } + Error::Create => { + write!(f, "Creation Error:\nFailed to create a guestfs handle") + } + } + } +} + +impl std::error::Error for Error {} + impl convert::From for Error { fn from(error: ffi::NulError) -> Self { Error::IllegalString(error)