Skip to main content

apple_code_assistant/utils/
mod.rs

1//! Utilities: logging, clipboard, file operations, syntax highlighting
2
3mod languages;
4mod syntax_highlight;
5
6use std::path::Path;
7
8use crate::error::IoError;
9
10pub use languages::{from_extension, from_prompt, is_supported, normalize, SUPPORTED_LANGUAGES};
11pub use syntax_highlight::highlight_to_ansi;
12
13/// Initialize logging from verbosity and debug flags.
14/// Call once at startup after parsing args.
15pub fn setup_logging(verbose: bool, debug: bool) {
16    let level = if debug {
17        log::LevelFilter::Debug
18    } else if verbose {
19        log::LevelFilter::Info
20    } else {
21        log::LevelFilter::Warn
22    };
23    let _ = env_logger::Builder::from_default_env()
24        .filter_level(level)
25        .try_init();
26}
27
28/// Write content to a file. Creates parent dirs if needed.
29pub fn write_file(path: &Path, content: &str) -> Result<(), IoError> {
30    if let Some(parent) = path.parent() {
31        std::fs::create_dir_all(parent).map_err(IoError::Io)?;
32    }
33    std::fs::write(path, content).map_err(IoError::Io)?;
34    Ok(())
35}
36
37/// Read entire file as string.
38pub fn read_file(path: &Path) -> Result<String, IoError> {
39    std::fs::read_to_string(path).map_err(IoError::Io)
40}
41
42/// Copy text to system clipboard (macOS/Windows/Linux via arboard).
43pub fn copy_to_clipboard(text: &str) -> Result<(), IoError> {
44    let mut clipboard = arboard::Clipboard::new().map_err(|e| IoError::PermissionDenied(e.to_string()))?;
45    clipboard.set_text(text).map_err(|e| IoError::PermissionDenied(e.to_string()))?;
46    Ok(())
47}