use bevy_brp_mcp_macros::ResultStruct;
use bevy_brp_mcp_macros::ToolFn;
use serde::Deserialize;
use serde::Serialize;
use super::TracingLevel;
use crate::error::Result;
use crate::tool::HandlerContext;
use crate::tool::HandlerResult;
use crate::tool::NoParams;
use crate::tool::ToolFn;
use crate::tool::ToolResult;
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(from = "bool", into = "bool")]
enum TraceLogFile {
Missing,
Present,
}
impl From<bool> for TraceLogFile {
fn from(value: bool) -> Self { if value { Self::Present } else { Self::Missing } }
}
impl From<TraceLogFile> for bool {
fn from(value: TraceLogFile) -> Self { matches!(value, TraceLogFile::Present) }
}
#[cfg(feature = "mcp-debug")]
#[derive(Debug, Clone, Serialize, Deserialize, ResultStruct)]
pub struct GetTraceLogPathResult {
#[to_metadata]
log_path: String,
#[to_metadata]
exists: TraceLogFile,
#[to_metadata(skip_if_none)]
file_size_bytes: Option<u64>,
#[to_message(message_template = "Trace log path: {log_path}")]
message_template: String,
}
#[cfg(feature = "mcp-debug")]
#[derive(ToolFn)]
#[tool_fn(params = "NoParams", output = "GetTraceLogPathResult")]
pub struct GetTraceLogPath;
#[cfg(feature = "mcp-debug")]
#[allow(
clippy::unused_async,
reason = "ToolFn trait requires async handler signature"
)]
async fn handle_impl(_: NoParams) -> Result<GetTraceLogPathResult> {
let log_path = TracingLevel::get_trace_log_path();
let log_path = log_path.to_string_lossy().to_string();
let (exists, file_size_bytes) = std::fs::metadata(&log_path)
.map_or((TraceLogFile::Missing, None), |metadata| {
(TraceLogFile::Present, Some(metadata.len()))
});
Ok(GetTraceLogPathResult::new(
log_path,
exists,
file_size_bytes,
))
}