add enum containing all successful route returns (#614)

* add enum containing all successful route returns

This enum derives `Responder`, so it can be used as route result.
We also implement `From`, so it can be converted

* disable clippy warning about this enum

There's no use trying to chase this warning.
The next warning will be about `Redirect`, which itself is 360 byte,
according to @fdb-hiroshima (thanks for the research!)

So, if anything, we can try to get Rocket to work on that!

* refactor routes/posts to only use one level of Result

* admin settings error is not an ErrorPage, so dont use Result<>

* refactor: use early return to remove indent of main logic

This diff is absolutely atrocious.
but the resulting readability is worth it

* refactor routes/post for early returns & RespondOrRedirect

* refactor routes/session for early returns & use RespondOrRedirect

* refactor routes/user -- add another field to enum

* refactor routes/blogs for early returns & RespondOrRedirect

* refactor routes/blogs for early returns & RespondOrRedirect

This is a final refactor of `pub fn update()` for readability.
We're removing at least one indentation level.
This commit is contained in:
Igor Galić
2019-06-14 09:33:30 +02:00
committed by GitHub
parent 4b205fa995
commit 3d27e283ad
7 changed files with 337 additions and 292 deletions
+12 -10
View File
@@ -25,14 +25,14 @@ use plume_models::{
users::*,
Error, PlumeRocket,
};
use routes::{errors::ErrorPage, Page, RemoteForm};
use routes::{errors::ErrorPage, Page, RemoteForm, RespondOrRedirect};
use template_utils::{IntoContext, Ructe};
#[get("/me")]
pub fn me(user: Option<User>) -> Result<Redirect, Flash<Redirect>> {
pub fn me(user: Option<User>) -> RespondOrRedirect {
match user {
Some(user) => Ok(Redirect::to(uri!(details: name = user.username))),
None => Err(utils::requires_login("", uri!(me))),
Some(user) => Redirect::to(uri!(details: name = user.username)).into(),
None => utils::requires_login("", uri!(me)).into(),
}
}
@@ -190,7 +190,7 @@ pub fn follow_not_connected(
name: String,
remote_form: Option<LenientForm<RemoteForm>>,
i18n: I18n,
) -> Result<Result<Flash<Ructe>, Redirect>, ErrorPage> {
) -> Result<RespondOrRedirect, ErrorPage> {
let target = User::find_by_fqn(&rockets, &name)?;
if let Some(remote_form) = remote_form {
if let Some(uri) = User::fetch_remote_interact_uri(&remote_form)
@@ -207,7 +207,7 @@ pub fn follow_not_connected(
.ok()
})
{
Ok(Err(Redirect::to(uri)))
Ok(Redirect::to(uri).into())
} else {
let mut err = ValidationErrors::default();
err.add("remote",
@@ -217,7 +217,7 @@ pub fn follow_not_connected(
params: HashMap::new(),
},
);
Ok(Ok(Flash::new(
Ok(Flash::new(
render!(users::follow_remote(
&rockets.to_context(),
target,
@@ -228,10 +228,11 @@ pub fn follow_not_connected(
)),
"callback",
uri!(follow: name = name).to_string(),
)))
)
.into())
}
} else {
Ok(Ok(Flash::new(
Ok(Flash::new(
render!(users::follow_remote(
&rockets.to_context(),
target,
@@ -243,7 +244,8 @@ pub fn follow_not_connected(
)),
"callback",
uri!(follow: name = name).to_string(),
)))
)
.into())
}
}