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