Skip to content

refactor: Consolidate cross-cutting concerns and reduce code duplication #651

@jade-codes

Description

@jade-codes

Summary

Analysis of the syster-base crate reveals several cross-cutting concerns and duplications that should be consolidated for better maintainability.

Identified Issues

1. Duplicate Type Definitions

Position/Span duplication:

  • core::span::Position and semantic::types::diagnostic::Position - SAME type defined twice
  • core::span::Span and semantic::types::diagnostic::Range - SAME concept with different names
  • core::span::SymbolReference and semantic::symbol_table::symbol::SymbolReference - SAME type defined twice

Recommendation: Consolidate all position/span types into core::span and re-export from semantic where needed. The Range type should be an alias or import from Span.

2. Parallel KerML/SysML Implementations

The following files have nearly identical structure with only language-specific types swapped:

SysML KerML Lines Similarity
adapters/sysml/folding.rs adapters/kerml/folding.rs 83/82 ~95%
adapters/sysml/selection.rs adapters/kerml/selection.rs 130/127 ~90%
adapters/sysml/inlay_hints.rs adapters/kerml/inlay_hints.rs 137/109 ~85%
adapters/sysml/helpers.rs adapters/kerml/helpers.rs 137/34 ~70%
adapters/sysml/visitors.rs adapters/kerml/visitors.rs 285/226 ~80%
adapters/sysml/validator.rs adapters/kerml/validator.rs 109/47 ~60%
syntax/sysml/parser.rs syntax/kerml/parser.rs 157/63 ~70%
sysml_adapter.rs kerml_adapter.rs identical structs ~100%

Recommendation: Create a generic/trait-based approach:

  • Define a LanguageAdapter<T> trait
  • Create shared implementations with type parameters
  • Reduce boilerplate while maintaining type safety

3. File I/O Scattered Across Modules

File loading concerns are spread across:

  • core::file_io - low-level file operations
  • project::file_loader - re-exports from core + adds collection
  • syntax::parser - uses file_io for parsing

Recommendation: Clarify the layers:

  • core::file_io - pure file operations (no language awareness)
  • syntax::parser - language-aware parsing (owns parse dispatch)
  • project::file_loader - higher-level workspace operations (remove re-exports)

4. Error Types Fragmentation

Error-related types are scattered:

  • core::error_codes - error code constants
  • core::parse_result - ParseError, ParseErrorKind
  • semantic::types::error - SemanticError, SemanticErrorKind
  • semantic::types::diagnostic - Diagnostic, Severity

Recommendation: Consider a unified error hierarchy in core::errors:

core::errors/
  codes.rs        # Error code constants
  parse.rs        # ParseError
  semantic.rs     # SemanticError  
  diagnostic.rs   # Diagnostic (unified format)

5. Re-export Chains

Several modules have long re-export chains that obscure the source:

  • project::mod.rs re-exports from core
  • semantic::mod.rs has 15+ re-exports
  • This creates confusion about where types are defined

Recommendation:

  • Document the canonical import path for each type
  • Reduce re-exports to only the most common use cases
  • Use explicit paths in internal code

Proposed Consolidation

Phase 1: Type Deduplication (Low Risk)

  1. Remove semantic::types::diagnostic::Position - use core::Position
  2. Alias Range to Span or unify them
  3. Remove duplicate SymbolReference

Phase 2: Adapter Generalization (Medium Risk)

  1. Create LanguageAdapter<T> trait
  2. Create FoldingExtractor<T> trait
  3. Create SelectionExtractor<T> trait
  4. Implement for both SysML and KerML

Phase 3: Module Restructuring (Higher Risk)

  1. Consolidate error types
  2. Simplify file_loader module
  3. Reduce re-export chains

Success Criteria

  • No duplicate type definitions
  • Parallel language implementations share common code via traits
  • Clear module boundaries documented
  • cargo clippy passes without dead_code warnings
  • All existing tests pass

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions