Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement base64 fakes #216

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions fake/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ bson = { version = "2", optional = true }
url = { version = "2", optional = true }
indexmap = { version = "2", optional = true}
clap = { version = "4.0.32", optional = true, features=["cargo"] }
base64 = { version = "0.22.1", optional = true }

[dev-dependencies]
chrono = { version = "0.4", features = ["clock"], default-features = false }
Expand All @@ -62,6 +63,7 @@ geo = ["geo-types", "num-traits"]
http = ["dep:http", "url-escape"]
bson_oid = ["bson"]
cli = ["dep:clap","random_color","chrono","http"]
base64 = ["dep:base64"]

[[example]]
name = "basic"
Expand Down
52 changes: 52 additions & 0 deletions fake/src/impls/base64/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate::{Dummy, Fake, Faker};
use base64::prelude::*;

#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct UrlSafeBase64Value(pub String);

#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct Base64Value(pub String);

pub struct Base64;

pub struct UrlSafeBase64;

impl Dummy<Base64> for String {
fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &Base64, rng: &mut R) -> Self {
let data: Vec<u8> = Faker.fake_with_rng(rng);
let padding = Faker.fake_with_rng(rng);
let encoded = if padding {
BASE64_STANDARD.encode(&data)
} else {
BASE64_STANDARD_NO_PAD.encode(&data)
};
encoded
}
}

impl Dummy<UrlSafeBase64> for String {
fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &UrlSafeBase64, rng: &mut R) -> Self {
let data: Vec<u8> = Faker.fake_with_rng(rng);
let padding = Faker.fake_with_rng(rng);
let encoded = if padding {
BASE64_URL_SAFE.encode(&data)
} else {
BASE64_URL_SAFE_NO_PAD.encode(&data)
};
encoded
}
}

impl Dummy<Faker> for Base64Value {
fn dummy_with_rng<R: rand::Rng + ?Sized>(config: &Faker, rng: &mut R) -> Self {
let s = String::dummy_with_rng(&Base64, rng);
Base64Value(s)
}
}

impl Dummy<Faker> for UrlSafeBase64Value {
fn dummy_with_rng<R: rand::Rng + ?Sized>(config: &Faker, rng: &mut R) -> Self {
let s = String::dummy_with_rng(&UrlSafeBase64, rng);
UrlSafeBase64Value(s)
}
}
2 changes: 2 additions & 0 deletions fake/src/impls/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! This module contains implementations of `Dummy` trait

#[cfg(feature = "base64")]
pub mod base64;
#[cfg(feature = "bigdecimal-rs")]
pub mod bigdecimal;
#[cfg(feature = "bson_oid")]
Expand Down
4 changes: 4 additions & 0 deletions fake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ pub use impls::chrono;
#[cfg_attr(docsrs, doc(cfg(feature = "bson_oid")))]
pub use impls::bson_oid;

#[cfg(feature = "base64")]
#[cfg_attr(docsrs, doc(cfg(feature = "base64")))]
pub use impls::base64;

/// Fake value generation for specific formats.
///
/// It is structured in a way such that the modules here describes the custom
Expand Down
10 changes: 10 additions & 0 deletions fake/tests/determinism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,13 @@ mod bson_oid {

check_determinism! { one fake_bson_oid, ObjectId, Faker }
}

#[cfg(feature = "base64")]
mod base64 {
use fake::base64::*;
use fake::{Fake, Faker};
use rand::SeedableRng as _;

check_determinism! { one fake_base64, Base64Value, Faker }
check_determinism! { one fake_url_safe_base64, UrlSafeBase64Value, Faker }
}
Loading