@@ -22,28 +22,32 @@ fn main() -> anyhow::Result<()> {
2222
2323 match opts. cmd {
2424 Command :: Insert { message } => {
25- let config = Config :: load ( ) ?;
26- let bundle = Bundle {
27- primary : PrimaryBlock {
28- version : config. bundle . version ,
29- destination : config. endpoints . destination ,
30- source : config. endpoints . source ,
31- report_to : config. endpoints . report_to ,
32- creation_timestamp : generate_creation_timestamp ( ) ,
33- lifetime : config. bundle . lifetime ,
34- } ,
35- payload : message. into_bytes ( ) ,
36- } ;
37-
38- let encoded = serde_cbor:: to_vec ( & bundle) ?;
39- std:: fs:: write ( "bundle.cbor" , encoded) ?;
40- println ! ( "Bundle saved to bundle.cbor" ) ;
25+ handle_insert ( message) ?;
4126 }
4227 }
4328
4429 Ok ( ( ) )
4530}
4631
32+ fn handle_insert ( message : String ) -> anyhow:: Result < ( ) > {
33+ let config = Config :: load ( ) ?;
34+ let bundle = Bundle {
35+ primary : PrimaryBlock {
36+ version : config. bundle . version ,
37+ destination : config. endpoints . destination ,
38+ source : config. endpoints . source ,
39+ report_to : config. endpoints . report_to ,
40+ creation_timestamp : generate_creation_timestamp ( ) ,
41+ lifetime : config. bundle . lifetime ,
42+ } ,
43+ payload : message. into_bytes ( ) ,
44+ } ;
45+ let encoded = serde_cbor:: to_vec ( & bundle) ?;
46+ std:: fs:: write ( "bundle.cbor" , encoded) ?;
47+ println ! ( "Bundle saved to bundle.cbor" ) ;
48+ Ok ( ( ) )
49+ }
50+
4751#[ cfg( test) ]
4852mod tests {
4953 use super :: * ;
@@ -57,4 +61,10 @@ mod tests {
5761 Command :: Insert { message } => assert_eq ! ( message, "hello" ) ,
5862 }
5963 }
64+
65+ #[ test]
66+ fn test_handle_insert ( ) {
67+ let result = handle_insert ( "test message" . to_string ( ) ) ;
68+ assert ! ( result. is_ok( ) ) ;
69+ }
6070}
0 commit comments