pre_serve.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //! Types for the `before_serve` configuration and `exec_aliases` table.
  2. //!
  3. //! Lives outside `config.rs` so the latter stays under the 500-line limit
  4. //! the project enforces (see AGENTS.md).
  5. use serde::Deserialize;
  6. /// Behaviour on a non-zero exit code for a [`PreServeCommand`].
  7. #[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Deserialize)]
  8. #[serde(rename_all = "lowercase")]
  9. pub enum OnFailure {
  10. /// Abort the serve (default).
  11. #[default]
  12. Abort,
  13. /// Log a warning but continue.
  14. Warn,
  15. /// Continue silently.
  16. Continue,
  17. }
  18. /// One entry in a `before_serve` block. Either a bare command string
  19. /// (defaults to `on_failure = abort`) or a table with explicit `on_failure`.
  20. #[derive(Clone, Debug, Deserialize)]
  21. #[serde(untagged)]
  22. pub enum PreServeCommand {
  23. Bare(String),
  24. Configured {
  25. command: String,
  26. #[serde(default)]
  27. on_failure: OnFailure,
  28. },
  29. }
  30. impl PreServeCommand {
  31. pub fn command(&self) -> &str {
  32. match self {
  33. PreServeCommand::Bare(c) => c,
  34. PreServeCommand::Configured { command, .. } => command,
  35. }
  36. }
  37. pub fn on_failure(&self) -> OnFailure {
  38. match self {
  39. PreServeCommand::Bare(_) => OnFailure::default(),
  40. PreServeCommand::Configured { on_failure, .. } => *on_failure,
  41. }
  42. }
  43. }
  44. /// TOML sub-table under `[before_serve."<cluster>"]`. Currently only
  45. /// carries `commands`; reserved for future per-cluster tuning.
  46. #[derive(Clone, Debug, Default, Deserialize)]
  47. pub struct BeforeServeTable {
  48. #[serde(default)]
  49. pub commands: Vec<PreServeCommand>,
  50. }