From b9a40ae46a72096255ed2be3f16c037d7440f374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Tue, 5 Nov 2019 03:36:44 +0100 Subject: [PATCH 1/6] :heavy_minus_sign: remove failure dependency --- Cargo.toml | 3 +- src/action/maven.rs | 3 +- src/build/common.rs | 13 +++++--- src/build/mod.rs | 8 +++-- src/client.rs | 7 ++-- src/client_internals/builder.rs | 3 +- src/client_internals/csrf.rs | 6 ++-- src/client_internals/errors.rs | 57 +++++++++++++++++++++------------ src/client_internals/mod.rs | 16 +++++---- src/home.rs | 3 +- src/job/builder.rs | 20 ++++++++---- src/job/common.rs | 28 ++++++++++------ src/job/mod.rs | 10 +++--- src/nodes/mod.rs | 10 +++--- src/queue.rs | 15 ++++++--- src/view.rs | 32 ++++++++++++++---- tests/client.rs | 4 +-- tests/propertied.rs | 4 +-- 18 files changed, 152 insertions(+), 90 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f1de9082..6be737f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,8 +24,7 @@ serde_urlencoded = "0.6" urlencoding = "1.0.0" -failure = "0.1" -regex = "1.1" +regex = "1.3" log = "0.4" diff --git a/src/action/maven.rs b/src/action/maven.rs index 62c63971..e0f8b81e 100644 --- a/src/action/maven.rs +++ b/src/action/maven.rs @@ -1,6 +1,5 @@ //! Types related to maven -use failure::Error; use serde::Deserialize; use crate::client; @@ -42,7 +41,7 @@ impl ShortMavenArtifactRecord { pub fn get_full_artifact_record( &self, jenkins_client: &Jenkins, - ) -> Result { + ) -> Result> { let path = jenkins_client.url_to_path(&self.url); if let Path::MavenArtifactRecord { .. } = path { Ok(jenkins_client.get(&path)?.json()?) diff --git a/src/build/common.rs b/src/build/common.rs index f295646f..d9064235 100644 --- a/src/build/common.rs +++ b/src/build/common.rs @@ -1,6 +1,5 @@ use std::marker::PhantomData; -use failure::Error; use serde::{self, Deserialize, Serialize}; use serde_json; @@ -36,7 +35,10 @@ where for<'de> T: Deserialize<'de>, { /// Get the full details of a `Build` matching the `ShortBuild` - pub fn get_full_build(&self, jenkins_client: &Jenkins) -> Result { + pub fn get_full_build( + &self, + jenkins_client: &Jenkins, + ) -> Result> { let path = jenkins_client.url_to_path(&self.url); if let Path::Build { .. } = path { Ok(jenkins_client.get(&path)?.json()?) @@ -165,7 +167,10 @@ pub trait Build { fn url(&self) -> &str; /// Get the `Job` from a `Build` - fn get_job(&self, jenkins_client: &Jenkins) -> Result + fn get_job( + &self, + jenkins_client: &Jenkins, + ) -> Result> where for<'de> Self::ParentJob: Deserialize<'de>, { @@ -192,7 +197,7 @@ pub trait Build { } /// Get the console output from a `Build` - fn get_console(&self, jenkins_client: &Jenkins) -> Result { + fn get_console(&self, jenkins_client: &Jenkins) -> Result> { let path = jenkins_client.url_to_path(&self.url()); if let Path::Build { job_name, diff --git a/src/build/mod.rs b/src/build/mod.rs index 76508ef8..b27c4a03 100644 --- a/src/build/mod.rs +++ b/src/build/mod.rs @@ -1,7 +1,5 @@ //! Jenkins Builds -use failure::Error; - use crate::client_internals::path::{Name, Path}; use crate::job::JobName; use crate::Jenkins; @@ -24,7 +22,11 @@ pub use self::multijob::MultiJobBuild; impl Jenkins { /// Get a build from a `job_name` and `build_number` - pub fn get_build<'a, J, B>(&self, job_name: J, build_number: B) -> Result + pub fn get_build<'a, J, B>( + &self, + job_name: J, + build_number: B, + ) -> Result> where J: Into>, B: Into, diff --git a/src/client.rs b/src/client.rs index 15b8f77a..b8607dfe 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,6 +1,5 @@ //! Helpers to build advanced queries -use failure::Error as FailureError; use serde::{self, Deserialize}; use crate::client_internals::path::{Name, Path as PrivatePath}; @@ -163,7 +162,11 @@ impl super::Jenkins { /// # } /// ``` /// - pub fn get_object_as(&self, object: Path, parameters: Q) -> Result + pub fn get_object_as( + &self, + object: Path, + parameters: Q, + ) -> Result> where Q: Into>, for<'de> T: Deserialize<'de>, diff --git a/src/client_internals/builder.rs b/src/client_internals/builder.rs index c78a43b3..ce353d6b 100644 --- a/src/client_internals/builder.rs +++ b/src/client_internals/builder.rs @@ -1,6 +1,5 @@ use std::str::FromStr; -use failure::Error; use reqwest::{self, Client, Url}; use super::{Jenkins, User}; @@ -45,7 +44,7 @@ impl JenkinsBuilder { } /// Build the Jenkins client - pub fn build(self) -> Result { + pub fn build(self) -> Result> { let url = Url::from_str(&self.url)?; if url.cannot_be_a_base() { return Err(reqwest::UrlError::RelativeUrlWithoutBase.into()); diff --git a/src/client_internals/csrf.rs b/src/client_internals/csrf.rs index b1dee8eb..6dc84100 100644 --- a/src/client_internals/csrf.rs +++ b/src/client_internals/csrf.rs @@ -1,5 +1,3 @@ -use failure; - use reqwest::{header::HeaderName, header::HeaderValue, RequestBuilder}; use serde::Deserialize; @@ -16,7 +14,7 @@ impl Jenkins { pub(crate) fn add_csrf_to_request( &self, request_builder: RequestBuilder, - ) -> Result { + ) -> Result> { if self.csrf_enabled { let crumb = self.get_csrf()?; Ok(request_builder.header( @@ -28,7 +26,7 @@ impl Jenkins { } } - pub(crate) fn get_csrf(&self) -> Result { + pub(crate) fn get_csrf(&self) -> Result> { let crumb: Crumb = self.get(&Path::CrumbIssuer)?.json()?; Ok(crumb) } diff --git a/src/client_internals/errors.rs b/src/client_internals/errors.rs index 19d71e78..32337744 100644 --- a/src/client_internals/errors.rs +++ b/src/client_internals/errors.rs @@ -1,12 +1,9 @@ use std::fmt; -use failure::Fail; - /// Errors that can be thrown -#[derive(Debug, Fail)] +#[derive(Debug)] pub enum Error { - /// Error thrown when a link between objects has an unexpected format - #[fail(display = "invalid url for {}: {}", expected, url)] + /// Error thrown when a link between objects has an unexpected format InvalidUrl { /// URL found url: String, @@ -14,38 +11,27 @@ pub enum Error { expected: ExpectedType, }, - /// Error thrown when CSRF protection use an unexpected field name - #[fail( - display = "invalid crumbfield '{}', expected 'Jenkins-Crumb'", - field_name - )] + /// Error thrown when CSRF protection use an unexpected field name InvalidCrumbFieldName { /// Field name provided by Jenkins api for crumb field_name: String, }, - /// Error thrown when building a parameterized job with an invalid parameter - #[fail(display = "illegal argument: '{}'", message)] + /// Error thrown when building a parameterized job with an invalid parameter IllegalArgument { /// Exception message provided by Jenkins message: String, }, - /// Error thrown when building a job with invalid parameters - #[fail(display = "illegal state: '{}'", message)] + /// Error thrown when building a job with invalid parameters IllegalState { /// Exception message provided by Jenkins message: String, }, - /// Error when trying to remotely build a job with parameters - #[fail(display = "can't build a job remotely with parameters")] + /// Error when trying to remotely build a job with parameters UnsupportedBuildConfiguration, - /// Error when trying to do an action on an object not supporting it - #[fail( - display = "can't do '{}' on a {} of type {}", - action, object_type, variant_name - )] + /// Error when trying to do an action on an object not supporting it InvalidObjectType { /// Object type object_type: ExpectedType, @@ -55,6 +41,35 @@ pub enum Error { action: Action, }, } +impl std::error::Error for Error {} +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Error::InvalidUrl { url, expected } => { + write!(f, "invalid url for {}: {}", expected, url) + } + Error::InvalidCrumbFieldName { field_name } => write!( + f, + "invalid crumbfield '{}', expected 'Jenkins-Crumb'", + field_name + ), + Error::IllegalArgument { message } => write!(f, "illegal argument: '{}'", message), + Error::IllegalState { message } => write!(f, "illegal state: '{}'", message), + Error::UnsupportedBuildConfiguration => { + write!(f, "can't build a job remotely with parameters") + } + Error::InvalidObjectType { + object_type, + variant_name, + action, + } => write!( + f, + "can't do '{}' on a {} of type {}", + action, object_type, variant_name + ), + } + } +} /// Possible type of URL expected in links between items #[derive(Debug, Copy, Clone)] diff --git a/src/client_internals/mod.rs b/src/client_internals/mod.rs index 23639105..e687186c 100644 --- a/src/client_internals/mod.rs +++ b/src/client_internals/mod.rs @@ -3,7 +3,6 @@ use std::fmt::Debug; use std::string::ToString; -use failure; use log::{debug, warn}; use regex::Regex; use reqwest::{ @@ -84,7 +83,10 @@ impl Jenkins { format!("{}{}", self.url, endpoint) } - fn send(&self, mut request_builder: RequestBuilder) -> Result { + fn send( + &self, + mut request_builder: RequestBuilder, + ) -> Result> { if let Some(ref user) = self.user { request_builder = request_builder.basic_auth(user.username.clone(), user.password.clone()); @@ -94,7 +96,7 @@ impl Jenkins { Ok(self.client.execute(query)?) } - fn error_for_status(response: Response) -> Result { + fn error_for_status(response: Response) -> Result> { let status = response.status(); if status.is_client_error() || status.is_server_error() { warn!("got an error: {}", status); @@ -102,7 +104,7 @@ impl Jenkins { Ok(response.error_for_status()?) } - pub(crate) fn get(&self, path: &Path) -> Result { + pub(crate) fn get(&self, path: &Path) -> Result> { self.get_with_params(path, &[("depth", &self.depth.to_string())]) } @@ -110,7 +112,7 @@ impl Jenkins { &self, path: &Path, qps: T, - ) -> Result { + ) -> Result> { let query = self .client .get(&self.url_api_json(&path.to_string())) @@ -118,7 +120,7 @@ impl Jenkins { Ok(Self::error_for_status(self.send(query)?)?) } - pub(crate) fn post(&self, path: &Path) -> Result { + pub(crate) fn post(&self, path: &Path) -> Result> { let mut request_builder = self.client.post(&self.url(&path.to_string())); request_builder = self.add_csrf_to_request(request_builder)?; @@ -131,7 +133,7 @@ impl Jenkins { path: &Path, body: T, qps: &[(&str, &str)], - ) -> Result { + ) -> Result> { let mut request_builder = self.client.post(&self.url(&path.to_string())); request_builder = self.add_csrf_to_request(request_builder)?; diff --git a/src/home.rs b/src/home.rs index 0c2a0288..a3fb33d2 100644 --- a/src/home.rs +++ b/src/home.rs @@ -1,6 +1,5 @@ //! Jenkins Home, describing state of the master -use failure::Error; use serde::Deserialize; use crate::client_internals::Path; @@ -48,7 +47,7 @@ pub struct Home { impl Jenkins { /// Get Jenkins `Home` - pub fn get_home(&self) -> Result { + pub fn get_home(&self) -> Result> { Ok(self.get(&Path::Home)?.json()?) } } diff --git a/src/job/builder.rs b/src/job/builder.rs index 85226743..e29505e7 100644 --- a/src/job/builder.rs +++ b/src/job/builder.rs @@ -1,5 +1,4 @@ //! Helper to build a job -use failure::Error; use reqwest::header::LOCATION; @@ -25,7 +24,10 @@ pub struct JobBuilder<'a, 'b, 'c, 'd> { impl<'a, 'b, 'c, 'd> JobBuilder<'a, 'b, 'c, 'd> { #[allow(clippy::new_ret_no_self)] - pub(crate) fn new(job: &'a T, jenkins_client: &'b Jenkins) -> Result + pub(crate) fn new( + job: &'a T, + jenkins_client: &'b Jenkins, + ) -> Result> where T: Job, { @@ -52,7 +54,10 @@ impl<'a, 'b, 'c, 'd> JobBuilder<'a, 'b, 'c, 'd> { } } - pub(crate) fn new_from_job_name(name: J, jenkins_client: &'b Jenkins) -> Result + pub(crate) fn new_from_job_name( + name: J, + jenkins_client: &'b Jenkins, + ) -> Result> where J: Into>, { @@ -67,7 +72,7 @@ impl<'a, 'b, 'c, 'd> JobBuilder<'a, 'b, 'c, 'd> { } /// Trigger the build - pub fn send(self) -> Result { + pub fn send(self) -> Result> { let response = match (self.token, self.parameters) { (Some(token), None) => { let bound_cause = self.cause.unwrap_or(""); @@ -145,7 +150,7 @@ impl<'a, 'b, 'c, 'd> JobBuilder<'a, 'b, 'c, 'd> { mut self, token: &'d str, cause: Option<&'c str>, - ) -> Result { + ) -> Result> { if self.parameters.is_some() { return Err(client::Error::UnsupportedBuildConfiguration.into()); } @@ -168,7 +173,10 @@ impl<'a, 'b, 'c, 'd> JobBuilder<'a, 'b, 'c, 'd> { /// /// This methods will return an error if serializing `parameters` fails, or if passing /// parameters to a remote build. - pub fn with_parameters(mut self, parameters: &T) -> Result { + pub fn with_parameters( + mut self, + parameters: &T, + ) -> Result> { if self.token.is_some() { return Err(client::Error::UnsupportedBuildConfiguration.into()); } diff --git a/src/job/common.rs b/src/job/common.rs index 5dd6c2eb..34dab0be 100644 --- a/src/job/common.rs +++ b/src/job/common.rs @@ -1,6 +1,5 @@ use std::marker::PhantomData; -use failure::Error; use serde::{self, Deserialize, Serialize}; use serde_json; @@ -88,7 +87,7 @@ where for<'de> T: Deserialize<'de>, { /// Get the full details of a `Job` matching the `ShortJob` - pub fn get_full_job(&self, jenkins_client: &Jenkins) -> Result { + pub fn get_full_job(&self, jenkins_client: &Jenkins) -> Result> { let path = jenkins_client.url_to_path(&self.url); if let Path::Job { .. } = path { Ok(jenkins_client.get(&path)?.json()?) @@ -134,7 +133,7 @@ pub trait Job { fn name(&self) -> &str; /// Enable a `Job`. It may need to be refreshed as it may have been updated - fn enable(&self, jenkins_client: &Jenkins) -> Result<(), Error> { + fn enable(&self, jenkins_client: &Jenkins) -> Result<(), Box> { let path = jenkins_client.url_to_path(&self.url()); if let Path::Job { name, @@ -153,7 +152,7 @@ pub trait Job { } /// Disable a `Job`. It may need to be refreshed as it may have been updated - fn disable(&self, jenkins_client: &Jenkins) -> Result<(), Error> { + fn disable(&self, jenkins_client: &Jenkins) -> Result<(), Box> { let path = jenkins_client.url_to_path(&self.url()); if let Path::Job { name, @@ -172,7 +171,11 @@ pub trait Job { } /// Add this job to the view `view_name` - fn add_to_view<'a, V>(&self, jenkins_client: &Jenkins, view_name: V) -> Result<(), Error> + fn add_to_view<'a, V>( + &self, + jenkins_client: &Jenkins, + view_name: V, + ) -> Result<(), Box> where V: Into>, { @@ -197,7 +200,11 @@ pub trait Job { } /// Remove this job from the view `view_name` - fn remove_from_view<'a, V>(&self, jenkins_client: &Jenkins, view_name: V) -> Result<(), Error> + fn remove_from_view<'a, V>( + &self, + jenkins_client: &Jenkins, + view_name: V, + ) -> Result<(), Box> where V: Into>, { @@ -363,7 +370,10 @@ impl CommonJob {} /// Common trait for jobs that can be build pub trait BuildableJob: Job + Sized { /// Build this job - fn build(&self, jenkins_client: &Jenkins) -> Result { + fn build( + &self, + jenkins_client: &Jenkins, + ) -> Result> { self.builder(jenkins_client)?.send() } @@ -371,7 +381,7 @@ pub trait BuildableJob: Job + Sized { fn builder<'a, 'b, 'c, 'd>( &'a self, jenkins_client: &'b Jenkins, - ) -> Result, Error> { + ) -> Result, Box> { JobBuilder::new(self, jenkins_client) } } @@ -379,7 +389,7 @@ pub trait BuildableJob: Job + Sized { /// Common trait for jobs that can poll a SCM pub trait SCMPollable: Job + Sized { /// Poll configured SCM for changes - fn poll_scm(&self, jenkins_client: &Jenkins) -> Result<(), Error> { + fn poll_scm(&self, jenkins_client: &Jenkins) -> Result<(), Box> { let path = jenkins_client.url_to_path(&self.url()); if let Path::Job { name, diff --git a/src/job/mod.rs b/src/job/mod.rs index 6b11ea97..edda50df 100644 --- a/src/job/mod.rs +++ b/src/job/mod.rs @@ -1,7 +1,5 @@ //! Jenkins Jobs -use failure::Error; - use crate::client_internals::{Name, Path}; use crate::queue::ShortQueueItem; use crate::Jenkins; @@ -31,7 +29,7 @@ pub use self::external::ExternalJob; impl Jenkins { /// Get a `Job` from it's `job_name` - pub fn get_job<'a, J>(&self, job_name: J) -> Result + pub fn get_job<'a, J>(&self, job_name: J) -> Result> where J: Into>, { @@ -45,7 +43,7 @@ impl Jenkins { } /// Build a `Job` from it's `job_name` - pub fn build_job<'a, J>(&self, job_name: J) -> Result + pub fn build_job<'a, J>(&self, job_name: J) -> Result> where J: Into>, { @@ -56,12 +54,12 @@ impl Jenkins { pub fn job_builder<'a, 'b, 'c, 'd>( &'b self, job_name: &'a str, - ) -> Result, Error> { + ) -> Result, Box> { JobBuilder::new_from_job_name(job_name, self) } /// Poll SCM of a `Job` from it's `job_name` - pub fn poll_scm_job<'a, J>(&self, job_name: J) -> Result<(), Error> + pub fn poll_scm_job<'a, J>(&self, job_name: J) -> Result<(), Box> where J: Into>, { diff --git a/src/nodes/mod.rs b/src/nodes/mod.rs index 3f9f57dd..9fa4d412 100644 --- a/src/nodes/mod.rs +++ b/src/nodes/mod.rs @@ -1,6 +1,5 @@ //! Jenkins Slaves Informations -use failure::Error; use serde::{Deserialize, Serialize}; use crate::client_internals::{Name, Path}; @@ -26,12 +25,15 @@ pub struct ComputerSet { impl Jenkins { /// Get a `ComputerSet` - pub fn get_nodes(&self) -> Result { + pub fn get_nodes(&self) -> Result> { Ok(self.get(&Path::Computers)?.json()?) } /// Get a `Computer` - pub fn get_node<'a, C>(&self, computer_name: C) -> Result + pub fn get_node<'a, C>( + &self, + computer_name: C, + ) -> Result> where C: Into>, { @@ -43,7 +45,7 @@ impl Jenkins { } /// Get the master `Computer` - pub fn get_master_node(&self) -> Result { + pub fn get_master_node(&self) -> Result> { Ok(self .get(&Path::Computer { name: Name::Name("(master)"), diff --git a/src/queue.rs b/src/queue.rs index d0dec7ef..650a58a4 100644 --- a/src/queue.rs +++ b/src/queue.rs @@ -1,6 +1,5 @@ //! Jenkins build queue -use failure::Error; use serde::{Deserialize, Serialize}; use serde_json; @@ -21,7 +20,10 @@ pub struct ShortQueueItem { } impl ShortQueueItem { /// Get the full details of a `QueueItem` matching the `ShortQueueItem` - pub fn get_full_queue_item(&self, jenkins_client: &Jenkins) -> Result { + pub fn get_full_queue_item( + &self, + jenkins_client: &Jenkins, + ) -> Result> { let path = jenkins_client.url_to_path(&self.url); if let Path::QueueItem { .. } = path { Ok(jenkins_client.get(&path)?.json()?) @@ -68,7 +70,10 @@ pub struct QueueItem { } impl QueueItem { /// Refresh a `QueueItem`, consuming the existing one and returning a new `QueueItem` - pub fn refresh_item(self, jenkins_client: &Jenkins) -> Result { + pub fn refresh_item( + self, + jenkins_client: &Jenkins, + ) -> Result> { let path = jenkins_client.url_to_path(&self.url); if let Path::QueueItem { .. } = path { Ok(jenkins_client.get(&path)?.json()?) @@ -92,12 +97,12 @@ pub struct Queue { impl Jenkins { /// Get the Jenkins items queue - pub fn get_queue(&self) -> Result { + pub fn get_queue(&self) -> Result> { Ok(self.get(&Path::Queue)?.json()?) } /// Get a queue item from it's ID - pub fn get_queue_item(&self, id: i32) -> Result { + pub fn get_queue_item(&self, id: i32) -> Result> { Ok(self.get(&Path::QueueItem { id })?.json()?) } } diff --git a/src/view.rs b/src/view.rs index b6a7911e..d1595f39 100644 --- a/src/view.rs +++ b/src/view.rs @@ -1,6 +1,5 @@ //! Jenkins Views, use to group Jobs -use failure::Error; use serde::{self, Deserialize, Serialize}; use serde_json; @@ -26,7 +25,10 @@ pub struct ShortView { impl ShortView { /// Get the full details of a `View` matching the `ShortView` - pub fn get_full_view(&self, jenkins_client: &Jenkins) -> Result { + pub fn get_full_view( + &self, + jenkins_client: &Jenkins, + ) -> Result> { let path = jenkins_client.url_to_path(&self.url); if let Path::View { .. } = path { Ok(jenkins_client.get(&path)?.json()?) @@ -121,7 +123,11 @@ impl View for ListView { impl ListView { /// Add the job `job_name` to this view - pub fn add_job<'a, J>(&self, jenkins_client: &Jenkins, job_name: J) -> Result<(), Error> + pub fn add_job<'a, J>( + &self, + jenkins_client: &Jenkins, + job_name: J, + ) -> Result<(), Box> where J: Into>, { @@ -142,7 +148,11 @@ impl ListView { } /// Remove the job `job_name` from this view - pub fn remove_job<'a, J>(&self, jenkins_client: &Jenkins, job_name: J) -> Result<(), Error> + pub fn remove_job<'a, J>( + &self, + jenkins_client: &Jenkins, + job_name: J, + ) -> Result<(), Box> where J: Into>, { @@ -165,7 +175,7 @@ impl ListView { impl Jenkins { /// Get a `View` - pub fn get_view<'a, V>(&self, view_name: V) -> Result + pub fn get_view<'a, V>(&self, view_name: V) -> Result> where V: Into>, { @@ -177,7 +187,11 @@ impl Jenkins { } /// Add the job `job_name` to the view `view_name` - pub fn add_job_to_view<'a, 'b, V, J>(&self, view_name: V, job_name: J) -> Result<(), Error> + pub fn add_job_to_view<'a, 'b, V, J>( + &self, + view_name: V, + job_name: J, + ) -> Result<(), Box> where V: Into>, J: Into>, @@ -190,7 +204,11 @@ impl Jenkins { } /// Remove the job `job_name` from the view `view_name` - pub fn remove_job_from_view<'a, 'b, V, J>(&self, view_name: V, job_name: J) -> Result<(), Error> + pub fn remove_job_from_view<'a, 'b, V, J>( + &self, + view_name: V, + job_name: J, + ) -> Result<(), Box> where V: Into>, J: Into>, diff --git a/tests/client.rs b/tests/client.rs index 452183f0..0effa1f5 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -14,9 +14,9 @@ use jenkins_api::job::{BuildableJob, Job, SCMPollable}; use jenkins_api::JenkinsBuilder; use std::{thread, time}; -use std::sync::{Once, ONCE_INIT}; +use std::sync::Once; -static INIT: Once = ONCE_INIT; +static INIT: Once = Once::new(); fn setup() { INIT.call_once(|| { diff --git a/tests/propertied.rs b/tests/propertied.rs index b7974ae5..54994752 100644 --- a/tests/propertied.rs +++ b/tests/propertied.rs @@ -7,9 +7,9 @@ extern crate jenkins_api; use jenkins_api::JenkinsBuilder; -use std::sync::{Once, ONCE_INIT}; +use std::sync::Once; -static INIT: Once = ONCE_INIT; +static INIT: Once = Once::new(); fn setup() { INIT.call_once(|| { From 61f4d334917c5f413c4ac23b9960a299af1344cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Tue, 5 Nov 2019 03:43:13 +0100 Subject: [PATCH 2/6] :art: format --- src/job/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/job/mod.rs b/src/job/mod.rs index edda50df..afd807d7 100644 --- a/src/job/mod.rs +++ b/src/job/mod.rs @@ -43,7 +43,10 @@ impl Jenkins { } /// Build a `Job` from it's `job_name` - pub fn build_job<'a, J>(&self, job_name: J) -> Result> + pub fn build_job<'a, J>( + &self, + job_name: J, + ) -> Result> where J: Into>, { From cf69f7a403c295dfde5e8401933c42251cbfeccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Tue, 5 Nov 2019 04:04:49 +0100 Subject: [PATCH 3/6] :pencil: remove failure crate from doc --- src/client.rs | 1 - src/lib.rs | 17 ----------------- 2 files changed, 18 deletions(-) diff --git a/src/client.rs b/src/client.rs index b8607dfe..9dbd841c 100644 --- a/src/client.rs +++ b/src/client.rs @@ -117,7 +117,6 @@ impl super::Jenkins { /// # Example /// /// ```rust - /// # extern crate failure; /// # #[macro_use] /// # extern crate serde; /// # diff --git a/src/lib.rs b/src/lib.rs index a02239a8..23e26f6b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,7 +20,6 @@ //! # Example //! //! ```rust -//! extern crate failure; //! //! extern crate jenkins_api; //! @@ -43,22 +42,6 @@ //! ``` //! -// extern crate reqwest; - -// #[macro_use] -// use serde::{Serialize, Deserialize, self}; -// extern crate serde_json; -// extern crate serde_urlencoded; - -// extern crate urlencoding; - -// #[macro_use] -// extern crate failure; -// extern crate regex; - -// #[macro_use] -// extern crate log; - mod client_internals; pub use crate::client_internals::{Jenkins, JenkinsBuilder}; pub mod client; From 1dc321971c674d3aae8b1eb04d2b056e801a7a4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Thu, 7 Nov 2019 21:14:23 +0100 Subject: [PATCH 4/6] :art: use type alias for Result --- src/action/maven.rs | 4 ++-- src/build/common.rs | 14 ++++---------- src/build/mod.rs | 7 ++----- src/client.rs | 8 ++------ src/client_internals/builder.rs | 3 ++- src/client_internals/csrf.rs | 5 +++-- src/client_internals/errors.rs | 3 +++ src/client_internals/mod.rs | 21 +++++++-------------- src/helpers.rs | 3 ++- src/home.rs | 4 ++-- src/job/builder.rs | 21 ++++++--------------- src/job/common.rs | 29 +++++++++-------------------- src/job/mod.rs | 13 +++++-------- src/nodes/mod.rs | 11 ++++------- src/queue.rs | 16 +++++----------- src/view.rs | 33 +++++++-------------------------- 16 files changed, 65 insertions(+), 130 deletions(-) diff --git a/src/action/maven.rs b/src/action/maven.rs index e0f8b81e..0046e526 100644 --- a/src/action/maven.rs +++ b/src/action/maven.rs @@ -2,7 +2,7 @@ use serde::Deserialize; -use crate::client; +use crate::client::{self, Result}; use crate::client_internals::path::Path; use crate::Jenkins; @@ -41,7 +41,7 @@ impl ShortMavenArtifactRecord { pub fn get_full_artifact_record( &self, jenkins_client: &Jenkins, - ) -> Result> { + ) -> Result { let path = jenkins_client.url_to_path(&self.url); if let Path::MavenArtifactRecord { .. } = path { Ok(jenkins_client.get(&path)?.json()?) diff --git a/src/build/common.rs b/src/build/common.rs index d9064235..b1a078fa 100644 --- a/src/build/common.rs +++ b/src/build/common.rs @@ -6,7 +6,7 @@ use serde_json; use crate::helpers::Class; use crate::action::CommonAction; -use crate::client; +use crate::client::{self, Result}; use crate::client_internals::path::Path; use crate::job::{CommonJob, Job}; use crate::Jenkins; @@ -35,10 +35,7 @@ where for<'de> T: Deserialize<'de>, { /// Get the full details of a `Build` matching the `ShortBuild` - pub fn get_full_build( - &self, - jenkins_client: &Jenkins, - ) -> Result> { + pub fn get_full_build(&self, jenkins_client: &Jenkins) -> Result { let path = jenkins_client.url_to_path(&self.url); if let Path::Build { .. } = path { Ok(jenkins_client.get(&path)?.json()?) @@ -167,10 +164,7 @@ pub trait Build { fn url(&self) -> &str; /// Get the `Job` from a `Build` - fn get_job( - &self, - jenkins_client: &Jenkins, - ) -> Result> + fn get_job(&self, jenkins_client: &Jenkins) -> Result where for<'de> Self::ParentJob: Deserialize<'de>, { @@ -197,7 +191,7 @@ pub trait Build { } /// Get the console output from a `Build` - fn get_console(&self, jenkins_client: &Jenkins) -> Result> { + fn get_console(&self, jenkins_client: &Jenkins) -> Result { let path = jenkins_client.url_to_path(&self.url()); if let Path::Build { job_name, diff --git a/src/build/mod.rs b/src/build/mod.rs index b27c4a03..9b6db4b5 100644 --- a/src/build/mod.rs +++ b/src/build/mod.rs @@ -1,5 +1,6 @@ //! Jenkins Builds +use crate::client::Result; use crate::client_internals::path::{Name, Path}; use crate::job::JobName; use crate::Jenkins; @@ -22,11 +23,7 @@ pub use self::multijob::MultiJobBuild; impl Jenkins { /// Get a build from a `job_name` and `build_number` - pub fn get_build<'a, J, B>( - &self, - job_name: J, - build_number: B, - ) -> Result> + pub fn get_build<'a, J, B>(&self, job_name: J, build_number: B) -> Result where J: Into>, B: Into, diff --git a/src/client.rs b/src/client.rs index 9dbd841c..9e4613e3 100644 --- a/src/client.rs +++ b/src/client.rs @@ -7,7 +7,7 @@ use crate::client_internals::InternalAdvancedQueryParams; // pub use client_internals::path::Name; pub use crate::client_internals::AdvancedQuery; -pub use crate::client_internals::{error, Error}; +pub use crate::client_internals::{error, Error, Result}; pub use crate::client_internals::{TreeBuilder, TreeQueryParam}; use crate::build; @@ -161,11 +161,7 @@ impl super::Jenkins { /// # } /// ``` /// - pub fn get_object_as( - &self, - object: Path, - parameters: Q, - ) -> Result> + pub fn get_object_as(&self, object: Path, parameters: Q) -> Result where Q: Into>, for<'de> T: Deserialize<'de>, diff --git a/src/client_internals/builder.rs b/src/client_internals/builder.rs index ce353d6b..2cf51c19 100644 --- a/src/client_internals/builder.rs +++ b/src/client_internals/builder.rs @@ -3,6 +3,7 @@ use std::str::FromStr; use reqwest::{self, Client, Url}; use super::{Jenkins, User}; +use crate::client::Result; /// Builder for Jenkins client /// @@ -44,7 +45,7 @@ impl JenkinsBuilder { } /// Build the Jenkins client - pub fn build(self) -> Result> { + pub fn build(self) -> Result { let url = Url::from_str(&self.url)?; if url.cannot_be_a_base() { return Err(reqwest::UrlError::RelativeUrlWithoutBase.into()); diff --git a/src/client_internals/csrf.rs b/src/client_internals/csrf.rs index 6dc84100..ed04bef1 100644 --- a/src/client_internals/csrf.rs +++ b/src/client_internals/csrf.rs @@ -2,6 +2,7 @@ use reqwest::{header::HeaderName, header::HeaderValue, RequestBuilder}; use serde::Deserialize; use super::{path::Path, Jenkins}; +use crate::client::Result; #[derive(Debug, Deserialize, Clone)] #[serde(rename_all = "camelCase")] @@ -14,7 +15,7 @@ impl Jenkins { pub(crate) fn add_csrf_to_request( &self, request_builder: RequestBuilder, - ) -> Result> { + ) -> Result { if self.csrf_enabled { let crumb = self.get_csrf()?; Ok(request_builder.header( @@ -26,7 +27,7 @@ impl Jenkins { } } - pub(crate) fn get_csrf(&self) -> Result> { + pub(crate) fn get_csrf(&self) -> Result { let crumb: Crumb = self.get(&Path::CrumbIssuer)?.json()?; Ok(crumb) } diff --git a/src/client_internals/errors.rs b/src/client_internals/errors.rs index 32337744..db8ac99b 100644 --- a/src/client_internals/errors.rs +++ b/src/client_internals/errors.rs @@ -1,5 +1,8 @@ use std::fmt; +/// Wrapper `Result` type +pub type Result = std::result::Result>; + /// Errors that can be thrown #[derive(Debug)] pub enum Error { diff --git a/src/client_internals/mod.rs b/src/client_internals/mod.rs index e687186c..ceb367f8 100644 --- a/src/client_internals/mod.rs +++ b/src/client_internals/mod.rs @@ -11,7 +11,7 @@ use reqwest::{ use serde::Serialize; mod errors; -pub use self::errors::Error; +pub use self::errors::{Error, Result}; mod builder; pub mod path; pub use self::builder::JenkinsBuilder; @@ -83,10 +83,7 @@ impl Jenkins { format!("{}{}", self.url, endpoint) } - fn send( - &self, - mut request_builder: RequestBuilder, - ) -> Result> { + fn send(&self, mut request_builder: RequestBuilder) -> Result { if let Some(ref user) = self.user { request_builder = request_builder.basic_auth(user.username.clone(), user.password.clone()); @@ -96,7 +93,7 @@ impl Jenkins { Ok(self.client.execute(query)?) } - fn error_for_status(response: Response) -> Result> { + fn error_for_status(response: Response) -> Result { let status = response.status(); if status.is_client_error() || status.is_server_error() { warn!("got an error: {}", status); @@ -104,15 +101,11 @@ impl Jenkins { Ok(response.error_for_status()?) } - pub(crate) fn get(&self, path: &Path) -> Result> { + pub(crate) fn get(&self, path: &Path) -> Result { self.get_with_params(path, &[("depth", &self.depth.to_string())]) } - pub(crate) fn get_with_params( - &self, - path: &Path, - qps: T, - ) -> Result> { + pub(crate) fn get_with_params(&self, path: &Path, qps: T) -> Result { let query = self .client .get(&self.url_api_json(&path.to_string())) @@ -120,7 +113,7 @@ impl Jenkins { Ok(Self::error_for_status(self.send(query)?)?) } - pub(crate) fn post(&self, path: &Path) -> Result> { + pub(crate) fn post(&self, path: &Path) -> Result { let mut request_builder = self.client.post(&self.url(&path.to_string())); request_builder = self.add_csrf_to_request(request_builder)?; @@ -133,7 +126,7 @@ impl Jenkins { path: &Path, body: T, qps: &[(&str, &str)], - ) -> Result> { + ) -> Result { let mut request_builder = self.client.post(&self.url(&path.to_string())); request_builder = self.add_csrf_to_request(request_builder)?; diff --git a/src/helpers.rs b/src/helpers.rs index 9f9838dc..b3694f77 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -20,7 +20,8 @@ macro_rules! specialize { ($common:ty => $trait:path) => { impl $common { #[doc = "Read the object as one of it's specialization implementing $trait"] - pub fn as_variant(&self) -> Result + #[allow(unused_qualifications)] + pub fn as_variant(&self) -> std::result::Result where for<'de> T: Deserialize<'de>, { diff --git a/src/home.rs b/src/home.rs index a3fb33d2..71bb2de2 100644 --- a/src/home.rs +++ b/src/home.rs @@ -2,7 +2,7 @@ use serde::Deserialize; -use crate::client_internals::Path; +use crate::client_internals::{Path, Result}; use crate::job::ShortJob; use crate::view::ShortView; use crate::Jenkins; @@ -47,7 +47,7 @@ pub struct Home { impl Jenkins { /// Get Jenkins `Home` - pub fn get_home(&self) -> Result> { + pub fn get_home(&self) -> Result { Ok(self.get(&Path::Home)?.json()?) } } diff --git a/src/job/builder.rs b/src/job/builder.rs index e29505e7..1c45ac9d 100644 --- a/src/job/builder.rs +++ b/src/job/builder.rs @@ -5,7 +5,7 @@ use reqwest::header::LOCATION; use serde::{self, Serialize}; use serde_urlencoded; -use crate::client; +use crate::client::{self, Result}; use crate::client_internals::{Name, Path}; use crate::job::{Job, JobName}; use crate::queue::ShortQueueItem; @@ -24,10 +24,7 @@ pub struct JobBuilder<'a, 'b, 'c, 'd> { impl<'a, 'b, 'c, 'd> JobBuilder<'a, 'b, 'c, 'd> { #[allow(clippy::new_ret_no_self)] - pub(crate) fn new( - job: &'a T, - jenkins_client: &'b Jenkins, - ) -> Result> + pub(crate) fn new(job: &'a T, jenkins_client: &'b Jenkins) -> Result where T: Job, { @@ -54,10 +51,7 @@ impl<'a, 'b, 'c, 'd> JobBuilder<'a, 'b, 'c, 'd> { } } - pub(crate) fn new_from_job_name( - name: J, - jenkins_client: &'b Jenkins, - ) -> Result> + pub(crate) fn new_from_job_name(name: J, jenkins_client: &'b Jenkins) -> Result where J: Into>, { @@ -72,7 +66,7 @@ impl<'a, 'b, 'c, 'd> JobBuilder<'a, 'b, 'c, 'd> { } /// Trigger the build - pub fn send(self) -> Result> { + pub fn send(self) -> Result { let response = match (self.token, self.parameters) { (Some(token), None) => { let bound_cause = self.cause.unwrap_or(""); @@ -150,7 +144,7 @@ impl<'a, 'b, 'c, 'd> JobBuilder<'a, 'b, 'c, 'd> { mut self, token: &'d str, cause: Option<&'c str>, - ) -> Result> { + ) -> Result { if self.parameters.is_some() { return Err(client::Error::UnsupportedBuildConfiguration.into()); } @@ -173,10 +167,7 @@ impl<'a, 'b, 'c, 'd> JobBuilder<'a, 'b, 'c, 'd> { /// /// This methods will return an error if serializing `parameters` fails, or if passing /// parameters to a remote build. - pub fn with_parameters( - mut self, - parameters: &T, - ) -> Result> { + pub fn with_parameters(mut self, parameters: &T) -> Result { if self.token.is_some() { return Err(client::Error::UnsupportedBuildConfiguration.into()); } diff --git a/src/job/common.rs b/src/job/common.rs index 34dab0be..a215fc1b 100644 --- a/src/job/common.rs +++ b/src/job/common.rs @@ -8,7 +8,7 @@ use crate::helpers::Class; use super::JobBuilder; use crate::action::CommonAction; use crate::build::{CommonBuild, ShortBuild}; -use crate::client; +use crate::client::{self, Result}; use crate::client_internals::{Name, Path}; use crate::property::CommonProperty; use crate::queue::ShortQueueItem; @@ -87,7 +87,7 @@ where for<'de> T: Deserialize<'de>, { /// Get the full details of a `Job` matching the `ShortJob` - pub fn get_full_job(&self, jenkins_client: &Jenkins) -> Result> { + pub fn get_full_job(&self, jenkins_client: &Jenkins) -> Result { let path = jenkins_client.url_to_path(&self.url); if let Path::Job { .. } = path { Ok(jenkins_client.get(&path)?.json()?) @@ -133,7 +133,7 @@ pub trait Job { fn name(&self) -> &str; /// Enable a `Job`. It may need to be refreshed as it may have been updated - fn enable(&self, jenkins_client: &Jenkins) -> Result<(), Box> { + fn enable(&self, jenkins_client: &Jenkins) -> Result<()> { let path = jenkins_client.url_to_path(&self.url()); if let Path::Job { name, @@ -152,7 +152,7 @@ pub trait Job { } /// Disable a `Job`. It may need to be refreshed as it may have been updated - fn disable(&self, jenkins_client: &Jenkins) -> Result<(), Box> { + fn disable(&self, jenkins_client: &Jenkins) -> Result<()> { let path = jenkins_client.url_to_path(&self.url()); if let Path::Job { name, @@ -171,11 +171,7 @@ pub trait Job { } /// Add this job to the view `view_name` - fn add_to_view<'a, V>( - &self, - jenkins_client: &Jenkins, - view_name: V, - ) -> Result<(), Box> + fn add_to_view<'a, V>(&self, jenkins_client: &Jenkins, view_name: V) -> Result<()> where V: Into>, { @@ -200,11 +196,7 @@ pub trait Job { } /// Remove this job from the view `view_name` - fn remove_from_view<'a, V>( - &self, - jenkins_client: &Jenkins, - view_name: V, - ) -> Result<(), Box> + fn remove_from_view<'a, V>(&self, jenkins_client: &Jenkins, view_name: V) -> Result<()> where V: Into>, { @@ -370,10 +362,7 @@ impl CommonJob {} /// Common trait for jobs that can be build pub trait BuildableJob: Job + Sized { /// Build this job - fn build( - &self, - jenkins_client: &Jenkins, - ) -> Result> { + fn build(&self, jenkins_client: &Jenkins) -> Result { self.builder(jenkins_client)?.send() } @@ -381,7 +370,7 @@ pub trait BuildableJob: Job + Sized { fn builder<'a, 'b, 'c, 'd>( &'a self, jenkins_client: &'b Jenkins, - ) -> Result, Box> { + ) -> Result> { JobBuilder::new(self, jenkins_client) } } @@ -389,7 +378,7 @@ pub trait BuildableJob: Job + Sized { /// Common trait for jobs that can poll a SCM pub trait SCMPollable: Job + Sized { /// Poll configured SCM for changes - fn poll_scm(&self, jenkins_client: &Jenkins) -> Result<(), Box> { + fn poll_scm(&self, jenkins_client: &Jenkins) -> Result<()> { let path = jenkins_client.url_to_path(&self.url()); if let Path::Job { name, diff --git a/src/job/mod.rs b/src/job/mod.rs index afd807d7..afa9a393 100644 --- a/src/job/mod.rs +++ b/src/job/mod.rs @@ -1,6 +1,6 @@ //! Jenkins Jobs -use crate::client_internals::{Name, Path}; +use crate::client_internals::{Name, Path, Result}; use crate::queue::ShortQueueItem; use crate::Jenkins; @@ -29,7 +29,7 @@ pub use self::external::ExternalJob; impl Jenkins { /// Get a `Job` from it's `job_name` - pub fn get_job<'a, J>(&self, job_name: J) -> Result> + pub fn get_job<'a, J>(&self, job_name: J) -> Result where J: Into>, { @@ -43,10 +43,7 @@ impl Jenkins { } /// Build a `Job` from it's `job_name` - pub fn build_job<'a, J>( - &self, - job_name: J, - ) -> Result> + pub fn build_job<'a, J>(&self, job_name: J) -> Result where J: Into>, { @@ -57,12 +54,12 @@ impl Jenkins { pub fn job_builder<'a, 'b, 'c, 'd>( &'b self, job_name: &'a str, - ) -> Result, Box> { + ) -> Result> { JobBuilder::new_from_job_name(job_name, self) } /// Poll SCM of a `Job` from it's `job_name` - pub fn poll_scm_job<'a, J>(&self, job_name: J) -> Result<(), Box> + pub fn poll_scm_job<'a, J>(&self, job_name: J) -> Result<()> where J: Into>, { diff --git a/src/nodes/mod.rs b/src/nodes/mod.rs index 9fa4d412..95f7942d 100644 --- a/src/nodes/mod.rs +++ b/src/nodes/mod.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; -use crate::client_internals::{Name, Path}; +use crate::client_internals::{Name, Path, Result}; use crate::Jenkins; pub mod computer; @@ -25,15 +25,12 @@ pub struct ComputerSet { impl Jenkins { /// Get a `ComputerSet` - pub fn get_nodes(&self) -> Result> { + pub fn get_nodes(&self) -> Result { Ok(self.get(&Path::Computers)?.json()?) } /// Get a `Computer` - pub fn get_node<'a, C>( - &self, - computer_name: C, - ) -> Result> + pub fn get_node<'a, C>(&self, computer_name: C) -> Result where C: Into>, { @@ -45,7 +42,7 @@ impl Jenkins { } /// Get the master `Computer` - pub fn get_master_node(&self) -> Result> { + pub fn get_master_node(&self) -> Result { Ok(self .get(&Path::Computer { name: Name::Name("(master)"), diff --git a/src/queue.rs b/src/queue.rs index 650a58a4..aa19bf2a 100644 --- a/src/queue.rs +++ b/src/queue.rs @@ -5,7 +5,7 @@ use serde_json; use crate::action::CommonAction; use crate::build::ShortBuild; -use crate::client; +use crate::client::{self, Result}; use crate::client_internals::Path; use crate::job::ShortJob; use crate::Jenkins; @@ -20,10 +20,7 @@ pub struct ShortQueueItem { } impl ShortQueueItem { /// Get the full details of a `QueueItem` matching the `ShortQueueItem` - pub fn get_full_queue_item( - &self, - jenkins_client: &Jenkins, - ) -> Result> { + pub fn get_full_queue_item(&self, jenkins_client: &Jenkins) -> Result { let path = jenkins_client.url_to_path(&self.url); if let Path::QueueItem { .. } = path { Ok(jenkins_client.get(&path)?.json()?) @@ -70,10 +67,7 @@ pub struct QueueItem { } impl QueueItem { /// Refresh a `QueueItem`, consuming the existing one and returning a new `QueueItem` - pub fn refresh_item( - self, - jenkins_client: &Jenkins, - ) -> Result> { + pub fn refresh_item(self, jenkins_client: &Jenkins) -> Result { let path = jenkins_client.url_to_path(&self.url); if let Path::QueueItem { .. } = path { Ok(jenkins_client.get(&path)?.json()?) @@ -97,12 +91,12 @@ pub struct Queue { impl Jenkins { /// Get the Jenkins items queue - pub fn get_queue(&self) -> Result> { + pub fn get_queue(&self) -> Result { Ok(self.get(&Path::Queue)?.json()?) } /// Get a queue item from it's ID - pub fn get_queue_item(&self, id: i32) -> Result> { + pub fn get_queue_item(&self, id: i32) -> Result { Ok(self.get(&Path::QueueItem { id })?.json()?) } } diff --git a/src/view.rs b/src/view.rs index d1595f39..6d6a116f 100644 --- a/src/view.rs +++ b/src/view.rs @@ -5,7 +5,7 @@ use serde_json; use crate::helpers::Class; -use crate::client; +use crate::client::{self, Result}; use crate::client_internals::{Name, Path}; use crate::job::{JobName, ShortJob}; use crate::property::CommonProperty; @@ -25,10 +25,7 @@ pub struct ShortView { impl ShortView { /// Get the full details of a `View` matching the `ShortView` - pub fn get_full_view( - &self, - jenkins_client: &Jenkins, - ) -> Result> { + pub fn get_full_view(&self, jenkins_client: &Jenkins) -> Result { let path = jenkins_client.url_to_path(&self.url); if let Path::View { .. } = path { Ok(jenkins_client.get(&path)?.json()?) @@ -123,11 +120,7 @@ impl View for ListView { impl ListView { /// Add the job `job_name` to this view - pub fn add_job<'a, J>( - &self, - jenkins_client: &Jenkins, - job_name: J, - ) -> Result<(), Box> + pub fn add_job<'a, J>(&self, jenkins_client: &Jenkins, job_name: J) -> Result<()> where J: Into>, { @@ -148,11 +141,7 @@ impl ListView { } /// Remove the job `job_name` from this view - pub fn remove_job<'a, J>( - &self, - jenkins_client: &Jenkins, - job_name: J, - ) -> Result<(), Box> + pub fn remove_job<'a, J>(&self, jenkins_client: &Jenkins, job_name: J) -> Result<()> where J: Into>, { @@ -175,7 +164,7 @@ impl ListView { impl Jenkins { /// Get a `View` - pub fn get_view<'a, V>(&self, view_name: V) -> Result> + pub fn get_view<'a, V>(&self, view_name: V) -> Result where V: Into>, { @@ -187,11 +176,7 @@ impl Jenkins { } /// Add the job `job_name` to the view `view_name` - pub fn add_job_to_view<'a, 'b, V, J>( - &self, - view_name: V, - job_name: J, - ) -> Result<(), Box> + pub fn add_job_to_view<'a, 'b, V, J>(&self, view_name: V, job_name: J) -> Result<()> where V: Into>, J: Into>, @@ -204,11 +189,7 @@ impl Jenkins { } /// Remove the job `job_name` from the view `view_name` - pub fn remove_job_from_view<'a, 'b, V, J>( - &self, - view_name: V, - job_name: J, - ) -> Result<(), Box> + pub fn remove_job_from_view<'a, 'b, V, J>(&self, view_name: V, job_name: J) -> Result<()> where V: Into>, J: Into>, From 85fcc282b9a4482083baa615ff1515933e9cb300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Thu, 7 Nov 2019 21:37:02 +0100 Subject: [PATCH 5/6] :pencil: remove failure from doc examples too --- src/client.rs | 2 +- src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client.rs b/src/client.rs index 9e4613e3..128bc28d 100644 --- a/src/client.rs +++ b/src/client.rs @@ -138,7 +138,7 @@ impl super::Jenkins { /// last_build: LastBuild, /// } /// - /// # fn main() -> Result<(), failure::Error> { + /// # fn main() -> Result<(), Box> { /// # let jenkins = JenkinsBuilder::new("http://localhost:8080") /// # .with_user("user", Some("password")) /// # .build()?; diff --git a/src/lib.rs b/src/lib.rs index 23e26f6b..b756be5f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,7 @@ //! //! use jenkins_api::JenkinsBuilder; //! -//! fn main() -> Result<(), failure::Error> { +//! fn main() -> Result<(), Box> { //! let jenkins = JenkinsBuilder::new("http://localhost:8080") //! .with_user("user", Some("password")) //! .build()?; From a45d449af4c68b5accfc6939e6f67e4a03bd880c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Wed, 13 Nov 2019 21:13:32 +0100 Subject: [PATCH 6/6] :heavy_plus_sign: using thiserror to manage error type --- Cargo.toml | 4 +--- src/client_internals/errors.rs | 40 +++++++++------------------------- 2 files changed, 11 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6be737f6..1ee8be80 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,12 +21,10 @@ reqwest = "0.9" serde = { version = "1.0", features = [ "derive" ] } serde_json = "1.0" serde_urlencoded = "0.6" - urlencoding = "1.0.0" - regex = "1.3" - log = "0.4" +thiserror = "1.0" [build-dependencies] skeptic = "0.13" diff --git a/src/client_internals/errors.rs b/src/client_internals/errors.rs index db8ac99b..fd4af709 100644 --- a/src/client_internals/errors.rs +++ b/src/client_internals/errors.rs @@ -1,11 +1,14 @@ use std::fmt; +use thiserror::Error; + /// Wrapper `Result` type pub type Result = std::result::Result>; /// Errors that can be thrown -#[derive(Debug)] +#[derive(Debug, Error)] pub enum Error { + #[error("invalid url for {expected}: {url}")] /// Error thrown when a link between objects has an unexpected format InvalidUrl { /// URL found @@ -14,26 +17,32 @@ pub enum Error { expected: ExpectedType, }, + #[error("invalid crumbfield '{field_name}', expected 'Jenkins-Crumb'")] /// Error thrown when CSRF protection use an unexpected field name InvalidCrumbFieldName { /// Field name provided by Jenkins api for crumb field_name: String, }, + #[error("illegal argument: '{message}'")] /// Error thrown when building a parameterized job with an invalid parameter IllegalArgument { /// Exception message provided by Jenkins message: String, }, + + #[error("illegal state: '{message}'")] /// Error thrown when building a job with invalid parameters IllegalState { /// Exception message provided by Jenkins message: String, }, + #[error("can't build a job remotely with parameters")] /// Error when trying to remotely build a job with parameters UnsupportedBuildConfiguration, + #[error("can't do '{action}' on a {object_type} of type {variant_name}")] /// Error when trying to do an action on an object not supporting it InvalidObjectType { /// Object type @@ -44,35 +53,6 @@ pub enum Error { action: Action, }, } -impl std::error::Error for Error {} -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - Error::InvalidUrl { url, expected } => { - write!(f, "invalid url for {}: {}", expected, url) - } - Error::InvalidCrumbFieldName { field_name } => write!( - f, - "invalid crumbfield '{}', expected 'Jenkins-Crumb'", - field_name - ), - Error::IllegalArgument { message } => write!(f, "illegal argument: '{}'", message), - Error::IllegalState { message } => write!(f, "illegal state: '{}'", message), - Error::UnsupportedBuildConfiguration => { - write!(f, "can't build a job remotely with parameters") - } - Error::InvalidObjectType { - object_type, - variant_name, - action, - } => write!( - f, - "can't do '{}' on a {} of type {}", - action, object_type, variant_name - ), - } - } -} /// Possible type of URL expected in links between items #[derive(Debug, Copy, Clone)]