///|
/// Why a plugin failed. The host makes no more calls to the plugin.
#derive(Eq, Debug)
pub suberror PluginFailure {
  /// The plugin reported a failure of this operation, with a message.
  Reported(operation~ : Operation, message~ : String)
  /// The response of this operation breaks the contract.
  BrokenContract(operation~ : Operation, error~ : MessageError)
  /// There is no response. The operation is `None` when no call was in progress.
  NoResponse(operation~ : Operation?, fault~ : TransportFault)
}

///|
pub extend PluginFailure with Eq::{equal, not_equal}

///|
pub extend PluginFailure with @debug.Debug::{to_repr}

///|
/// Text for people that describes the fault.
pub fn PluginFailure::message(self : PluginFailure) -> String {
  match self {
    Reported(operation~, message~) =>
      "the operation '\{operation}' failed: \{message}"
    BrokenContract(operation~, error~) =>
      "the operation '\{operation}' broke the contract: \{error.message()}"
    NoResponse(operation=Some(operation), fault~) =>
      "the operation '\{operation}' gave no response: \{fault.message()}"
    NoResponse(operation=None, fault~) => fault.message()
  }
}

///|
pub impl Show for PluginFailure with fn output(self, logger) {
  logger.write_string(self.message())
}

///|
pub extend PluginFailure with Show::{output, to_string}

///|
/// Why a sender gets no reply for its event.
#derive(Eq, Debug)
pub suberror SendError {
  /// The description of the plugin does not hold this event name. The plugin got
  /// no call.
  UnknownEvent(EventName)
  /// The plugin rejected the event `event`, and it continues.
  RejectedEvent(event~ : EventName, message~ : String)
  /// The plugin finished its work before it got the event.
  PluginFinished
  /// The host stopped the plugin before it answered the event.
  PluginStopped
  /// The plugin failed before it answered the event.
  PluginFailed(PluginFailure)
}

///|
pub extend SendError with Eq::{equal, not_equal}

///|
pub extend SendError with @debug.Debug::{to_repr}

///|
/// Text for people that describes the fault.
pub fn SendError::message(self : SendError) -> String {
  match self {
    UnknownEvent(event) => "The plugin does not accept the event '\{event}'"
    RejectedEvent(event~, message~) =>
      "The plugin rejected the event '\{event}': \{message}"
    PluginFinished => "The plugin finished and accepts no more events"
    PluginStopped => "The plugin stopped and accepts no more events"
    PluginFailed(failure) => "The plugin failed: \{failure.message()}"
  }
}

///|
pub impl Show for SendError with fn output(self, logger) {
  logger.write_string(self.message())
}

///|
pub extend SendError with Show::{output, to_string}