use anyhow::{Context, Result};
use std::fmt::Display;
use std::path::Path;
pub fn with_file_context<T, E>(
result: std::result::Result<T, E>,
operation: impl Display,
path: &Path,
) -> Result<T>
where
E: std::error::Error + Send + Sync + 'static,
{
result.with_context(|| format!("Failed to {operation} '{}'", path.display()))
}
pub fn with_path_context<T, E>(
result: std::result::Result<T, E>,
operation: impl Display,
path: impl Display,
) -> Result<T>
where
E: std::error::Error + Send + Sync + 'static,
{
result.with_context(|| format!("Failed to {operation} {path}"))
}
pub fn require_string_field(
args: &serde_json::Value,
field: &str,
tool_name: &str,
) -> Result<String> {
args.get(field)
.and_then(|v| v.as_str())
.map(|s| s.to_string())
.ok_or_else(|| anyhow::anyhow!("{field} is required for {tool_name}"))
}
pub fn optional_string_field(args: &serde_json::Value, field: &str) -> Option<String> {
args.get(field)
.and_then(|v| v.as_str())
.map(|s| s.to_string())
}
pub fn require_int_field(args: &serde_json::Value, field: &str, tool_name: &str) -> Result<i64> {
args.get(field)
.and_then(|v| v.as_i64())
.ok_or_else(|| anyhow::anyhow!("{field} is required for {tool_name}"))
}
pub fn require_field<T>(value: Option<T>, field: &str, tool_name: &str) -> Result<T> {
value.ok_or_else(|| anyhow::anyhow!("{field} is required for {tool_name}"))
}