Skip to main content

winx_code_agent/utils/
mod.rs

1//! Utility modules for the Winx application.
2//!
3//! This module contains various utility functions and types used throughout
4//! the application, such as file and path handling, repository analysis, etc.
5
6pub mod bash_parser;
7pub mod display_tree;
8pub mod encoder;
9pub mod mmap;
10pub mod mode_prompts;
11pub mod output_compress;
12pub mod path;
13pub mod path_prob;
14pub mod repo;
15pub mod syntax;
16pub mod workspace_stats;
17
18use crate::types::Initialize;
19use serde_json::Value;
20use tracing::debug;
21
22/// Largest index `<= idx` that is a char boundary of `s`.
23///
24/// Offsets derived from byte arithmetic over possibly-multibyte text (UTF-8
25/// CJK/emoji/box-drawing, the prompt glyphs, NBSP indentation) can land in the
26/// middle of a code point. Slicing there panics. Snap down to the nearest
27/// boundary so `&s[floor_char_boundary(s, idx)..]` is always valid. For ASCII
28/// input this is the identity, so hot paths keep their existing behavior.
29pub fn floor_char_boundary(s: &str, idx: usize) -> usize {
30    if idx >= s.len() {
31        return s.len();
32    }
33    let mut i = idx;
34    while i > 0 && !s.is_char_boundary(i) {
35        i -= 1;
36    }
37    i
38}
39
40/// Debug helper to test JSON parsing of an Initialize request
41pub fn test_json_parsing(json_str: &str) -> Result<(), String> {
42    // First, try to parse as raw JSON to see if the format is valid
43    let raw_json_result = serde_json::from_str::<Value>(json_str);
44    if let Err(e) = raw_json_result {
45        return Err(format!("Invalid JSON format: {e}"));
46    }
47
48    // Now try to parse into our Initialize struct
49    let init_result = serde_json::from_str::<Initialize>(json_str);
50    match init_result {
51        Ok(init) => {
52            debug!(
53                init_type = ?init.init_type,
54                mode_name = ?init.mode_name,
55                code_writer_config = ?init.code_writer_config,
56                task_id_to_resume = init.task_id_to_resume,
57                "Successfully parsed JSON into Initialize struct"
58            );
59            Ok(())
60        }
61        Err(e) => Err(format!("Failed to parse JSON into Initialize struct: {e}")),
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn floor_char_boundary_snaps_into_multibyte() {
71        let s = "a\u{3000}b"; // 'a' (1B) + ideographic space (3B) + 'b' (1B)
72        assert_eq!(floor_char_boundary(s, 0), 0);
73        assert_eq!(floor_char_boundary(s, 1), 1); // boundary before the wide char
74        assert_eq!(floor_char_boundary(s, 2), 1); // mid code point -> snap back to 1
75        assert_eq!(floor_char_boundary(s, 3), 1); // still mid -> 1
76        assert_eq!(floor_char_boundary(s, 4), 4); // boundary after the wide char
77        assert_eq!(floor_char_boundary(s, 999), s.len()); // past end -> len
78                                                          // every returned index must be sliceable without panicking
79        for i in 0..=s.len() + 5 {
80            let cut = floor_char_boundary(s, i);
81            let _ = &s[cut..];
82        }
83    }
84}