Skip to main content

tokio_cast/
types.rs

1use serde::{Deserialize, Serialize};
2use std::path::PathBuf;
3
4/// Supported source languages.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum Language {
7    Rust,
8}
9
10impl Language {
11    /// File extensions recognised for this language.
12    pub fn extensions(&self) -> &[&str] {
13        match self {
14            Language::Rust => &["rs"],
15        }
16    }
17
18    /// Display name.
19    pub fn name(&self) -> &str {
20        match self {
21            Language::Rust => "rust",
22        }
23    }
24}
25
26/// Owned representation of a tree-sitter AST node.
27///
28/// Unlike `tree_sitter::Node`, this is `Send + 'static` and can be passed
29/// through actor mailboxes.
30#[derive(Debug, Clone)]
31pub(crate) struct AstNode {
32    pub kind: String,
33    pub start_byte: usize,
34    pub end_byte: usize,
35    pub children: Vec<AstNode>,
36}
37
38impl AstNode {
39    /// Count non-whitespace characters covered by this node.
40    pub fn non_ws_size(&self, source: &str) -> usize {
41        source[self.start_byte..self.end_byte]
42            .chars()
43            .filter(|c| !c.is_whitespace())
44            .count()
45    }
46
47    /// Recursively convert a tree-sitter `Node` into an owned `AstNode`.
48    pub fn from_tree_sitter(node: tree_sitter::Node) -> Self {
49        let children = (0..node.child_count())
50            .map(|i| AstNode::from_tree_sitter(node.child(i).unwrap()))
51            .collect();
52        AstNode {
53            kind: node.kind().to_string(),
54            start_byte: node.start_byte(),
55            end_byte: node.end_byte(),
56            children,
57        }
58    }
59}
60
61/// A chunk of source code produced by the CAST algorithm.
62#[derive(Debug, Clone, Serialize, Deserialize)]
63pub struct Chunk {
64    /// Source text of this chunk.
65    pub content: String,
66    /// Byte offset where this chunk starts in the original source.
67    pub start_byte: usize,
68    /// Byte offset where this chunk ends in the original source.
69    pub end_byte: usize,
70    /// Top-level AST node kinds contained in this chunk.
71    pub node_kinds: Vec<String>,
72    /// Non-whitespace character count.
73    pub size: usize,
74}
75
76/// A fully indexed chunk with file-level metadata.
77#[derive(Debug, Clone, Serialize, Deserialize)]
78pub struct IndexEntry {
79    pub file_path: PathBuf,
80    pub language: String,
81    pub chunk_index: usize,
82    pub total_chunks: usize,
83    pub content: String,
84    pub start_byte: usize,
85    pub end_byte: usize,
86    pub line_start: usize,
87    pub line_end: usize,
88    pub node_kinds: Vec<String>,
89    pub size: usize,
90}