From 4d83564d27139f4c9ed6238f7e8a15858eefb612 Mon Sep 17 00:00:00 2001 From: Jan Allersma Date: Feb 01 2019 14:36:31 +0000 Subject: Update README.md Rename 'debug' module to 'util'. --- diff --git a/README.md b/README.md index 19c939c..f29a2cf 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ # Ambassade debug +A crate to send postmortem debug information. Built for +[Ambassade](https://pagure.io/Ambassade) in particular. + ## License See the [`LICENSE`](https://pagure.io/Ambassade/raw/master/f/doc/LICENSE) -document for this library's license. +document for this crate's license. diff --git a/src/debug.rs b/src/debug.rs deleted file mode 100644 index ad3e081..0000000 --- a/src/debug.rs +++ /dev/null @@ -1,97 +0,0 @@ -use std::panic::Location; -use std::marker::{Send, Sync}; -use std::fmt; - -/// The trait that needs to be implemented for using the `watch()` function. -/// -/// # Example mockup -/// ``` -/// pub struct MockType {} -/// -/// impl SubmitMethod for MockType { -/// fn submit(&self, info: &DebugInfo) -> bool { -/// false -/// } -/// } -/// ``` - -pub trait SubmitMethod: Send + Sync { - /// The `submit()` function is used for submitting information about a crash of an exectuable - /// The return value of type 'bool' should indicate wether the - /// `DebugInfo` is submitted successfully. - fn submit(&self, info: &DebugInfo) -> bool; - - /// This function will be called by `watch()` if the `submit()` function - /// returns 'true'. - /// # Default implementation - /// ``` - /// fn submission_succeeded(&self, info: &DebugInfo) { - /// println!("Thank you for your submission. We will investigate what happened A.S.A.P."); - /// } - /// ``` - #[allow(unused_variables)] - fn submission_succeeded(&self, info: &DebugInfo) { - println!("Thank you for your submission. We will investigate what happened A.S.A.P."); - } - - /// This function will be called by `watch()` if the `submit()` function - /// returns 'false'. - /// # Default implementation - /// ``` - /// fn submission_failed(&self, info &DebugInfo) { - /// println!("Something went wrong. Our apologies for the inconvenience. This information could not be sent:\n{}", info); - /// } - /// ``` - fn submission_failed(&self, info: &DebugInfo) { - println!("Something went wrong. Our apologies for the inconvenience. This information could not be sent:\n{}", info); - } -} - -/// A struct containing postmortem debug information that will be sent with -/// something that implements the `SubmitMethod` trait, through `watch()`. -pub struct DebugInfo<'a> { - sha: &'a str, - description: String, - info: &'a str, - location: Option<&'a Location<'a>> -} - -impl<'a> DebugInfo<'a> { - pub fn new(sha: &'a str, desc: String, message: &'a str, panic_location: Option<&'a Location<'a>>) -> DebugInfo<'a> { - DebugInfo { - sha: sha, - description: desc, - info: message, - location: panic_location - } - } -} - -impl<'a> fmt::Display for DebugInfo<'a> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let mut string = String::new(); - - string.push_str("tree: "); - string.push_str(self.sha); - string.push('\n'); - - string.push_str("at: "); - - match self.location { - Some(location) => string.push_str(&location.to_string()), - None => string.push_str("") - } - - string.push('\n'); - - string.push_str("Panic details: \n"); - string.push_str(self.info); - string.push('\n'); - - string.push_str("Summary: \n"); - string.push_str(&self.description); - - try!(write!(f, "{}", string)); - Ok(()) - } -} diff --git a/src/lib.rs b/src/lib.rs index fe74e5a..f668401 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ /// This module contains all the tools that the `watch()` function needs. -pub mod debug; +pub mod util; mod git; use std::{io, panic}; @@ -14,7 +14,7 @@ mod tests { /// This function is used to send debug information, using the `submit()` function /// from the `method` parameter when the Rust application panics. -pub fn watch(method: F) where F: debug::SubmitMethod { +pub fn watch(method: F) where F: util::SubmitMethod { panic::set_hook(Box::new(move |panic_info| { let mut input = String::new(); @@ -37,7 +37,7 @@ pub fn watch(method: F) where F: debug::SubmitMethod { None => String::from("None") }; - let dbg = debug::DebugInfo::new ( + let dbg = util::DebugInfo::new ( &sha, desc, panic_info.payload().downcast_ref::<&str>().unwrap(), diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..3e1ab7c --- /dev/null +++ b/src/util.rs @@ -0,0 +1,99 @@ +use std::panic::Location; +use std::marker::{Send, Sync}; +use std::fmt; + +/// The trait that needs to be implemented for using the `watch()` function. +/// +/// # Example mockup +/// ``` +/// pub struct MockType {} +/// +/// impl SubmitMethod for MockType { +/// fn submit(&self, info: &DebugInfo) -> bool { +/// false +/// } +/// } +/// ``` + +pub trait SubmitMethod: Send + Sync { + /// The `submit()` function is used for submitting information about a crash of an exectuable + /// The return value of type 'bool' should indicate wether the + /// `DebugInfo` is submitted successfully. + // Experimental attribute. Check if this is used on 'impl' as well. + #[allow(unused_variables)] + fn submit(&self, info: &DebugInfo) -> bool; + + /// This function will be called by `watch()` if the `submit()` function + /// returns 'true'. + /// # Default implementation + /// ``` + /// fn submission_succeeded(&self, info: &DebugInfo) { + /// println!("Thank you for your submission. We will investigate what happened A.S.A.P."); + /// } + /// ``` + #[allow(unused_variables)] + fn submission_succeeded(&self, info: &DebugInfo) { + println!("Thank you for your submission. We will investigate what happened A.S.A.P."); + } + + /// This function will be called by `watch()` if the `submit()` function + /// returns 'false'. + /// # Default implementation + /// ``` + /// fn submission_failed(&self, info &DebugInfo) { + /// println!("Something went wrong. Our apologies for the inconvenience. This information could not be sent:\n{}", info); + /// } + /// ``` + fn submission_failed(&self, info: &DebugInfo) { + println!("Something went wrong. Our apologies for the inconvenience. This information could not be sent:\n{}", info); + } +} + +/// A struct containing postmortem debug information that will be sent with +/// something that implements the `SubmitMethod` trait, through `watch()`. +pub struct DebugInfo<'a> { + sha: &'a str, + description: String, + info: &'a str, + location: Option<&'a Location<'a>> +} + +impl<'a> DebugInfo<'a> { + pub fn new(sha: &'a str, desc: String, message: &'a str, panic_location: Option<&'a Location<'a>>) -> DebugInfo<'a> { + DebugInfo { + sha: sha, + description: desc, + info: message, + location: panic_location + } + } +} + +impl<'a> fmt::Display for DebugInfo<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let mut string = String::new(); + + string.push_str("tree: "); + string.push_str(self.sha); + string.push('\n'); + + string.push_str("at: "); + + match self.location { + Some(location) => string.push_str(&location.to_string()), + None => string.push_str("") + } + + string.push('\n'); + + string.push_str("Panic details: \n"); + string.push_str(self.info); + string.push('\n'); + + string.push_str("Summary: \n"); + string.push_str(&self.description); + + try!(write!(f, "{}", string)); + Ok(()) + } +}