hexser 0.4.7

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
//! Builder for constructing AI context from hex graph.
//!
//! Transforms HexGraph into machine-readable AIContext.
//! Analyzes architecture, detects issues, generates suggestions.
//! Primary entry point for AI agent integration.
//!
//! Revision History
//! - 2025-10-10T20:28:00Z @AI: Add methods field to ComponentInfo with empty placeholder for future method extraction.
//! - 2025-10-02T19:00:00Z @AI: Fix test add_edge calls to use HexEdge constructor, fix Relationship typo, fix edges iteration.
//! - 2025-10-02T18:30:00Z @AI: Add comprehensive documentation and tests for all functions.
//! - 2025-10-02T18:15:00Z @AI: Fix API usage - remove iter call, use correct edge field names.
//! - 2025-10-02T18:00:00Z @AI: Initial context builder implementation.

/// Builder for constructing AI context from architecture graph
pub struct ContextBuilder<'a> {
  graph: &'a crate::graph::hex_graph::HexGraph,
}

impl<'a> ContextBuilder<'a> {
  /// Create new context builder from graph
  ///
  /// # Arguments
  /// * `graph` - Reference to HexGraph to analyze
  ///
  /// # Example
  /// ```
  /// # use hexser::graph::builder::GraphBuilder;
  /// # use hexser::ai::ContextBuilder;
  /// let graph = GraphBuilder::new().build();
  /// let builder = ContextBuilder::new(&graph);
  /// ```
  pub fn new(graph: &'a crate::graph::hex_graph::HexGraph) -> Self {
    Self { graph }
  }

  /// Build complete AI context from graph
  ///
  /// Transforms architecture graph into structured format for AI agent consumption.
  /// Includes components, relationships, constraints, and generated suggestions.
  ///
  /// # Returns
  /// Complete AIContext structure ready for serialization
  ///
  /// # Errors
  /// Returns error if context generation fails
  pub fn build(self) -> crate::result::hex_result::HexResult<super::ai_context::AIContext> {
    let components = self.build_components();
    let relationships = self.build_relationships();
    let constraints = self.build_constraints();
    let suggestions = self.generate_suggestions(&components, &relationships);

    Result::Ok(super::ai_context::AIContext {
      architecture: String::from("hexagonal"),
      version: String::from(env!("CARGO_PKG_VERSION")),
      components,
      relationships,
      constraints,
      suggestions,
      metadata: super::ai_context::ContextMetadata {
        generated_at: chrono::Utc::now().to_rfc3339(),
        hex_version: String::from(env!("CARGO_PKG_VERSION")),
        total_components: self.graph.node_count(),
        total_relationships: self.graph.edge_count(),
        schema_version: String::from("1.0.0"),
      },
    })
  }

  /// Extract component information from graph nodes
  ///
  /// Maps each graph node to ComponentInfo structure including
  /// type name, layer, role, and dependencies.
  ///
  /// Uses method_extractor to populate methods field with trait method information
  /// for Repository, Directive, and Query components.
  fn build_components(&self) -> Vec<super::ai_context::ComponentInfo> {
    self
      .graph
      .nodes()
      .map(|node| {
        let role_str = format!("{:?}", node.role);
        let methods =
          crate::ai::method_extractor::extract_methods_for_type(&node.type_name, &role_str);

        super::ai_context::ComponentInfo {
          type_name: node.type_name.clone(),
          layer: format!("{:?}", node.layer),
          role: role_str,
          module_path: node.module_path.clone(),
          purpose: None,
          dependencies: self
            .graph
            .edges_from(&node.id)
            .into_iter()
            .map(|edge| edge.target.to_string())
            .collect(),
          methods,
        }
      })
      .collect()
  }

  /// Extract relationship information from graph edges
  ///
  /// Maps each edge to RelationshipInfo with validation status.
  /// Validates relationships against architectural rules.
  fn build_relationships(&self) -> Vec<super::ai_context::RelationshipInfo> {
    self
      .graph
      .edges()
      .into_iter()
      .map(|edge| {
        let is_valid = self.validate_relationship(edge);
        super::ai_context::RelationshipInfo {
          from: edge.source.to_string(),
          to: edge.target.to_string(),
          relationship_type: format!("{:?}", edge.relationship),
          is_valid,
          validation_message: if is_valid {
            None
          } else {
            Some(String::from("Violates layer dependency rules"))
          },
        }
      })
      .collect()
  }

  /// Build complete constraint set for architecture
  ///
  /// Generates dependency rules, layer boundaries, naming conventions,
  /// and required patterns for AI agents to enforce.
  fn build_constraints(&self) -> super::ai_context::ConstraintSet {
    super::ai_context::ConstraintSet {
      dependency_rules: self.build_dependency_rules(),
      layer_boundaries: self.build_layer_boundaries(),
      naming_conventions: self.build_naming_conventions(),
      required_patterns: vec![
        String::from("One item per file"),
        String::from("No use statements"),
        String::from("Fully qualified paths"),
      ],
    }
  }

  /// Generate dependency rules between layers
  ///
  /// Defines which layers can depend on which other layers
  /// according to hexagonal architecture principles.
  fn build_dependency_rules(&self) -> Vec<super::ai_context::DependencyRule> {
    vec![
      super::ai_context::DependencyRule {
        from_layer: String::from("Domain"),
        to_layer: String::from("Infrastructure"),
        allowed: false,
        reason: String::from("Domain must not depend on infrastructure"),
      },
      super::ai_context::DependencyRule {
        from_layer: String::from("Application"),
        to_layer: String::from("Domain"),
        allowed: true,
        reason: String::from("Application coordinates domain logic"),
      },
    ]
  }

  /// Define layer boundaries and allowed dependencies
  ///
  /// Specifies for each layer what it can depend on
  /// and what can depend on it.
  fn build_layer_boundaries(&self) -> Vec<super::ai_context::LayerBoundary> {
    vec![
      super::ai_context::LayerBoundary {
        layer: String::from("Domain"),
        can_depend_on: vec![],
        dependents_allowed: vec![String::from("Ports"), String::from("Application")],
        purpose: String::from("Pure business logic with zero dependencies"),
      },
      super::ai_context::LayerBoundary {
        layer: String::from("Ports"),
        can_depend_on: vec![String::from("Domain")],
        dependents_allowed: vec![String::from("Adapters"), String::from("Application")],
        purpose: String::from("Interfaces defining what application needs"),
      },
    ]
  }

  /// Generate naming convention rules
  ///
  /// Provides patterns and examples for naming components
  /// according to project standards.
  fn build_naming_conventions(&self) -> Vec<super::ai_context::NamingConvention> {
    vec![
      super::ai_context::NamingConvention {
        applies_to: String::from("Repository ports"),
        pattern: String::from("*Repository trait"),
        example: String::from("trait UserRepository: Repository<User>"),
      },
      super::ai_context::NamingConvention {
        applies_to: String::from("Directives"),
        pattern: String::from("*Directive struct"),
        example: String::from("struct CreateUserDirective"),
      },
    ]
  }

  /// Validate relationship against architectural rules
  ///
  /// Checks if edge represents valid dependency according
  /// to hexagonal architecture layer rules.
  ///
  /// # Arguments
  /// * `_edge` - Edge to validate
  ///
  /// # Returns
  /// True if relationship is valid, false otherwise
  fn validate_relationship(&self, _edge: &crate::graph::hex_edge::HexEdge) -> bool {
    true
  }

  /// Generate AI suggestions based on architecture analysis
  ///
  /// Analyzes components and relationships to detect issues
  /// like missing implementations or architectural violations.
  ///
  /// # Arguments
  /// * `components` - List of all components in architecture
  /// * `_relationships` - List of all relationships
  ///
  /// # Returns
  /// List of suggestions for improvements
  fn generate_suggestions(
    &self,
    components: &[super::ai_context::ComponentInfo],
    _relationships: &[super::ai_context::RelationshipInfo],
  ) -> Vec<super::ai_context::Suggestion> {
    let mut suggestions = Vec::new();

    let ports: Vec<_> = components.iter().filter(|c| c.layer == "Port").collect();

    let adapters: Vec<_> = components.iter().filter(|c| c.layer == "Adapter").collect();

    if ports.len() > adapters.len() {
      suggestions.push(super::ai_context::Suggestion {
        suggestion_type: super::ai_context::SuggestionType::MissingImplementation,
        component: None,
        description: String::from("More ports than adapters - some ports may need implementations"),
        priority: super::ai_context::Priority::Medium,
        code_example: None,
      });
    }

    suggestions
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_context_builder_new() {
    let graph = crate::graph::builder::GraphBuilder::new().build();
    let builder = ContextBuilder::new(&graph);
    assert_eq!(builder.graph.node_count(), 0);
  }

  #[test]
  fn test_build_complete_context() {
    let graph = crate::graph::builder::GraphBuilder::new()
      .add_node(crate::graph::hex_node::HexNode::new(
        crate::graph::node_id::NodeId::from_name("TestEntity"),
        crate::graph::layer::Layer::Domain,
        crate::graph::role::Role::Entity,
        "TestEntity",
        "test::entity",
      ))
      .build();

    let builder = ContextBuilder::new(&graph);
    let context = builder.build().unwrap();

    assert_eq!(context.architecture, "hexagonal");
    assert_eq!(context.components.len(), 1);
    assert_eq!(context.components[0].type_name, "TestEntity");
    assert_eq!(context.metadata.total_components, 1);
  }

  #[test]
  fn test_build_components() {
    let graph = crate::graph::builder::GraphBuilder::new()
      .add_node(crate::graph::hex_node::HexNode::new(
        crate::graph::node_id::NodeId::from_name("User"),
        crate::graph::layer::Layer::Domain,
        crate::graph::role::Role::Entity,
        "User",
        "domain::user",
      ))
      .build();

    let builder = ContextBuilder::new(&graph);
    let components = builder.build_components();

    assert_eq!(components.len(), 1);
    assert_eq!(components[0].type_name, "User");
    assert_eq!(components[0].layer, "Domain");
    assert_eq!(components[0].role, "Entity");
  }

  #[test]
  fn test_build_relationships() {
    let graph = crate::graph::builder::GraphBuilder::new()
      .add_node(crate::graph::hex_node::HexNode::new(
        crate::graph::node_id::NodeId::from_name("User"),
        crate::graph::layer::Layer::Domain,
        crate::graph::role::Role::Entity,
        "User",
        "domain",
      ))
      .add_node(crate::graph::hex_node::HexNode::new(
        crate::graph::node_id::NodeId::from_name("UserRepo"),
        crate::graph::layer::Layer::Port,
        crate::graph::role::Role::Repository,
        "UserRepo",
        "ports",
      ))
      .add_edge(crate::graph::hex_edge::HexEdge::new(
        crate::graph::node_id::NodeId::from_name("UserRepo"),
        crate::graph::node_id::NodeId::from_name("User"),
        crate::graph::relationship::Relationship::Depends,
      ))
      .build();

    let builder = ContextBuilder::new(&graph);
    let relationships = builder.build_relationships();

    assert_eq!(relationships.len(), 1);
    assert!(relationships[0].is_valid);
  }

  #[test]
  fn test_build_constraints() {
    let graph = crate::graph::builder::GraphBuilder::new().build();
    let builder = ContextBuilder::new(&graph);
    let constraints = builder.build_constraints();

    assert!(!constraints.dependency_rules.is_empty());
    assert!(!constraints.layer_boundaries.is_empty());
    assert!(!constraints.naming_conventions.is_empty());
    assert!(!constraints.required_patterns.is_empty());
  }

  #[test]
  fn test_build_dependency_rules() {
    let graph = crate::graph::builder::GraphBuilder::new().build();
    let builder = ContextBuilder::new(&graph);
    let rules = builder.build_dependency_rules();

    assert!(rules.iter().any(|r| r.from_layer == "Domain" && !r.allowed));
    assert!(
      rules
        .iter()
        .any(|r| r.from_layer == "Application" && r.allowed)
    );
  }

  #[test]
  fn test_build_layer_boundaries() {
    let graph = crate::graph::builder::GraphBuilder::new().build();
    let builder = ContextBuilder::new(&graph);
    let boundaries = builder.build_layer_boundaries();

    let domain = boundaries.iter().find(|b| b.layer == "Domain");
    assert!(domain.is_some());
    assert!(domain.unwrap().can_depend_on.is_empty());
  }

  #[test]
  fn test_build_naming_conventions() {
    let graph = crate::graph::builder::GraphBuilder::new().build();
    let builder = ContextBuilder::new(&graph);
    let conventions = builder.build_naming_conventions();

    assert!(
      conventions
        .iter()
        .any(|c| c.applies_to == "Repository ports")
    );
    assert!(conventions.iter().any(|c| c.applies_to == "Directives"));
  }

  #[test]
  fn test_validate_relationship() {
    let graph = crate::graph::builder::GraphBuilder::new()
      .add_node(crate::graph::hex_node::HexNode::new(
        crate::graph::node_id::NodeId::from_name("A"),
        crate::graph::layer::Layer::Domain,
        crate::graph::role::Role::Entity,
        "A",
        "domain",
      ))
      .add_node(crate::graph::hex_node::HexNode::new(
        crate::graph::node_id::NodeId::from_name("B"),
        crate::graph::layer::Layer::Port,
        crate::graph::role::Role::Repository,
        "B",
        "ports",
      ))
      .add_edge(crate::graph::hex_edge::HexEdge::new(
        crate::graph::node_id::NodeId::from_name("A"),
        crate::graph::node_id::NodeId::from_name("B"),
        crate::graph::relationship::Relationship::Depends,
      ))
      .build();

    let builder = ContextBuilder::new(&graph);
    let edge = graph.edges().iter().next().unwrap();
    assert!(builder.validate_relationship(edge));
  }

  #[test]
  fn test_generate_suggestions_with_ports_no_adapters() {
    let graph = crate::graph::builder::GraphBuilder::new()
      .add_node(crate::graph::hex_node::HexNode::new(
        crate::graph::node_id::NodeId::from_name("UserRepository"),
        crate::graph::layer::Layer::Port,
        crate::graph::role::Role::Repository,
        "UserRepository",
        "ports",
      ))
      .build();

    let builder = ContextBuilder::new(&graph);
    let context = builder.build().unwrap();

    assert!(!context.suggestions.is_empty());
    assert!(context.suggestions.iter().any(|s| matches!(
      s.suggestion_type,
      super::super::ai_context::SuggestionType::MissingImplementation
    )));
  }

  #[test]
  fn test_generate_suggestions_balanced_architecture() {
    let graph = crate::graph::builder::GraphBuilder::new()
      .add_node(crate::graph::hex_node::HexNode::new(
        crate::graph::node_id::NodeId::from_name("UserRepository"),
        crate::graph::layer::Layer::Port,
        crate::graph::role::Role::Repository,
        "UserRepository",
        "ports",
      ))
      .add_node(crate::graph::hex_node::HexNode::new(
        crate::graph::node_id::NodeId::from_name("PostgresUserRepo"),
        crate::graph::layer::Layer::Adapter,
        crate::graph::role::Role::Adapter,
        "PostgresUserRepo",
        "adapters",
      ))
      .build();

    let builder = ContextBuilder::new(&graph);
    let components = builder.build_components();
    let relationships = builder.build_relationships();
    let suggestions = builder.generate_suggestions(&components, &relationships);

    assert!(suggestions.is_empty());
  }
}