Skip to content

Commit

Permalink
Complete some TODOs (yewstack#1304)
Browse files Browse the repository at this point in the history
* Complete some `TODO`s

* Fix error handling.

* Simplify things.

* Update changelogs.
  • Loading branch information
teymour-aldridge committed Jun 20, 2020
1 parent 8d78620 commit 21a8228
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion yew-router/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ END TEMPLATE-->
- #### 🛠 Fixes
- None
- #### 🚨 Breaking changes
- None
- The `unit_state` module has been removed.


## **0.13.0** *2020-5-12*
Expand Down
8 changes: 0 additions & 8 deletions yew-router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@ pub mod components;
#[cfg(feature = "router")]
pub mod router;

/// TODO remove this
/// Contains aliases and functions for working with this library using a state of type `()`.
#[cfg(feature = "unit_alias")]
pub mod unit_state {
define_router_state!(());
pub use router_state::*;
}

/// Prelude module that can be imported when working with the yew_router
pub mod prelude {
pub use super::matcher::Captures;
Expand Down
9 changes: 9 additions & 0 deletions yewtil/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
END TEMPLATE-->

## **0.3.0** *(DATE)*

- #### ⚡️ Features
- Sample
- #### 🛠 Fixes
- Sample
- #### 🚨 Breaking changes
- `FetchAction::Success` has been renamed to `FetchAction::Fetched`

## **v0.2.0** *11/18/19*
- #### ⚡️ Features
- Add new `FetchRequest` trait, `fetch_resource()` function, and `FetchState` enum
Expand Down
9 changes: 4 additions & 5 deletions yewtil/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl<REQ: Default, RES: PartialEq> Fetch<REQ, RES> {
match action {
FetchAction::NotFetching => self.set_not_fetching(),
FetchAction::Fetching => self.set_fetching(),
FetchAction::Success(res) => self.set_fetched(res),
FetchAction::Fetched(res) => self.set_fetched(res),
FetchAction::Failed(err) => self.set_failed(err),
}
}
Expand Down Expand Up @@ -138,7 +138,7 @@ impl<REQ, RES> Fetch<REQ, RES> {
let req_type: PhantomData<T> = PhantomData;
async move {
let fetch_state = match fetch_resource(request, req_type).await {
Ok(response) => FetchAction::Success(response),
Ok(response) => FetchAction::Fetched(response),
Err(err) => FetchAction::Failed(err),
};

Expand All @@ -159,8 +159,7 @@ impl<REQ, RES> Fetch<REQ, RES> {
/// # Panics
/// If the Fetch wrapper doesn't contain an instance of a response, this function will panic.
pub fn unwrap(self) -> RES {
// TODO, actually provide some diagnostic here.
self.res().unwrap()
self.res().expect("No response body is present.")
}

/// Gets the response body (if present).
Expand Down Expand Up @@ -230,7 +229,7 @@ impl<REQ: FetchRequest> Fetch<REQ, REQ::ResponseBody> {
let req_type: PhantomData<REQ> = PhantomData;
async move {
let fetch_state = match fetch_resource(request, req_type).await {
Ok(response) => FetchAction::Success(response),
Ok(response) => FetchAction::Fetched(response),
Err(err) => FetchAction::Failed(err),
};

Expand Down
12 changes: 6 additions & 6 deletions yewtil/src/fetch/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::NeqAssign;
pub enum FetchAction<T> {
NotFetching,
Fetching,
Success(T), // TODO rename to Fetched(T)
Fetched(T),
Failed(FetchError),
}

Expand All @@ -20,14 +20,14 @@ impl<T> FetchAction<T> {
/// Returns a reference to the Success case
pub fn success(&self) -> Option<&T> {
match self {
FetchAction::Success(value) => Some(value),
FetchAction::Fetched(value) => Some(value),
_ => None,
}
}

/// Gets the value out of the fetch state if it is a `Success` variant.
pub fn unwrap(self) -> T {
if let FetchAction::Success(value) = self {
if let FetchAction::Fetched(value) = self {
value
} else {
panic!("Could not unwrap value of FetchState");
Expand All @@ -39,14 +39,14 @@ impl<T> FetchAction<T> {
match self {
FetchAction::NotFetching => FetchAction::NotFetching,
FetchAction::Fetching => FetchAction::NotFetching,
FetchAction::Success(t) => FetchAction::Success(f(t)),
FetchAction::Fetched(t) => FetchAction::Fetched(f(t)),
FetchAction::Failed(e) => FetchAction::Failed(e),
}
}

/// Applies a function that mutates the response if the Action is the success case.
pub fn alter<F: Fn(&mut T)>(&mut self, f: F) {
if let FetchAction::Success(t) = self {
if let FetchAction::Fetched(t) = self {
f(t)
}
}
Expand All @@ -56,7 +56,7 @@ impl<T> FetchAction<T> {
match self {
FetchAction::NotFetching => FetchAction::NotFetching,
FetchAction::Fetching => FetchAction::NotFetching,
FetchAction::Success(t) => FetchAction::Success(t),
FetchAction::Fetched(t) => FetchAction::Fetched(t),
FetchAction::Failed(e) => FetchAction::Failed(e.clone()),
}
}
Expand Down

0 comments on commit 21a8228

Please sign in to comment.