pub mod blame;
pub mod context;
pub mod diff;
pub mod entities;
pub mod graph;
pub mod impact;
pub mod log;
pub mod setup;
pub mod verify;
use sem_core::parser::plugins::create_default_registry;
use sem_core::parser::registry::ParserRegistry;
use std::path::Path;
pub fn create_registry(cwd: &str) -> ParserRegistry {
let mut registry = create_default_registry();
let root = Path::new(cwd);
registry.load_semrc(root);
registry.load_gitattributes(root);
registry
}
pub fn truncate_str(s: &str, max_chars: usize) -> String {
if max_chars <= 3 {
return s.chars().take(max_chars).collect();
}
let mut last_boundary = 0;
let mut truncate_boundary = 0;
let mut count = 0;
for (i, c) in s.char_indices() {
count += 1;
if count == max_chars - 3 {
truncate_boundary = i + c.len_utf8();
}
if count == max_chars {
last_boundary = i + c.len_utf8();
break;
}
}
if count < max_chars {
s.to_string()
} else if s[last_boundary..].is_empty() {
s.to_string()
} else {
format!("{}...", &s[..truncate_boundary])
}
}
#[cfg(test)]
mod tests {
use super::truncate_str;
#[test]
fn ascii_short_string_unchanged() {
assert_eq!(truncate_str("hello", 10), "hello");
}
#[test]
fn ascii_exact_length_unchanged() {
assert_eq!(truncate_str("hello", 5), "hello");
}
#[test]
fn ascii_truncated_with_ellipsis() {
assert_eq!(truncate_str("abcdef", 5), "ab...");
}
#[test]
fn cjk_short_string_unchanged() {
assert_eq!(truncate_str("日本語", 10), "日本語");
}
#[test]
fn cjk_truncated_at_char_boundary() {
let msg = "bff側でwebsocketエラーが頻発している問題を修正";
let result = truncate_str(msg, 15);
assert_eq!(result.chars().count(), 15);
assert!(result.ends_with("..."));
}
#[test]
fn emoji_truncated_at_char_boundary() {
let msg = "🎉🚀✨ feat: add new feature with celebration";
let result = truncate_str(msg, 10);
assert_eq!(result.chars().count(), 10);
assert!(result.ends_with("..."));
}
#[test]
fn mixed_cjk_ascii_truncation() {
let msg = ":bug: bff側でwebsocketエラーが頻発している問題を修正";
let result = truncate_str(msg, 20);
assert_eq!(result.chars().count(), 20);
assert!(result.ends_with("..."));
}
#[test]
fn empty_string() {
assert_eq!(truncate_str("", 10), "");
}
#[test]
fn max_chars_zero() {
assert_eq!(truncate_str("hello", 0), "");
}
#[test]
fn max_chars_one() {
assert_eq!(truncate_str("hello", 1), "h");
}
#[test]
fn max_chars_three_with_longer_string() {
assert_eq!(truncate_str("hello", 3), "hel");
}
#[test]
fn max_chars_four_triggers_ellipsis() {
assert_eq!(truncate_str("hello", 4), "h...");
}
}