Skip to main content

claude_rust_tools/infrastructure/
cron_create_tool.rs

1use claude_rust_errors::AppResult;
2use claude_rust_types::{PermissionLevel, Tool};
3use serde_json::{Value, json};
4
5/// Tool to create a cron job.
6///
7/// TODO: Phase 4 — integrate with real cron scheduler.
8pub struct CronCreateTool;
9
10impl CronCreateTool {
11    pub fn new() -> Self {
12        Self
13    }
14}
15
16#[async_trait::async_trait]
17impl Tool for CronCreateTool {
18    fn name(&self) -> &str {
19        "cron_create"
20    }
21
22    fn description(&self) -> &str {
23        "Create a new cron job with a schedule expression and command."
24    }
25
26    fn input_schema(&self) -> Value {
27        json!({
28            "type": "object",
29            "properties": {
30                "schedule": {
31                    "type": "string",
32                    "description": "Cron expression (e.g. \"0 */5 * * *\")"
33                },
34                "command": {
35                    "type": "string",
36                    "description": "Command to execute on the schedule"
37                },
38                "name": {
39                    "type": "string",
40                    "description": "Optional human-readable name for the cron job"
41                }
42            },
43            "required": ["schedule", "command"]
44        })
45    }
46
47    fn permission_level(&self) -> PermissionLevel {
48        PermissionLevel::Dangerous
49    }
50
51    async fn execute(&self, input: Value) -> AppResult<String> {
52        let schedule = input
53            .get("schedule")
54            .and_then(|v| v.as_str())
55            .ok_or_else(|| claude_rust_errors::AppError::Tool("missing 'schedule' field".into()))?;
56
57        let command = input
58            .get("command")
59            .and_then(|v| v.as_str())
60            .ok_or_else(|| claude_rust_errors::AppError::Tool("missing 'command' field".into()))?;
61
62        let name = input.get("name").and_then(|v| v.as_str());
63
64        // TODO: Phase 4 — replace stub with real cron scheduler integration
65        let cron_id = format!("cron-{:08x}", rand_id());
66
67        tracing::info!(schedule, command, ?name, cron_id, "creating cron job (stub)");
68
69        Ok(format!(
70            "Cron job created.\n  id: {cron_id}\n  schedule: {schedule}\n  command: {command}{}",
71            name.map(|n| format!("\n  name: {n}")).unwrap_or_default()
72        ))
73    }
74}
75
76fn rand_id() -> u32 {
77    use std::collections::hash_map::DefaultHasher;
78    use std::hash::{Hash, Hasher};
79    let mut h = DefaultHasher::new();
80    std::time::SystemTime::now().hash(&mut h);
81    h.finish() as u32
82}