@@ -4,6 +4,7 @@ use crate::client::RestClient;
44use crate :: error:: Result ;
55use serde:: { Deserialize , Serialize } ;
66use serde_json:: Value ;
7+ use typed_builder:: TypedBuilder ;
78
89// Aliases for easier use
910pub type Database = DatabaseInfo ;
@@ -243,136 +244,62 @@ pub struct EndpointInfo {
243244 pub dns_name : Option < String > ,
244245}
245246
246- /// Builder for CreateDatabaseRequest
247- #[ derive( Debug , Default ) ]
248- pub struct CreateDatabaseRequestBuilder {
249- name : Option < String > ,
250- memory_size : Option < u64 > ,
251- port : Option < u16 > ,
252- replication : Option < bool > ,
253- persistence : Option < String > ,
254- eviction_policy : Option < String > ,
255- shards_count : Option < u32 > ,
256- module_list : Option < Vec < ModuleConfig > > ,
257- authentication_redis_pass : Option < String > ,
258- }
259-
260- impl CreateDatabaseRequestBuilder {
261- /// Create a new builder
262- pub fn new ( ) -> Self {
263- Self :: default ( )
264- }
265-
266- /// Set the database name (required)
267- pub fn name ( mut self , name : impl Into < String > ) -> Self {
268- self . name = Some ( name. into ( ) ) ;
269- self
270- }
271-
272- /// Set the memory size in bytes (required)
273- pub fn memory_size ( mut self , size : u64 ) -> Self {
274- self . memory_size = Some ( size) ;
275- self
276- }
277-
278- /// Set the port number
279- pub fn port ( mut self , port : u16 ) -> Self {
280- self . port = Some ( port) ;
281- self
282- }
283-
284- /// Enable or disable replication
285- pub fn replication ( mut self , enabled : bool ) -> Self {
286- self . replication = Some ( enabled) ;
287- self
288- }
289-
290- /// Set persistence type ("aof", "snapshot", "disabled")
291- pub fn persistence ( mut self , persistence : impl Into < String > ) -> Self {
292- self . persistence = Some ( persistence. into ( ) ) ;
293- self
294- }
295-
296- /// Set eviction policy
297- pub fn eviction_policy ( mut self , policy : impl Into < String > ) -> Self {
298- self . eviction_policy = Some ( policy. into ( ) ) ;
299- self
300- }
301-
302- /// Set number of shards
303- pub fn shards ( mut self , count : u32 ) -> Self {
304- self . shards_count = Some ( count) ;
305- self
306- }
307-
308- /// Add Redis modules
309- pub fn modules ( mut self , modules : Vec < ModuleConfig > ) -> Self {
310- self . module_list = Some ( modules) ;
311- self
312- }
313-
314- /// Set database password
315- pub fn password ( mut self , password : impl Into < String > ) -> Self {
316- self . authentication_redis_pass = Some ( password. into ( ) ) ;
317- self
318- }
319-
320- /// Build the request
321- pub fn build ( self ) -> Result < CreateDatabaseRequest > {
322- Ok ( CreateDatabaseRequest {
323- name : self . name . ok_or_else ( || {
324- crate :: error:: RestError :: ValidationError ( "Database name is required" . to_string ( ) )
325- } ) ?,
326- memory_size : self . memory_size . ok_or_else ( || {
327- crate :: error:: RestError :: ValidationError ( "Memory size is required" . to_string ( ) )
328- } ) ?,
329- port : self . port ,
330- replication : self . replication ,
331- persistence : self . persistence ,
332- eviction_policy : self . eviction_policy ,
333- shards_count : self . shards_count ,
334- module_list : self . module_list ,
335- authentication_redis_pass : self . authentication_redis_pass ,
336- } )
337- }
338- }
339-
340247/// Module configuration for database creation
341- #[ derive( Debug , Clone , Serialize , Deserialize ) ]
248+ #[ derive( Debug , Clone , Serialize , Deserialize , TypedBuilder ) ]
342249pub struct ModuleConfig {
250+ #[ builder( setter( into) ) ]
343251 pub module_name : String ,
344252 #[ serde( skip_serializing_if = "Option::is_none" ) ]
253+ #[ builder( default , setter( into, strip_option) ) ]
345254 pub module_args : Option < String > ,
346255}
347256
348257/// Create database request
349- #[ derive( Debug , Serialize , Deserialize ) ]
258+ ///
259+ /// # Examples
260+ ///
261+ /// ```rust,no_run
262+ /// use redis_enterprise::{CreateDatabaseRequest, ModuleConfig};
263+ ///
264+ /// let request = CreateDatabaseRequest::builder()
265+ /// .name("my-database")
266+ /// .memory_size(1024 * 1024 * 1024) // 1GB
267+ /// .port(12000)
268+ /// .replication(true)
269+ /// .persistence("aof")
270+ /// .eviction_policy("volatile-lru")
271+ /// .shards_count(2)
272+ /// .authentication_redis_pass("secure-password")
273+ /// .build();
274+ /// ```
275+ #[ derive( Debug , Serialize , Deserialize , TypedBuilder ) ]
350276pub struct CreateDatabaseRequest {
277+ #[ builder( setter( into) ) ]
351278 pub name : String ,
352279 pub memory_size : u64 ,
353280 #[ serde( skip_serializing_if = "Option::is_none" ) ]
281+ #[ builder( default , setter( strip_option) ) ]
354282 pub port : Option < u16 > ,
355283 #[ serde( skip_serializing_if = "Option::is_none" ) ]
284+ #[ builder( default , setter( strip_option) ) ]
356285 pub replication : Option < bool > ,
357286 #[ serde( skip_serializing_if = "Option::is_none" ) ]
287+ #[ builder( default , setter( into, strip_option) ) ]
358288 pub persistence : Option < String > ,
359289 #[ serde( skip_serializing_if = "Option::is_none" ) ]
290+ #[ builder( default , setter( into, strip_option) ) ]
360291 pub eviction_policy : Option < String > ,
361292 #[ serde( skip_serializing_if = "Option::is_none" ) ]
293+ #[ builder( default , setter( strip_option) ) ]
362294 pub shards_count : Option < u32 > ,
363295 #[ serde( skip_serializing_if = "Option::is_none" ) ]
296+ #[ builder( default , setter( strip_option) ) ]
364297 pub module_list : Option < Vec < ModuleConfig > > ,
365298 #[ serde( skip_serializing_if = "Option::is_none" ) ]
299+ #[ builder( default , setter( into, strip_option) ) ]
366300 pub authentication_redis_pass : Option < String > ,
367301}
368302
369- impl CreateDatabaseRequest {
370- /// Create a new builder for the request
371- pub fn builder ( ) -> CreateDatabaseRequestBuilder {
372- CreateDatabaseRequestBuilder :: new ( )
373- }
374- }
375-
376303/// Database handler for executing database commands
377304pub struct DatabaseHandler {
378305 client : RestClient ,
@@ -398,16 +325,6 @@ impl DatabaseHandler {
398325 self . client . post ( "/v1/bdbs" , & request) . await
399326 }
400327
401- /// Create a new database using builder pattern
402- pub async fn create_with_builder < F > ( & self , f : F ) -> Result < DatabaseInfo >
403- where
404- F : FnOnce ( CreateDatabaseRequestBuilder ) -> CreateDatabaseRequestBuilder ,
405- {
406- let builder = CreateDatabaseRequestBuilder :: new ( ) ;
407- let request = f ( builder) . build ( ) ?;
408- self . create ( request) . await
409- }
410-
411328 /// Update database configuration (BDB.UPDATE)
412329 pub async fn update ( & self , uid : u32 , updates : Value ) -> Result < DatabaseInfo > {
413330 self . client
0 commit comments