1use anyhow::{Context, Result};
4use serde::{Deserialize, Serialize};
5use std::path::{Path, PathBuf};
6use tokio::fs;
7
8pub async fn ensure_dir_exists(path: &Path) -> Result<()> {
10 if !path.exists() {
11 fs::create_dir_all(path)
12 .await
13 .with_context(|| format!("Failed to create directory: {}", path.display()))?;
14 }
15 Ok(())
16}
17
18pub async fn read_file_with_context(path: &Path, context: &str) -> Result<String> {
20 fs::read_to_string(path)
21 .await
22 .with_context(|| format!("Failed to read {}: {}", context, path.display()))
23}
24
25pub async fn write_file_with_context(path: &Path, content: &str, context: &str) -> Result<()> {
27 if let Some(parent) = path.parent() {
28 ensure_dir_exists(parent).await?;
29 }
30 fs::write(path, content)
31 .await
32 .with_context(|| format!("Failed to write {}: {}", context, path.display()))
33}
34
35pub async fn write_json_file<T: Serialize>(path: &Path, data: &T) -> Result<()> {
37 let json = serde_json::to_string_pretty(data)
38 .with_context(|| format!("Failed to serialize data for {}", path.display()))?;
39
40 write_file_with_context(path, &json, "JSON data").await
41}
42
43pub async fn read_json_file<T: for<'de> Deserialize<'de>>(path: &Path) -> Result<T> {
45 let content = read_file_with_context(path, "JSON file").await?;
46
47 serde_json::from_str(&content)
48 .with_context(|| format!("Failed to parse JSON from {}", path.display()))
49}
50
51pub fn parse_json_with_context<T: for<'de> Deserialize<'de>>(
53 content: &str,
54 context: &str,
55) -> Result<T> {
56 serde_json::from_str(content).with_context(|| format!("Failed to parse JSON from {}", context))
57}
58
59pub fn serialize_json_with_context<T: Serialize>(data: &T, context: &str) -> Result<String> {
61 serde_json::to_string(data).with_context(|| format!("Failed to serialize JSON for {}", context))
62}
63
64pub fn serialize_json_pretty_with_context<T: Serialize>(data: &T, context: &str) -> Result<String> {
66 serde_json::to_string_pretty(data)
67 .with_context(|| format!("Failed to pretty-serialize JSON for {}", context))
68}
69
70#[must_use]
76#[inline]
77pub fn try_parse_json<T: for<'de> Deserialize<'de>>(input: &str) -> Option<T> {
78 serde_json::from_str(input).ok()
79}
80
81#[must_use]
86#[inline]
87pub fn try_parse_json_value(input: &str) -> Option<serde_json::Value> {
88 serde_json::from_str(input).ok()
89}
90
91#[inline]
96pub fn parse_json_or_default<T: for<'de> Deserialize<'de> + Default>(
97 input: &str,
98 label: &str,
99) -> T {
100 serde_json::from_str(input).unwrap_or_else(|err| {
101 tracing::debug!(label, %err, "JSON parse failed, using default");
102 T::default()
103 })
104}
105
106pub fn canonicalize_with_context(path: &Path, context: &str) -> Result<PathBuf> {
108 path.canonicalize().with_context(|| {
109 format!(
110 "Failed to canonicalize {} path: {}",
111 context,
112 path.display()
113 )
114 })
115}
116
117pub fn ensure_dir_exists_sync(path: &Path) -> Result<()> {
121 if !path.exists() {
122 std::fs::create_dir_all(path)
123 .with_context(|| format!("Failed to create directory: {}", path.display()))?;
124 }
125 Ok(())
126}
127
128pub fn read_file_with_context_sync(path: &Path, context: &str) -> Result<String> {
130 std::fs::read_to_string(path)
131 .with_context(|| format!("Failed to read {}: {}", context, path.display()))
132}
133
134pub fn write_file_with_context_sync(path: &Path, content: &str, context: &str) -> Result<()> {
136 if let Some(parent) = path.parent() {
137 ensure_dir_exists_sync(parent)?;
138 }
139 std::fs::write(path, content)
140 .with_context(|| format!("Failed to write {}: {}", context, path.display()))
141}
142
143pub fn write_json_file_sync<T: Serialize>(path: &Path, data: &T) -> Result<()> {
145 let json = serde_json::to_string_pretty(data)
146 .with_context(|| format!("Failed to serialize data for {}", path.display()))?;
147
148 write_file_with_context_sync(path, &json, "JSON data")
149}
150
151pub fn read_json_file_sync<T: for<'de> Deserialize<'de>>(path: &Path) -> Result<T> {
153 let content = read_file_with_context_sync(path, "JSON file")?;
154
155 serde_json::from_str(&content)
156 .with_context(|| format!("Failed to parse JSON from {}", path.display()))
157}
158
159pub fn is_image_path(path: &Path) -> bool {
161 let Some(extension) = path.extension().and_then(|ext| ext.to_str()) else {
162 return false;
163 };
164
165 matches!(
166 extension,
167 _ if extension.eq_ignore_ascii_case("png")
168 || extension.eq_ignore_ascii_case("jpg")
169 || extension.eq_ignore_ascii_case("jpeg")
170 || extension.eq_ignore_ascii_case("gif")
171 || extension.eq_ignore_ascii_case("bmp")
172 || extension.eq_ignore_ascii_case("webp")
173 || extension.eq_ignore_ascii_case("tiff")
174 || extension.eq_ignore_ascii_case("tif")
175 || extension.eq_ignore_ascii_case("svg")
176 )
177}