Skip to content
This repository has been archived by the owner on Jun 28, 2020. It is now read-only.

Few adjustments and formatting #2

Merged
merged 1 commit into from
Oct 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

extern crate serde;

use serde::{Serialize, de::DeserializeOwned};
use serde::{de::DeserializeOwned, Serialize};
use std::fmt::Display;
use std::option::NoneError;

Expand All @@ -20,7 +20,7 @@ impl From<NoneError> for Error {
}

/// API to DB link
///
///
/// `E` is the endpoint (API)
/// `P` is the provider (DB)
pub trait Provider<P> {
Expand All @@ -36,7 +36,11 @@ pub trait Provider<P> {
fn create(provider: &P, query: Self::Data) -> Result<Self::Data, Error>;

/// Update an object
fn update(provider: &P, id: <Self::Data as Endpoint>::Id, new_data: Self::Data) -> Result<Self::Data, Error>;
fn update(
provider: &P,
id: <Self::Data as Endpoint>::Id,
new_data: Self::Data,
) -> Result<Self::Data, Error>;

/// Delete an object
fn delete(provider: &P, id: <Self::Data as Endpoint>::Id);
Expand All @@ -47,38 +51,37 @@ pub trait Endpoint: Default + Serialize + DeserializeOwned {
type Id: Display;

/// The URL on which this endpoint is mounted.
///
///
/// It should start with a /, and end without.
fn endpoint() -> &'static str;
const ENDPOINT: &'static str;

fn get<F: Fetch>(&self, id: Self::Id) -> Result<Self, Error> {
F::fetch("GET", format!("{}/{}", Self::endpoint(), id), None)
F::fetch("GET", format!("{}/{}", Self::ENDPOINT, id), None)
}

fn list<F: Fetch>(&self) -> Result<Self, Error> {
F::fetch("GET", format!("{}", Self::endpoint()), None)
F::fetch("GET", Self::ENDPOINT.to_string(), None)
}

fn find<F: Fetch>(&self, query: Self) -> Result<Self, Error> {
F::fetch("GET", format!("{}", Self::endpoint()), Some(query))
F::fetch("GET", Self::ENDPOINT.to_string(), Some(query))
}

fn create<F: Fetch>(&self, new: Self) -> Result<Self, Error> {
F::fetch("POST", format!("{}", Self::endpoint()), Some(new))
F::fetch("POST", Self::ENDPOINT.to_string(), Some(new))
}

fn update<F: Fetch>(&self, id: Self::Id, data: Self) -> Result<Self, Error> {
F::fetch("PUT", format!("{}/{}", Self::endpoint(), id), Some(data))
F::fetch("PUT", format!("{}/{}", Self::ENDPOINT, id), Some(data))
}

fn delete<F: Fetch>(&self, id: Self::Id) -> Result<Self, Error> {
F::fetch("DELETE", format!("{}/{}", Self::endpoint(), id), None)
F::fetch("DELETE", format!("{}/{}", Self::ENDPOINT, id), None)
}
}

/// Anything that can perform a network request to fetch an endpoint
pub trait Fetch {

/// Fetch a given endpoint
fn fetch<T: Endpoint>(method: &'static str, url: String, query: Option<T>) -> Result<T, Error>;
fn fetch<T: Endpoint>(method: &'static str, url: String, query: Option<T>) -> Result<T, Error>;
}