winx_code_agent/utils/
mod.rs1pub mod bash_parser;
7pub mod display_tree;
8pub mod encoder;
9pub mod file_cache;
10pub mod mmap;
11pub mod mode_prompts;
12pub mod output_compress;
13pub mod path;
14pub mod path_prob;
15pub mod repo;
16pub mod syntax;
17pub mod workspace_stats;
18
19use crate::types::Initialize;
20use serde_json::Value;
21use tracing::debug;
22
23pub fn floor_char_boundary(s: &str, idx: usize) -> usize {
31 if idx >= s.len() {
32 return s.len();
33 }
34 let mut i = idx;
35 while i > 0 && !s.is_char_boundary(i) {
36 i -= 1;
37 }
38 i
39}
40
41pub fn test_json_parsing(json_str: &str) -> Result<(), String> {
43 let raw_json_result = serde_json::from_str::<Value>(json_str);
45 if let Err(e) = raw_json_result {
46 return Err(format!("Invalid JSON format: {e}"));
47 }
48
49 let init_result = serde_json::from_str::<Initialize>(json_str);
51 match init_result {
52 Ok(init) => {
53 debug!(
54 init_type = ?init.init_type,
55 mode_name = ?init.mode_name,
56 code_writer_config = ?init.code_writer_config,
57 task_id_to_resume = init.task_id_to_resume,
58 "Successfully parsed JSON into Initialize struct"
59 );
60 Ok(())
61 }
62 Err(e) => Err(format!("Failed to parse JSON into Initialize struct: {e}")),
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn floor_char_boundary_snaps_into_multibyte() {
72 let s = "a\u{3000}b"; assert_eq!(floor_char_boundary(s, 0), 0);
74 assert_eq!(floor_char_boundary(s, 1), 1); assert_eq!(floor_char_boundary(s, 2), 1); assert_eq!(floor_char_boundary(s, 3), 1); assert_eq!(floor_char_boundary(s, 4), 4); assert_eq!(floor_char_boundary(s, 999), s.len()); for i in 0..=s.len() + 5 {
81 let cut = floor_char_boundary(s, i);
82 let _ = &s[cut..];
83 }
84 }
85}