pre_serve.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #[default]
  11. Abort,
  12. Warn,
  13. Continue,
  14. }
  15. /// One entry in a `before_serve` block.
  16. #[derive(Clone, Debug, Deserialize)]
  17. #[serde(untagged)]
  18. pub enum PreServeCommand {
  19. Bare(String),
  20. Configured {
  21. command: String,
  22. #[serde(default)]
  23. on_failure: OnFailure,
  24. },
  25. }
  26. impl PreServeCommand {
  27. pub fn command(&self) -> &str {
  28. match self {
  29. PreServeCommand::Bare(c) => c,
  30. PreServeCommand::Configured { command, .. } => command,
  31. }
  32. }
  33. pub fn on_failure(&self) -> OnFailure {
  34. match self {
  35. PreServeCommand::Bare(_) => OnFailure::default(),
  36. PreServeCommand::Configured { on_failure, .. } => *on_failure,
  37. }
  38. }
  39. }
  40. #[derive(Clone, Debug, Default, Deserialize)]
  41. pub struct BeforeServeTable {
  42. #[serde(default)]
  43. pub commands: Vec<PreServeCommand>,
  44. }
  45. #[derive(Clone, Debug, Default, Deserialize)]
  46. pub struct ModsTable {
  47. #[serde(default)]
  48. pub commands: Vec<PreServeCommand>,
  49. }
  50. /// Distributed execution backend — `mp`, `ray`, or absent (single-node).
  51. /// Defaults to [`None_`] when not specified in the recipe.
  52. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Deserialize)]
  53. #[serde(rename_all = "lowercase")]
  54. pub enum DistributedBackend {
  55. Mp,
  56. Ray,
  57. #[default]
  58. None_,
  59. }
  60. impl std::fmt::Display for DistributedBackend {
  61. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  62. match self {
  63. DistributedBackend::Mp => write!(f, "mp"),
  64. DistributedBackend::Ray => write!(f, "ray"),
  65. DistributedBackend::None_ => write!(f, "none"),
  66. }
  67. }
  68. }