LogikalResponse.cs 982 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Collections.Generic;
  3. using Newtonsoft.Json;
  4. namespace InABox.Logikal
  5. {
  6. public abstract class LogikalResponse : LogikalObject
  7. {
  8. public new abstract string ToString();
  9. public LogikalResponse Always(Action<LogikalResponse> handler)
  10. {
  11. handler?.Invoke(this);
  12. return this;
  13. }
  14. public LogikalResponse Error(Action<LogikalErrorResponse> handler, LogikalStatus status = LogikalStatus.IsError)
  15. {
  16. if (this is LogikalErrorResponse error && status.HasFlag(error.Status))
  17. {
  18. handler?.Invoke(error);
  19. return null;
  20. }
  21. return this;
  22. }
  23. public T Success<T>(Action<T> handler) where T : LogikalResponse
  24. {
  25. if (this is T response)
  26. {
  27. handler?.Invoke(response);
  28. return response;
  29. }
  30. return null;
  31. }
  32. }
  33. }