microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
dotnet/autoShell/ActionResult.cs
59lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.Json; |
| 5 | using System.Text.Json.Serialization; |
| 6 | |
| 7 | namespace autoShell; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Represents the result of executing an action. |
| 11 | /// Serialized to JSON and written to stdout as the response to the caller. |
| 12 | /// </summary> |
| 13 | internal class ActionResult |
| 14 | { |
| 15 | [JsonPropertyName("id")] |
| 16 | [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] |
| 17 | public string Id { get; set; } |
| 18 | |
| 19 | [JsonPropertyName("success")] |
| 20 | public bool Success { get; init; } |
| 21 | |
| 22 | [JsonPropertyName("message")] |
| 23 | public string Message { get; init; } |
| 24 | |
| 25 | [JsonPropertyName("data")] |
| 26 | [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] |
| 27 | public JsonElement? Data { get; init; } |
| 28 | |
| 29 | /// <summary> |
| 30 | /// When true, the caller should exit the interactive loop after sending this result. |
| 31 | /// Not serialized — this is internal control flow only. |
| 32 | /// </summary> |
| 33 | [JsonIgnore] |
| 34 | public bool IsQuit { get; init; } |
| 35 | |
| 36 | /// <summary> |
| 37 | /// Creates a successful result with a message. |
| 38 | /// </summary> |
| 39 | public static ActionResult Ok(string message) => |
| 40 | new() { Success = true, Message = message }; |
| 41 | |
| 42 | /// <summary> |
| 43 | /// Creates a successful result with a message and associated data. |
| 44 | /// </summary> |
| 45 | public static ActionResult Ok(string message, JsonElement data) => |
| 46 | new() { Success = true, Message = message, Data = data }; |
| 47 | |
| 48 | /// <summary> |
| 49 | /// Creates a failure result with an error message. |
| 50 | /// </summary> |
| 51 | public static ActionResult Fail(string message) => |
| 52 | new() { Success = false, Message = message }; |
| 53 | |
| 54 | /// <summary> |
| 55 | /// Creates a successful quit result that signals the interactive loop to exit. |
| 56 | /// </summary> |
| 57 | public static ActionResult Quit() => |
| 58 | new() { Success = true, Message = "Quitting", IsQuit = true }; |
| 59 | } |
| 60 | |