@@ -12,6 +12,9 @@ pub use ansible_host::{AnsibleHost, AnsibleHostError};
1212pub use ansible_port:: { AnsiblePort , AnsiblePortError } ;
1313pub use ssh_private_key_file:: { SshPrivateKeyFile , SshPrivateKeyFileError } ;
1414
15+ pub mod builder;
16+ pub use builder:: InventoryContextBuilder ;
17+
1518/// Errors that can occur when creating an `InventoryContext`
1619#[ derive( Debug , Error ) ]
1720pub enum InventoryContextError {
@@ -49,82 +52,6 @@ pub struct InventoryContext {
4952 ansible_user : String ,
5053}
5154
52- /// Builder for `InventoryContext` with fluent interface
53- #[ derive( Debug , Default ) ]
54- #[ allow( clippy:: struct_field_names) ] // Field names mirror Ansible inventory variables
55- pub struct InventoryContextBuilder {
56- ansible_host : Option < AnsibleHost > ,
57- ansible_ssh_private_key_file : Option < SshPrivateKeyFile > ,
58- ansible_port : Option < AnsiblePort > ,
59- ansible_user : Option < String > ,
60- }
61-
62- impl InventoryContextBuilder {
63- /// Creates a new empty builder
64- #[ must_use]
65- pub fn new ( ) -> Self {
66- Self :: default ( )
67- }
68-
69- /// Sets the Ansible host for the builder.
70- #[ must_use]
71- pub fn with_host ( mut self , ansible_host : AnsibleHost ) -> Self {
72- self . ansible_host = Some ( ansible_host) ;
73- self
74- }
75-
76- /// Sets the SSH port for the builder.
77- #[ must_use]
78- pub fn with_ssh_port ( mut self , ansible_port : AnsiblePort ) -> Self {
79- self . ansible_port = Some ( ansible_port) ;
80- self
81- }
82-
83- /// Sets the SSH private key file path for the builder.
84- #[ must_use]
85- pub fn with_ssh_priv_key_path ( mut self , ssh_private_key_file : SshPrivateKeyFile ) -> Self {
86- self . ansible_ssh_private_key_file = Some ( ssh_private_key_file) ;
87- self
88- }
89-
90- /// Sets the Ansible user for the builder.
91- #[ must_use]
92- pub fn with_ansible_user ( mut self , ansible_user : String ) -> Self {
93- self . ansible_user = Some ( ansible_user) ;
94- self
95- }
96-
97- /// Builds the `InventoryContext`
98- ///
99- /// # Errors
100- ///
101- /// Returns an error if any required field is missing
102- pub fn build ( self ) -> Result < InventoryContext , InventoryContextError > {
103- let ansible_host = self
104- . ansible_host
105- . ok_or ( InventoryContextError :: MissingAnsibleHost ) ?;
106-
107- let ansible_ssh_private_key_file = self
108- . ansible_ssh_private_key_file
109- . ok_or ( InventoryContextError :: MissingSshPrivateKeyFile ) ?;
110-
111- let ansible_port = self
112- . ansible_port
113- . ok_or ( InventoryContextError :: MissingSshPort ) ?;
114-
115- let ansible_user = self
116- . ansible_user
117- . ok_or ( InventoryContextError :: MissingAnsibleUser ) ?;
118-
119- Ok ( InventoryContext {
120- ansible_host,
121- ansible_ssh_private_key_file,
122- ansible_port,
123- ansible_user,
124- } )
125- }
126- }
127-
12855impl InventoryContext {
12956 /// Creates a new `InventoryContext` using typed parameters
13057 ///
@@ -334,4 +261,16 @@ mod tests {
334261 assert_eq ! ( inventory_context. ansible_port( ) , 22 ) ;
335262 assert_eq ! ( inventory_context. ansible_user( ) , "ubuntu" ) ;
336263 }
264+
265+ #[ test]
266+ fn it_should_create_inventory_context_with_ipv6 ( ) {
267+ let host = AnsibleHost :: from_str ( "2001:db8::1" ) . unwrap ( ) ;
268+ let ssh_key = SshPrivateKeyFile :: new ( "/path/to/key" ) . unwrap ( ) ;
269+ let ssh_port = AnsiblePort :: new ( 22 ) . unwrap ( ) ;
270+ let user = "ubuntu" . to_string ( ) ;
271+
272+ let inventory_context = InventoryContext :: new ( host, ssh_key, ssh_port, user) . unwrap ( ) ;
273+
274+ assert_eq ! ( inventory_context. ansible_host( ) , "2001:db8::1" ) ;
275+ }
337276}
0 commit comments