#pragma once #include #include // --------------------------------------------------------------------------- // CommandResult // // Standardised return type from every ICommandHandler::execute() call. // Replaces ad-hoc string building scattered across CommandHandler.cpp. // // Use the static factory methods — they read clearly at the call site: // // return CommandResult::ok("playback", "Command executed"); // return CommandResult::ok("relay.get_config", doc); // return CommandResult::err("relay.test_output", "Player must be stopped first"); // --------------------------------------------------------------------------- struct CommandResult { enum class Status { SUCCESS, ERROR, INFO }; Status status = Status::ERROR; String type; // echoes the command name back to the caller String payload; // human-readable message (empty is fine for data responses) JsonDocument data; // structured data payload (optional) bool hasData = false; // ----------------------------------------------------------------------- // Factory methods // ----------------------------------------------------------------------- // Simple success with an optional message static CommandResult ok(const String& type, const String& msg = "") { CommandResult r; r.status = Status::SUCCESS; r.type = type; r.payload = msg; return r; } // Success with a structured JSON data payload static CommandResult ok(const String& type, const JsonDocument& d) { CommandResult r; r.status = Status::SUCCESS; r.type = type; r.data.set(d); r.hasData = true; return r; } // Error with a required message static CommandResult err(const String& type, const String& msg) { CommandResult r; r.status = Status::ERROR; r.type = type; r.payload = msg; return r; } // Info / event (used for unsolicited broadcasts) static CommandResult info(const String& type, const String& msg = "") { CommandResult r; r.status = Status::INFO; r.type = type; r.payload = msg; return r; } // Convenience bool isOk() const { return status == Status::SUCCESS; } bool isErr() const { return status == Status::ERROR; } };