Skip to main content

seekr_code/parser/
mod.rs

1//! Code parser module.
2//!
3//! Uses Tree-sitter for AST parsing and semantic chunking of source code.
4
5pub mod chunker;
6pub mod summary;
7pub mod treesitter;
8
9use std::ops::Range;
10use std::path::PathBuf;
11
12/// The kind of a code chunk.
13#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
14pub enum ChunkKind {
15    Function,
16    Method,
17    Class,
18    Struct,
19    Enum,
20    Interface,
21    Module,
22    /// Fallback for chunks that don't match any specific kind.
23    Block,
24}
25
26/// A semantic chunk of code extracted from a source file.
27#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
28pub struct CodeChunk {
29    /// Unique identifier for this chunk.
30    pub id: u64,
31
32    /// Path to the source file.
33    pub file_path: PathBuf,
34
35    /// Programming language.
36    pub language: String,
37
38    /// Kind of code construct.
39    pub kind: ChunkKind,
40
41    /// Name of the construct (e.g., function name).
42    pub name: Option<String>,
43
44    /// Full signature (e.g., `fn foo(x: i32) -> String`).
45    pub signature: Option<String>,
46
47    /// Documentation comment, if any.
48    pub doc_comment: Option<String>,
49
50    /// The full source text of this chunk.
51    pub body: String,
52
53    /// Byte range in the original file.
54    pub byte_range: Range<usize>,
55
56    /// Line range in the original file (0-indexed).
57    pub line_range: Range<usize>,
58}
59
60/// Result of parsing a single file.
61#[derive(Debug)]
62pub struct ParseResult {
63    /// Code chunks extracted from the file.
64    pub chunks: Vec<CodeChunk>,
65
66    /// The detected language.
67    pub language: String,
68}
69
70impl std::fmt::Display for ChunkKind {
71    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72        match self {
73            ChunkKind::Function => write!(f, "function"),
74            ChunkKind::Method => write!(f, "method"),
75            ChunkKind::Class => write!(f, "class"),
76            ChunkKind::Struct => write!(f, "struct"),
77            ChunkKind::Enum => write!(f, "enum"),
78            ChunkKind::Interface => write!(f, "interface"),
79            ChunkKind::Module => write!(f, "module"),
80            ChunkKind::Block => write!(f, "block"),
81        }
82    }
83}