Skip to content

Commit

Permalink
chore: Box the github error
Browse files Browse the repository at this point in the history
Clippy complains about this because the GithubError is relatively large.
This makes that smaller by boxing the error source.

See: <https://rust-lang.github.io/rust-clippy/master/index.html#result_large_err>
  • Loading branch information
joshka committed Dec 21, 2024
1 parent aeda150 commit 251c15b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/api/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ impl<'octo> UserHandler<'octo> {
let result: crate::Result<()> = self.crab.put(route, None::<&()>).await;
match result {
Ok(_) => Err(error::Error::GitHub {
source: GitHubError {
source: Box::new(GitHubError {
status_code: StatusCode::OK,
documentation_url: None,
errors: None,
message: "".to_string(),
},
}),
backtrace: Backtrace::capture(),
}),
Err(_v) => Ok(()),
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl std::error::Error for UriParseError {}
#[non_exhaustive]
pub enum Error {
GitHub {
source: GitHubError,
source: Box<GitHubError>,
backtrace: Backtrace,
},
UriParse {
Expand Down
22 changes: 9 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,12 @@ pub async fn map_github_error(
.context(error::SerdeSnafu)?;

Err(error::Error::GitHub {
source: GitHubError {
source: Box::new(GitHubError {
status_code: parts.status,
documentation_url,
errors,
message,
},
}),
backtrace: Backtrace::capture(),
})
}
Expand Down Expand Up @@ -365,8 +365,11 @@ pub fn instance() -> Arc<Octocrab> {
STATIC_INSTANCE.load().clone()
}

type Executor = Box<dyn Fn(Pin<Box<dyn Future<Output = ()>>>)>;

/// A builder struct for `Octocrab`, allowing you to configure the client, such
/// as using GitHub previews, the github instance, authentication, etc.
///
/// ```
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -377,17 +380,14 @@ pub fn instance() -> Arc<Octocrab> {
/// # Ok(())
/// # }
/// ```
//Typed builder, thanks to https://www.greyblake.com/blog/builder-with-typestate-in-rust/ for explaining

/// A builder struct for `Octocrab`.
///
/// OctocrabBuilder can be extended with a custom config, see [DefaultOctocrabBuilderConfig] for an example
pub struct OctocrabBuilder<Svc, Config, Auth, LayerReady> {
service: Svc,
auth: Auth,
config: Config,
_layer_ready: PhantomData<LayerReady>,
executor: Option<Box<dyn Fn(Pin<Box<dyn Future<Output = ()>>>)>>,
executor: Option<Executor>,
}

//Indicates weather the builder supports config
Expand Down Expand Up @@ -443,7 +443,7 @@ where
{
pub fn with_executor(
self,
executor: Box<dyn Fn(Pin<Box<dyn Future<Output = ()>>>)>,
executor: Executor,
) -> OctocrabBuilder<Svc, Config, Auth, LayerReady> {
OctocrabBuilder {
service: self.service,
Expand Down Expand Up @@ -1043,11 +1043,7 @@ impl Octocrab {
}

/// Creates a new `Octocrab` with a custom executor
fn new_with_executor<S>(
service: S,
auth_state: AuthState,
executor: Box<dyn Fn(Pin<Box<dyn Future<Output = ()>>>)>,
) -> Self
fn new_with_executor<S>(service: S, auth_state: AuthState, executor: Executor) -> Self
where
S: Service<Request<OctoBody>, Response = Response<BoxBody<Bytes, crate::Error>>>
+ Send
Expand Down

0 comments on commit 251c15b

Please sign in to comment.