1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#![doc(html_root_url = "https://docs.rs/captcha-rs/latest")]

//! Generate a verification image.
//!
//! ```rust
//! use captcha_rs::{Captcha, CaptchaBuilder};
//!
//! let dark_mode = true;
//! let text_length = 5;
//! let width = 130;
//! let height = 40;
//!
//! // generate a captcha with the given parameters
//! let captcha = Captcha::new(text_length, width, height, dark_mode);
//!
//! // generate a captcha using builder pattern
//! let captcha = CaptchaBuilder::new()
//! 	.length(text_length)
//! 	.width(width)
//! 	.height(height)
//! 	.dark_mode(dark_mode)
//! 	.build();
//!
//! println!("text: {}", captcha.text);
//! println!("base_img: {}", captcha.base_img);
//! ```

use crate::captcha::{get_captcha, get_captcha_img};

mod captcha;

pub struct Captcha {
	pub text: String,
	pub base_img: String,
	pub dark_mode: bool,
}

impl Captcha {
	pub fn new(length: usize, width: u32, height: u32, dark_mode: bool) -> Self {
		// Generate an array of captcha characters
		let res = get_captcha(length);
		let text = res.join("");
		// Generate an image based on the verification code character array and convert the image into a base 64 string
		let base_img = get_captcha_img(res, width, height, dark_mode);
		Captcha {
			text,
			base_img,
			dark_mode,
		}
	}
}

pub struct CaptchaBuilder {
	length: Option<usize>,
	width: Option<u32>,
	height: Option<u32>,
	dark_mode: Option<bool>,
}

impl CaptchaBuilder {
	pub fn new() -> Self {
		CaptchaBuilder {
			length: None,
			width: None,
			height: None,
			dark_mode: None,
		}
	}
	
	pub fn length(mut self, length: usize) -> Self {
		self.length = Some(length);
		self
	}
	
	pub fn width(mut self, width: u32) -> Self {
		self.width = Some(width);
		self
	}
	
	pub fn height(mut self, height: u32) -> Self {
		self.height = Some(height);
		self
	}
	
	pub fn dark_mode(mut self, dark_mode: bool) -> Self {
		self.dark_mode = Some(dark_mode);
		self
	}
	
	pub fn build(self) -> Captcha {
		let length = self.length.unwrap_or(5);
		let width = self.width.unwrap_or(130);
		let height = self.height.unwrap_or(40);
		let dark_mode = self.dark_mode.unwrap_or(false);
		Captcha::new(length, width, height, dark_mode)
	}
}

#[cfg(test)]
mod tests {
	use crate::{Captcha, CaptchaBuilder};
	
	#[test]
	fn it_generates_a_captcha() {
		let dark_mode = false;
		let text_length = 5;
		let width = 130;
		let height = 40;
		
		let start = std::time::Instant::now();
		
		let captcha = Captcha::new(text_length, width, height, dark_mode);
		
		let duration = start.elapsed();
		println!("Time elapsed in generating captcha() is: {:?}", duration);
		
		assert_eq!(captcha.text.len(), 5);
		
		let start_with = captcha.base_img.starts_with("data:image/png;base64,");
		assert_eq!(start_with, true);
		
		println!("text: {}", captcha.text);
		println!("base_img: {}", captcha.base_img);
	}
	
	#[test]
	fn it_generate_captcha_using_builder(){
		let start = std::time::Instant::now();
		let captcha = CaptchaBuilder::new()
			.length(5)
			.width(130)
			.height(40)
			.dark_mode(false)
			.build();
		
		let duration = start.elapsed();
		println!("Time elapsed in generating captcha() is: {:?}", duration);
		
		assert_eq!(captcha.text.len(), 5);
		
		let start_with = captcha.base_img.starts_with("data:image/png;base64,");
		assert_eq!(start_with, true);
	}
}