diff options
author | Martin Fischer <martin@push-f.com> | 2021-06-24 08:21:27 +0200 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-06-24 08:21:29 +0200 |
commit | b0cd90369a57187c35b2835b665645ba060e3ec7 (patch) | |
tree | 3fc2b6837fe36ae17b9b0246aad2367db8c8d0f7 | |
parent | afd6c8c083bcc11523cd4143e408e0831613fed9 (diff) |
refactor: move Error enum and impls to own module
-rw-r--r-- | src/error.rs | 49 | ||||
-rw-r--r-- | src/main.rs | 49 |
2 files changed, 51 insertions, 47 deletions
diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..c006255 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,49 @@ +use sputnik::hyper_body::FormError; +use sputnik::request::QueryError; +use std::str::Utf8Error; + +use crate::Context; + +pub enum Error { + /// A 400 bad request error. + BadRequest(String), + /// A 401 unauthorized error. + Unauthorized(String, Context), + /// A 403 forbidden error. + Forbidden(String), + /// A 404 not found error. + NotFound(String), + /// A 500 internal server error. + Internal(String), + /// A 302 redirect to the given path. + Redirect(String), + + // TODO: use Redirect instead + /// Missing trailing slash. + MissingTrailingSlash(String), +} + +impl From<Utf8Error> for Error { + fn from(_: Utf8Error) -> Self { + Self::BadRequest("invalid UTF-8".into()) + } +} + +impl From<git2::Error> for Error { + fn from(e: git2::Error) -> Self { + eprintln!("git error: {}", e); + Self::Internal("something went wrong with git".into()) + } +} + +impl From<FormError> for Error { + fn from(e: FormError) -> Self { + Self::BadRequest(e.to_string()) + } +} + +impl From<QueryError> for Error { + fn from(e: QueryError) -> Self { + Self::BadRequest(e.to_string()) + } +} diff --git a/src/main.rs b/src/main.rs index 658a245..99956c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,9 +19,7 @@ use pulldown_cmark::Options; use pulldown_cmark::Parser; use serde::Deserialize; use sputnik::html_escape; -use sputnik::hyper_body::FormError; use sputnik::mime; -use sputnik::request::QueryError; use sputnik::request::SputnikParts; use sputnik::response::SputnikBuilder; use std::convert::Infallible; @@ -29,7 +27,6 @@ use std::env; use std::path::Component; use std::path::Path; use std::path::PathBuf; -use std::str::Utf8Error; use std::sync::Arc; use url::Url; @@ -41,9 +38,11 @@ use { use crate::controller::MultiUserController; use crate::controller::SoloController; +use crate::error::Error; mod controller; mod diff; +mod error; mod forms; mod get_routes; mod post_routes; @@ -191,31 +190,6 @@ async fn serve<C: Controller + Send + Sync + 'static>(controller: C, args: Args) server.await.expect("server error"); } -pub enum Error { - /// A 400 bad request error. - BadRequest(String), - /// A 401 unauthorized error. - Unauthorized(String, Context), - /// A 403 forbidden error. - Forbidden(String), - /// A 404 not found error. - NotFound(String), - /// A 500 internal server error. - Internal(String), - /// A 302 redirect to the given path. - Redirect(String), - - // TODO: use Redirect instead - /// Missing trailing slash. - MissingTrailingSlash(String), -} - -impl From<Utf8Error> for Error { - fn from(_: Utf8Error) -> Self { - Self::BadRequest("invalid UTF-8".into()) - } -} - async fn service<C: Controller>( controller: Arc<C>, args: Arc<Args>, @@ -326,13 +300,6 @@ fn default_action() -> String { "view".into() } -impl From<git2::Error> for Error { - fn from(e: git2::Error) -> Self { - eprintln!("git error: {}", e); - Self::Internal("something went wrong with git".into()) - } -} - #[derive(Eq, PartialEq, Hash, Clone)] pub struct Branch(String); @@ -491,18 +458,6 @@ fn action_links<C: Controller>(active_action: &str, controller: &C, ctx: &Contex out } -impl From<FormError> for Error { - fn from(e: FormError) -> Self { - Self::BadRequest(e.to_string()) - } -} - -impl From<QueryError> for Error { - fn from(e: QueryError) -> Self { - Self::BadRequest(e.to_string()) - } -} - pub struct Context { repo: Repository, parts: Parts, |