Skip to main content

vtcode_tui/utils/
file_utils.rs

1use std::path::Path;
2
3use anyhow::{Context, Result};
4
5pub fn read_file_with_context_sync(path: &Path, purpose: &str) -> Result<String> {
6    std::fs::read_to_string(path)
7        .with_context(|| format!("Failed to read {} at {}", purpose, path.display()))
8}
9
10pub fn write_file_with_context_sync(path: &Path, content: &str, purpose: &str) -> Result<()> {
11    if let Some(parent) = path.parent() {
12        std::fs::create_dir_all(parent).with_context(|| {
13            format!(
14                "Failed to create parent directory for {} at {}",
15                purpose,
16                parent.display()
17            )
18        })?;
19    }
20
21    std::fs::write(path, content)
22        .with_context(|| format!("Failed to write {} at {}", purpose, path.display()))
23}
24
25pub fn is_image_path(path: &Path) -> bool {
26    let Some(extension) = path.extension().and_then(|ext| ext.to_str()) else {
27        return false;
28    };
29
30    matches!(
31        extension.to_ascii_lowercase().as_str(),
32        "png" | "jpg" | "jpeg" | "gif" | "bmp" | "webp" | "tiff" | "tif" | "svg"
33    )
34}