Skip to content

Commit

Permalink
refactor: avoid usage of boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
leroyguillaume committed Oct 13, 2023
1 parent fecc98b commit c3c6376
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 2,020 deletions.
6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@ categories = ["development-tools::testing"]
browser = ["dep:open"]
clock = ["dep:chrono"]
cmd = ["dep:async-trait", "dep:tokio"]
full = ["browser", "clock", "cmd", "http-client", "uuid"]
http-client = ["dep:async-trait", "dep:bytes", "dep:reqwest"]
full = ["browser", "clock", "cmd", "uuid"]
mock = ["dep:mockall"]
postgres = ["dep:async-trait", "dep:deadpool-postgres"]
uuid = ["dep:uuid"]

[dependencies]
async-trait = { version = "0.1", optional = true }
bytes = { version = "1.5", optional = true }
chrono = { version = "0.4", optional = true }
deadpool-postgres = { version = "0.10", optional = true }
mockall = { version = "0.11", optional = true }
open = { version = "5.0", optional = true }
reqwest = { version = "0.11", optional = true }
tokio = { version = "1.32", features = ["process"], optional = true}
tracing = "0.1"
uuid = { version = "0.8", features = ["v4"], optional = true }
Expand Down
30 changes: 12 additions & 18 deletions examples/clock.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
use chrono::{DateTime, Utc};
use mockable::{Clock, DefaultClock};

#[derive(Debug)]
#[derive(Debug, Eq, PartialEq)]
#[allow(dead_code)]
struct User {
creation: DateTime<Utc>,
name: String,
}

struct UserRegistry(Box<dyn Clock>);

impl UserRegistry {
fn new() -> Self {
Self(Box::new(DefaultClock))
}

fn create(&self, name: String) -> User {
User {
creation: self.0.utc(),
name,
}
fn create_user(name: String, clock: &dyn Clock) -> User {
User {
creation: clock.utc(),
name,
}
}

fn main() {
let registry = UserRegistry::new();
let user = registry.create("Alice".into());
let user = create_user("Alice".into(), &DefaultClock);
println!("{user:?}");
}

Expand All @@ -40,8 +31,11 @@ mod test {
let creation = Utc::now();
let mut clock = MockClock::new();
clock.expect_utc().return_const(creation);
let registry = UserRegistry(Box::new(clock));
let user = registry.create("Alice".into());
assert_eq!(user.creation, creation);
let expected_user = User {
creation,
name: "Alice".into(),
};
let user = create_user(expected_user.name.clone(), &clock);
assert_eq!(user, expected_user);
}
}
24 changes: 7 additions & 17 deletions examples/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
use mockable::{Command, CommandRunner, DefaultCommandRunner};

struct EchoRunner(Box<dyn CommandRunner>);

impl EchoRunner {
fn new() -> Self {
Self(Box::new(DefaultCommandRunner))
}

async fn echo(&self, message: &str) -> String {
let cmd = Command::new("echo".into()).with_arg(message.into());
let output = self.0.run(cmd).await.expect("echo failed");
String::from_utf8(output.stdout).expect("echo output is not utf8")
}
async fn echo(msg: &str, runner: &dyn CommandRunner) -> String {
let cmd = Command::new("echo".into()).with_arg(msg.into());
let output = runner.run(cmd).await.expect("echo failed");
String::from_utf8(output.stdout).expect("echo output is not utf8")
}

#[tokio::main]
async fn main() {
let runner = EchoRunner::new();
let message = runner.echo("Hello, world!").await;
println!("{message}");
let msg = echo("Hello, world!", &DefaultCommandRunner).await;
println!("{msg}");
}

#[cfg(test)]
Expand All @@ -40,8 +31,7 @@ mod test {
stdout: expected.as_bytes().to_vec(),
})
});
let runner = EchoRunner(Box::new(runner));
let message = runner.echo(expected).await;
let message = echo(&expected, &runner).await;
assert_eq!(message, expected);
}
}
22 changes: 6 additions & 16 deletions examples/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,15 @@ struct Config {
secret: String,
}

struct ConfigLoader(Box<dyn Env>);

impl ConfigLoader {
fn new() -> Self {
Self(Box::new(DefaultEnv::new()))
}

fn load(&self) -> Config {
Config {
secret: self.0.string("SECRET").expect("SECRET is not set"),
}
fn load(env: &dyn Env) -> Config {
Config {
secret: env.string("SECRET").expect("SECRET is not set"),
}
}

fn main() {
let loader = ConfigLoader::new();
let config = loader.load();
println!("{config:?}");
let cfg = load(&DefaultEnv);
println!("{cfg:?}");
}

#[cfg(test)]
Expand All @@ -42,8 +33,7 @@ mod test {
let expected = expected.clone();
move |_| Some(expected.secret.clone())
});
let loader = ConfigLoader(Box::new(env));
let cfg = loader.load();
let cfg = load(&env);
assert_eq!(cfg, expected);
}
}
81 changes: 0 additions & 81 deletions examples/fs.rs

This file was deleted.

47 changes: 0 additions & 47 deletions examples/http.rs

This file was deleted.

113 changes: 0 additions & 113 deletions examples/mock.rs

This file was deleted.

Loading

0 comments on commit c3c6376

Please sign in to comment.