lib.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //! codegraph-kernel — native extraction kernel (napi-rs).
  2. //!
  3. //! Replaces ONLY the parse+extract walk inside the parse workers, behind the
  4. //! existing `ExtractionResult` contract. Input `(filePath, content, language)`
  5. //! per file; output flat typed buffers — one boundary crossing per file.
  6. //! Everything downstream (resolution, synthesis, frameworks, MCP) is
  7. //! untouched and consumes the decoded result exactly as before.
  8. //!
  9. //! Calls are synchronous by design: the existing `ParseWorkerPool` workers
  10. //! already parallelize per-file, so each worker thread drives its own kernel
  11. //! call (do NOT rebuild the pool on the Rust side — see the migration plan §3).
  12. //!
  13. //! Per-language extraction lives in a dedicated walker module (tsjs/ for
  14. //! typescript/tsx/javascript/jsx) that mirrors the TS extractor for behavioral
  15. //! parity — verified by scripts/kernel-parity.mjs and the §5 gate.
  16. #![deny(clippy::all)]
  17. mod buffers;
  18. mod docstring;
  19. mod ids;
  20. mod go;
  21. mod java;
  22. mod langs;
  23. mod textutil;
  24. mod python;
  25. mod tsjs;
  26. use napi::bindgen_prelude::*;
  27. use napi_derive::napi;
  28. /// The five flat tables for one file. See buffers.rs for the byte layout;
  29. /// `src/extraction/kernel/layout.ts` is the TS mirror.
  30. #[napi(object)]
  31. pub struct ExtractBuffers {
  32. pub meta: Buffer,
  33. pub nodes: Buffer,
  34. pub edges: Buffer,
  35. pub refs: Buffer,
  36. pub arena: Buffer,
  37. }
  38. /// Wire-contract description — the TS loader verifies this against
  39. /// src/types.ts before routing anything to the kernel, so an out-of-date
  40. /// `.node` degrades to the wasm path instead of mis-decoding.
  41. #[napi(object)]
  42. pub struct ContractInfo {
  43. pub abi_version: u32,
  44. pub kernel_version: String,
  45. pub node_kinds: Vec<String>,
  46. pub edge_kinds: Vec<String>,
  47. /// Languages this binary can extract (routing is still TS-side policy).
  48. pub languages: Vec<String>,
  49. }
  50. /// Grammar identity for the grammar-source-parity gate: the wasm grammar and
  51. /// the native grammar must expose identical node-kind/field tables, or
  52. /// kernel-vs-fallback routing would be non-deterministic.
  53. #[napi(object)]
  54. pub struct GrammarInfo {
  55. pub abi_version: u32,
  56. pub node_kind_count: u32,
  57. pub field_count: u32,
  58. pub node_kinds: Vec<String>,
  59. pub field_names: Vec<String>,
  60. }
  61. #[napi]
  62. pub fn contract_info() -> ContractInfo {
  63. ContractInfo {
  64. abi_version: buffers::KERNEL_ABI_VERSION as u32,
  65. kernel_version: env!("CARGO_PKG_VERSION").to_string(),
  66. node_kinds: buffers::NODE_KINDS.iter().map(|s| s.to_string()).collect(),
  67. edge_kinds: buffers::EDGE_KINDS.iter().map(|s| s.to_string()).collect(),
  68. languages: langs::LANGUAGES.iter().map(|s| s.to_string()).collect(),
  69. }
  70. }
  71. #[napi]
  72. pub fn grammar_info(language: String) -> Option<GrammarInfo> {
  73. let lang = langs::grammar_for(&language)?;
  74. let node_kind_count = lang.node_kind_count();
  75. let field_count = lang.field_count();
  76. let node_kinds = (0..node_kind_count)
  77. .map(|i| lang.node_kind_for_id(i as u16).unwrap_or("").to_string())
  78. .collect();
  79. // Field ids are 1-based in tree-sitter.
  80. let field_names = (1..=field_count)
  81. .map(|i| lang.field_name_for_id(i as u16).unwrap_or("").to_string())
  82. .collect();
  83. Some(GrammarInfo {
  84. abi_version: lang.abi_version() as u32,
  85. node_kind_count: node_kind_count as u32,
  86. field_count: field_count as u32,
  87. node_kinds,
  88. field_names,
  89. })
  90. }
  91. #[napi]
  92. pub fn extract_file(file_path: String, content: String, language: String) -> Result<ExtractBuffers> {
  93. let out = match language.as_str() {
  94. "java" => java::extract(&file_path, &content).map_err(Error::from_reason)?,
  95. "python" => python::extract(&file_path, &content).map_err(Error::from_reason)?,
  96. "go" => go::extract(&file_path, &content).map_err(Error::from_reason)?,
  97. _ => tsjs::extract(&file_path, &content, &language).map_err(Error::from_reason)?,
  98. };
  99. Ok(ExtractBuffers {
  100. meta: out.meta.into(),
  101. nodes: out.nodes.into(),
  102. edges: out.edges.into(),
  103. refs: out.refs.into(),
  104. arena: out.arena.into(),
  105. })
  106. }