Skip to content
This repository has been archived by the owner on May 18, 2023. It is now read-only.

Commit

Permalink
Add StubClient to GenericClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
leoschwarz committed Jul 9, 2017
1 parent 4327e96 commit 9ce4ecc
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/client/generic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use client::{Client, Response};
use client::{DirectClient, RecordingTarget, ReplayClient};
use client::{DirectClient, RecordingTarget, ReplayClient, StubClient};
use config::ClientConfig;
use error::Error;
use request::Request;
Expand All @@ -8,6 +8,7 @@ use std::path::PathBuf;
enum InnerClient {
Direct(DirectClient),
Replay(ReplayClient),
Stub(StubClient),
}

/// Provides an interface over the different client types which you can use in your code
Expand Down Expand Up @@ -36,12 +37,18 @@ impl GenericClient {
ReplayClient::new(RecordingTarget::Dir(replay_dir.into())).into()
}

/// Create a `GenericClient` using `StubClient` internally.
pub fn stub(client: StubClient) -> Self {
client.into()
}

/// If this is a ReplayClient it will inform the Replay Client that whichever next request is
/// made should be recorded again, even if it has been made exactly this way before.
pub fn force_record_next(&self) {
match self.inner {
InnerClient::Direct(_) => {}
InnerClient::Replay(ref replay) => replay.force_record_next(),
InnerClient::Stub(_) => {}
}
}

Expand All @@ -68,25 +75,34 @@ impl From<ReplayClient> for GenericClient {
}
}

impl From<StubClient> for GenericClient {
fn from(c: StubClient) -> Self {
GenericClient { inner: InnerClient::Stub(c) }
}
}

impl Client for GenericClient {
fn execute(&self, config: Option<&ClientConfig>, request: Request) -> Result<Response, Error> {
match self.inner {
InnerClient::Direct(ref client) => client.execute(config, request),
InnerClient::Replay(ref client) => client.execute(config, request),
InnerClient::Stub(ref client) => client.execute(config, request),
}
}

fn config(&self) -> &ClientConfig {
match self.inner {
InnerClient::Direct(ref client) => client.config(),
InnerClient::Replay(ref client) => client.config(),
InnerClient::Stub(ref client) => client.config(),
}
}

fn config_mut(&mut self) -> &mut ClientConfig {
match self.inner {
InnerClient::Direct(ref mut client) => client.config_mut(),
InnerClient::Replay(ref mut client) => client.config_mut(),
InnerClient::Stub(ref mut client) => client.config_mut(),
}
}
}

0 comments on commit 9ce4ecc

Please sign in to comment.