hexser 0.6.0

Zero-boilerplate hexagonal architecture with graph-based introspection
Documentation
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
//! Integration tests for hex crate.
//!
//! These tests validate that all components of the hexagonal architecture
//! work together correctly. They test the flow from domain through ports
//! and adapters, ensuring proper separation of concerns and functionality.
//!
//! Revision History
//! - 2025-10-12T18:37:00Z @AI: Add comprehensive Application trait integration test with full CQRS flow.
//! - 2025-10-02T21:45:00Z @AI: Enhance HexAggregate macro test to use hex(invariants) attribute for custom validation.
//! - 2025-10-02T21:30:00Z @AI: Fix conflicting Aggregate implementations, remove derive from custom invariant test.

#[cfg(test)]
mod domain_integration {
  use hexser::{Aggregate, HexValueItem};

  /// Test Entity and ValueObject integration.
  #[test]
  fn test_entity_with_value_object() {
    struct Email(String);

    impl hexser::HexValueItem for Email {
      fn validate(&self) -> hexser::HexResult<()> {
        if self.0.contains('@') {
          Ok(())
        } else {
          Err(hexser::Hexserror::validation("Email must contain @"))
        }
      }
    }

    struct User {
      id: String,
      email: Email,
    }

    impl hexser::HexEntity for User {
      type Id = String;
    }

    let email = Email(String::from("test@example.com"));
    assert!(email.validate().is_ok());

    let user = User {
      id: String::from("1"),
      email,
    };

    let _id: <User as hexser::HexEntity>::Id = user.id;
  }

  /// Test HexAggregate macro with custom invariants via attribute.
  #[test]
  fn test_aggregate_invariants() {
    #[derive(hexser_macros::HexAggregate)]
    struct Order {
      id: String,
      items: Vec<String>,
    }

    impl hexser::HexEntity for Order {
      type Id = String;
    }

    impl Order {
      fn check_invariants(&self) -> hexser::HexResult<()> {
        if self.items.is_empty() {
          return Err(hexser::Hexserror::domain(
            hexser::error_codes::domain::INVARIANT_EMPTY,
            "Order must have items",
          ));
        }
        Ok(())
      }
    }

    let valid_order = Order {
      id: String::from("1"),
      items: vec![String::from("item1")],
    };
    assert!(valid_order.check_invariants().is_ok());

    let invalid_order = Order {
      id: String::from("2"),
      items: vec![],
    };
    assert!(invalid_order.check_invariants().is_err());
  }

  /// Test HexAggregate derive macro with default implementation.
  #[test]
  fn test_hex_aggregate_derive_default() {
    #[derive(hexser_macros::HexAggregate)]
    struct SimpleAggregate {
      id: String,
      value: i32,
    }

    impl hexser::HexEntity for SimpleAggregate {
      type Id = String;
    }

    let aggregate = SimpleAggregate {
      id: String::from("1"),
      value: 42,
    };

    assert!(aggregate.check_invariants().is_ok());
  }
}

#[cfg(test)]
mod port_adapter_integration {
  use hexser::{Mapper, Repository};

  /// Test Repository port with adapter implementation.
  #[test]
  fn test_repository_flow() {
    #[derive(Clone)]
    struct Todo {
      id: String,
      title: String,
      done: bool,
    }

    impl hexser::HexEntity for Todo {
      type Id = String;
    }

    struct InMemoryTodoRepository {
      todos: Vec<Todo>,
    }

    impl hexser::adapters::Adapter for InMemoryTodoRepository {}

    impl hexser::ports::Repository<Todo> for InMemoryTodoRepository {
      fn save(&mut self, todo: Todo) -> hexser::HexResult<()> {
        self.todos.push(todo);
        Ok(())
      }
    }

    #[derive(Clone)]
    enum TodoFilter {
      All,
      ById(String),
    }
    #[derive(Clone, Copy)]
    enum TodoSortKey {
      Id,
    }

    impl hexser::ports::repository::QueryRepository<Todo> for InMemoryTodoRepository {
      type Filter = TodoFilter;
      type SortKey = TodoSortKey;

      fn find_one(&self, filter: &TodoFilter) -> hexser::HexResult<Option<Todo>> {
        let found = match filter {
          TodoFilter::All => self.todos.first().cloned(),
          TodoFilter::ById(id) => self.todos.iter().find(|t| &t.id == id).cloned(),
        };
        Ok(found)
      }

      fn find(
        &self,
        filter: &TodoFilter,
        _opts: hexser::ports::repository::FindOptions<TodoSortKey>,
      ) -> hexser::HexResult<Vec<Todo>> {
        let items: Vec<Todo> = match filter {
          TodoFilter::All => self.todos.clone(),
          TodoFilter::ById(id) => self.todos.iter().filter(|t| &t.id == id).cloned().collect(),
        };
        Ok(items)
      }

      fn delete_where(&mut self, filter: &TodoFilter) -> hexser::HexResult<u64> {
        let before = self.todos.len();
        match filter {
          TodoFilter::All => self.todos.clear(),
          TodoFilter::ById(id) => self.todos.retain(|t| &t.id != id),
        }
        Ok((before.saturating_sub(self.todos.len())) as u64)
      }
    }

    let mut repo = InMemoryTodoRepository { todos: Vec::new() };

    let todo = Todo {
      id: String::from("1"),
      title: String::from("Test"),
      done: false,
    };

    assert!(repo.save(todo).is_ok());
    assert!(
      <InMemoryTodoRepository as hexser::ports::repository::QueryRepository<Todo>>::find_one(
        &repo,
        &TodoFilter::ById(String::from("1"))
      )
      .unwrap()
      .is_some()
    );
    assert_eq!(
      <InMemoryTodoRepository as hexser::ports::repository::QueryRepository<Todo>>::find(
        &repo,
        &TodoFilter::All,
        hexser::ports::repository::FindOptions::default()
      )
      .unwrap()
      .len(),
      1
    );
    assert_eq!(
      <InMemoryTodoRepository as hexser::ports::repository::QueryRepository<Todo>>::delete_where(
        &mut repo,
        &TodoFilter::ById(String::from("1"))
      )
      .unwrap(),
      1
    );
    assert!(
      <InMemoryTodoRepository as hexser::ports::repository::QueryRepository<Todo>>::find_one(
        &repo,
        &TodoFilter::ById(String::from("1"))
      )
      .unwrap()
      .is_none()
    );
  }

  /// Test Mapper transformations.
  #[test]
  fn test_mapper_transformations() {
    struct DomainUser {
      id: String,
      email: String,
    }

    struct DbUserRow {
      user_id: String,
      user_email: String,
    }

    struct UserMapper;

    impl hexser::adapters::Mapper<DomainUser, DbUserRow> for UserMapper {
      fn map(&self, from: DomainUser) -> hexser::HexResult<DbUserRow> {
        Ok(DbUserRow {
          user_id: from.id,
          user_email: from.email,
        })
      }
    }

    impl hexser::adapters::Mapper<DbUserRow, DomainUser> for UserMapper {
      fn map(&self, from: DbUserRow) -> hexser::HexResult<DomainUser> {
        Ok(DomainUser {
          id: from.user_id,
          email: from.user_email,
        })
      }
    }

    let mapper = UserMapper;

    let domain = DomainUser {
      id: String::from("1"),
      email: String::from("test@example.com"),
    };

    let db_row: DbUserRow = mapper.map(domain).unwrap();
    assert_eq!(db_row.user_id, "1");

    let back_to_domain: DomainUser = mapper.map(db_row).unwrap();
    assert_eq!(back_to_domain.id, "1");
  }
}

#[cfg(test)]
mod cqrs_integration {
  use hexser::{Directive, DirectiveHandler, QueryHandler};

  /// Test Directive with handler.
  #[test]
  fn test_directive_handler_flow() {
    struct CreateTodoDirective {
      title: String,
    }

    impl hexser::application::Directive for CreateTodoDirective {
      fn validate(&self) -> hexser::HexResult<()> {
        if self.title.is_empty() {
          return Err(hexser::Hexserror::validation("Title cannot be empty"));
        }
        Ok(())
      }
    }

    struct CreateTodoHandler;

    impl hexser::application::DirectiveHandler<CreateTodoDirective> for CreateTodoHandler {
      fn handle(&self, directive: CreateTodoDirective) -> hexser::HexResult<()> {
        directive.validate()?;
        // Would save to repository here
        Ok(())
      }
    }

    let handler = CreateTodoHandler;

    let valid = CreateTodoDirective {
      title: String::from("Test todo"),
    };
    assert!(handler.handle(valid).is_ok());

    let invalid = CreateTodoDirective {
      title: String::from(""),
    };
    assert!(handler.handle(invalid).is_err());
  }

  /// Test Query with handler.
  #[test]
  fn test_query_handler_flow() {
    struct FindTodoQuery {
      id: String,
    }

    #[derive(Clone)]
    struct TodoView {
      id: String,
      title: String,
    }

    struct FindTodoHandler {
      todos: Vec<TodoView>,
    }

    impl hexser::application::QueryHandler<FindTodoQuery, Option<TodoView>> for FindTodoHandler {
      fn handle(&self, query: FindTodoQuery) -> hexser::HexResult<Option<TodoView>> {
        Ok(self.todos.iter().find(|t| t.id == query.id).cloned())
      }
    }

    let handler = FindTodoHandler {
      todos: vec![TodoView {
        id: String::from("1"),
        title: String::from("Test"),
      }],
    };

    let query = FindTodoQuery {
      id: String::from("1"),
    };

    let result = handler.handle(query).unwrap();
    assert!(result.is_some());
  }
}

#[cfg(test)]
mod error_integration {
  /// Test error builder pattern.
  #[test]
  fn test_error_builder() {
    let err = hexser::Hexserror::domain(
      hexser::error_codes::domain::INVARIANT_EMPTY,
      "Order cannot be empty",
    )
    .with_next_step("Add at least one item")
    .with_suggestion("order.add_item(item)");

    let display = format!("{err}");
    assert!(display.contains("E_HEX_001"));
    assert!(display.contains("Next Steps"));
  }
}

#[cfg(test)]
mod application_integration {
  use hexser::{
    Application, Directive, DirectiveHandler, HexEntity, HexResult, QueryHandler, Repository,
  };

  // Domain layer
  #[derive(Clone, Debug, PartialEq)]
  struct Todo {
    id: String,
    title: String,
    completed: bool,
  }

  impl HexEntity for Todo {
    type Id = String;
  }

  // Repository port
  trait TodoRepository: Repository<Todo> {
    fn find_all(&self) -> HexResult<Vec<Todo>>;
  }

  // Repository adapter (using interior mutability for shared state)
  struct InMemoryTodoRepository {
    todos: std::sync::Arc<std::sync::Mutex<Vec<Todo>>>,
    initialized: std::sync::Arc<std::sync::Mutex<bool>>,
  }

  impl InMemoryTodoRepository {
    fn new() -> Self {
      Self {
        todos: std::sync::Arc::new(std::sync::Mutex::new(Vec::new())),
        initialized: std::sync::Arc::new(std::sync::Mutex::new(false)),
      }
    }

    fn initialize(&mut self) -> HexResult<()> {
      let mut initialized = self.initialized.lock().unwrap();
      *initialized = true;
      Ok(())
    }
  }

  impl Clone for InMemoryTodoRepository {
    fn clone(&self) -> Self {
      Self {
        todos: std::sync::Arc::clone(&self.todos),
        initialized: std::sync::Arc::clone(&self.initialized),
      }
    }
  }

  impl hexser::adapters::Adapter for InMemoryTodoRepository {}

  impl Repository<Todo> for InMemoryTodoRepository {
    fn save(&mut self, todo: Todo) -> HexResult<()> {
      let initialized = self.initialized.lock().unwrap();
      if !*initialized {
        return Err(hexser::Hexserror::adapter(
          hexser::error_codes::adapter::DB_CONNECTION_FAILURE,
          "Repository not initialized",
        ));
      }
      drop(initialized);

      let mut todos = self.todos.lock().unwrap();
      todos.push(todo);
      Ok(())
    }
  }

  impl TodoRepository for InMemoryTodoRepository {
    fn find_all(&self) -> HexResult<Vec<Todo>> {
      let initialized = self.initialized.lock().unwrap();
      if !*initialized {
        return Err(hexser::Hexserror::adapter(
          hexser::error_codes::adapter::DB_CONNECTION_FAILURE,
          "Repository not initialized",
        ));
      }
      drop(initialized);

      let todos = self.todos.lock().unwrap();
      Ok(todos.clone())
    }
  }

  // Directive (write side)
  struct CreateTodoDirective {
    title: String,
  }

  impl Directive for CreateTodoDirective {
    fn validate(&self) -> HexResult<()> {
      if self.title.is_empty() {
        return Err(hexser::Hexserror::validation_field(
          "Title cannot be empty",
          "title",
        ));
      }
      Ok(())
    }
  }

  // Directive handler
  struct CreateTodoHandler {
    repository: InMemoryTodoRepository,
    next_id: std::cell::Cell<u32>,
  }

  impl CreateTodoHandler {
    fn new(repository: InMemoryTodoRepository) -> Self {
      Self {
        repository,
        next_id: std::cell::Cell::new(1),
      }
    }
  }

  impl DirectiveHandler<CreateTodoDirective> for CreateTodoHandler {
    fn handle(&self, directive: CreateTodoDirective) -> HexResult<()> {
      directive.validate()?;

      let id = self.next_id.get();
      let todo = Todo {
        id: id.to_string(),
        title: directive.title,
        completed: false,
      };

      self.next_id.set(id + 1);
      let mut repo = self.repository.clone();
      repo.save(todo)
    }
  }

  // Query (read side)
  struct FindAllTodosQuery;

  // Query handler
  struct FindAllTodosHandler {
    repository: InMemoryTodoRepository,
  }

  impl FindAllTodosHandler {
    fn new(repository: InMemoryTodoRepository) -> Self {
      Self { repository }
    }
  }

  impl QueryHandler<FindAllTodosQuery, Vec<Todo>> for FindAllTodosHandler {
    fn handle(&self, _query: FindAllTodosQuery) -> HexResult<Vec<Todo>> {
      self.repository.find_all()
    }
  }

  // Application
  struct TodoApplication {
    directive_handler: Option<CreateTodoHandler>,
    query_handler: Option<FindAllTodosHandler>,
    repository: InMemoryTodoRepository,
    fail_on_initialize: bool,
    fail_on_run: bool,
  }

  impl TodoApplication {
    fn new() -> Self {
      Self {
        directive_handler: None,
        query_handler: None,
        repository: InMemoryTodoRepository::new(),
        fail_on_initialize: false,
        fail_on_run: false,
      }
    }

    fn with_init_failure() -> Self {
      let mut app = Self::new();
      app.fail_on_initialize = true;
      app
    }

    fn with_run_failure() -> Self {
      let mut app = Self::new();
      app.fail_on_run = true;
      app
    }
  }

  impl Application for TodoApplication {
    fn name(&self) -> &str {
      "TodoApplication"
    }

    fn initialize(&mut self) -> HexResult<()> {
      if self.fail_on_initialize {
        return Err(hexser::Hexserror::adapter(
          hexser::error_codes::io::IO_FAILURE,
          "Simulated initialization failure",
        ));
      }

      // Initialize repository
      self.repository.initialize()?;

      // Create handlers with initialized repository
      self.directive_handler = Some(CreateTodoHandler::new(self.repository.clone()));
      self.query_handler = Some(FindAllTodosHandler::new(self.repository.clone()));

      Ok(())
    }

    fn run(&mut self) -> HexResult<()> {
      if self.fail_on_run {
        return Err(hexser::Hexserror::domain(
          hexser::error_codes::domain::INVARIANT_VIOLATION,
          "Simulated runtime failure",
        ));
      }

      // Execute some directives
      let directive = CreateTodoDirective {
        title: String::from("Integration test todo"),
      };

      self
        .directive_handler
        .as_mut()
        .expect("Handler not initialized")
        .handle(directive)?;

      // Execute a query
      let query = FindAllTodosQuery;
      let todos = self
        .query_handler
        .as_ref()
        .expect("Handler not initialized")
        .handle(query)?;

      assert_eq!(todos.len(), 1);
      assert_eq!(todos[0].title, "Integration test todo");

      Ok(())
    }

    fn shutdown(&mut self) -> HexResult<()> {
      // Cleanup handlers
      self.directive_handler = None;
      self.query_handler = None;
      Ok(())
    }
  }

  /// Test: Complete application lifecycle with CQRS flow.
  /// Justification: Validates that the Application trait orchestrates the entire
  /// hexagonal architecture correctly, from initialization through execution to shutdown.
  #[test]
  fn test_complete_application_lifecycle() {
    let mut app = TodoApplication::new();

    // Execute full lifecycle
    let result = app.execute();
    assert!(result.is_ok());

    // Verify handlers were cleaned up
    assert!(app.directive_handler.is_none());
    assert!(app.query_handler.is_none());
  }

  /// Test: Application handles initialization failures.
  /// Justification: Ensures that initialization errors prevent the application
  /// from running in an invalid state.
  #[test]
  fn test_application_initialization_failure() {
    let mut app = TodoApplication::with_init_failure();

    let result = app.execute();
    assert!(result.is_err());

    let err = result.unwrap_err();
    assert!(err.to_string().contains("initialization failure"));
  }

  /// Test: Application handles runtime failures and still shuts down.
  /// Justification: Verifies that runtime errors are propagated but shutdown
  /// still occurs for cleanup.
  #[test]
  fn test_application_runtime_failure() {
    let mut app = TodoApplication::with_run_failure();

    let result = app.execute();
    assert!(result.is_err());

    let err = result.unwrap_err();
    assert!(err.to_string().contains("runtime failure"));

    // Verify shutdown was still called (handlers cleaned up)
    assert!(app.directive_handler.is_none());
    assert!(app.query_handler.is_none());
  }

  /// Test: Minimal application with default implementations.
  /// Justification: Validates the zero-boilerplate philosophy where only
  /// name() is required.
  #[test]
  fn test_minimal_application() {
    struct MinimalApp;

    impl Application for MinimalApp {
      fn name(&self) -> &str {
        "MinimalApp"
      }
    }

    let mut app = MinimalApp;
    assert_eq!(app.name(), "MinimalApp");

    let result = app.execute();
    assert!(result.is_ok());
  }

  /// Test: Directive validation through Application.
  /// Justification: Ensures that directive validation errors are properly
  /// propagated through the application layer.
  #[test]
  fn test_directive_validation_through_application() {
    let mut app = TodoApplication::new();
    app.initialize().expect("Initialization failed");

    let invalid_directive = CreateTodoDirective {
      title: String::from(""),
    };

    let result = app
      .directive_handler
      .as_mut()
      .unwrap()
      .handle(invalid_directive);

    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(err.to_string().contains("Title cannot be empty"));
  }

  /// Test: Repository not initialized error handling.
  /// Justification: Validates that the application properly handles infrastructure
  /// errors when components aren't properly initialized.
  #[test]
  fn test_uninitialized_repository_error() {
    let mut repo = InMemoryTodoRepository::new();

    let todo = Todo {
      id: String::from("1"),
      title: String::from("Test"),
      completed: false,
    };

    let result = repo.save(todo);
    assert!(result.is_err());

    let err = result.unwrap_err();
    assert!(err.to_string().contains("not initialized"));
  }
}