Skip to main content

Crate thread_services

Crate thread_services 

Source
Expand description

§Thread Service Layer

This crate provides the service layer interfaces for Thread that abstract over ast-grep functionality while preserving all its powerful capabilities.

§Core Philosophy

The service layer acts as abstraction glue that:

  • Preserves Power: All ast-grep capabilities (Matcher, Replacer, Position) remain accessible
  • Bridges Levels: Connects file-level AST operations to codebase-level relational intelligence
  • Enables Execution: Abstracts over different execution environments (rayon, cloud workers)
  • Commercial Ready: Clear boundaries for commercial extensions

§Architecture

Thread pushes ast-grep from file-level to codebase-level analysis:

  • File Level: ast-grep provides powerful AST pattern matching and replacement
  • Codebase Level: Thread adds graph intelligence and cross-file relationships
  • Service Layer: Abstracts and coordinates both levels seamlessly

§Key Components

  • types - Language-agnostic types that wrap ast-grep functionality
  • traits - Service interfaces for parsing, analysis, and storage
  • error - Comprehensive error handling with recovery strategies
  • Execution contexts for different environments (CLI, cloud, WASM)

§Examples

§Basic Usage - Preserving ast-grep Power

use thread_services::types::ParsedDocument;
use thread_services::traits::CodeAnalyzer;

async fn analyze_code(document: &ParsedDocument<impl thread_ast_engine::source::Doc>) {
    // Access underlying ast-grep functionality directly
    let root = document.ast_grep_root();
    let matches = root.root().find_all("fn $NAME($$$PARAMS) { $$$BODY }");

    // Plus codebase-level metadata
    let symbols = document.metadata().defined_symbols.keys();
    println!("Found symbols: {:?}", symbols.collect::<Vec<_>>());
}

§Codebase-Level Intelligence

use thread_services::traits::CodeAnalyzer;
use thread_services::types::{AnalysisContext, ExecutionScope};

async fn codebase_analysis(
    analyzer: &dyn CodeAnalyzer,
    documents: &[thread_services::types::ParsedDocument<impl thread_ast_engine::source::Doc>]
) -> Result<(), Box<dyn std::error::Error>> {
    let mut context = AnalysisContext::default();
    context.scope = ExecutionScope::Codebase;

    // Analyze relationships across entire codebase
    let relationships = analyzer.analyze_cross_file_relationships(documents, &context).await?;

    // This builds on ast-grep's file-level power to create codebase intelligence
    for rel in relationships {
        println!("Cross-file relationship: {:?} -> {:?}", rel.source_file, rel.target_file);
    }
    Ok(())
}

Re-exports§

pub use types::AnalysisContext;
pub use types::AnalysisDepth;
pub use types::CodeMatch;
pub use types::CrossFileRelationship;
pub use types::ExecutionScope;
pub use types::ParsedDocument;
pub use types::SupportLang;
pub use types::SupportLangErr;
pub use error::AnalysisError;
pub use error::ContextualError;
pub use error::ContextualResult;
pub use error::ErrorContextExt;
pub use error::ParseError;
pub use error::RecoverableError;
pub use error::ServiceError;
pub use error::ServiceResult;
pub use traits::AnalysisPerformanceProfile;
pub use traits::AnalyzerCapabilities;
pub use traits::CodeAnalyzer;
pub use traits::CodeParser;
pub use traits::ParserCapabilities;

Modules§

conversion
Conversion Utilities
error
Service Layer Error Types
facade
Thread Service Facade
traits
Service Layer Traits
types
Service Layer Types - Abstraction Glue for Thread

Macros§

analysis_error
Macro for creating analysis errors with context
parse_error
Macro for creating parse errors with context
storage_error
Macro for creating storage errors with context

Structs§

FileSystemContext
File system based execution context
MemoryContext
In-memory execution context for testing and WASM environments

Enums§

LegacyServiceErrorDeprecated
Legacy error type for backwards compatibility

Traits§

ExecutionContext
Abstract execution context that can provide code from various sources