crates/cli/git-ents/src/mutate.rs
mutate.rshistorycomment on this file
| 1 | //! Translating a reached [`ents_receive::Outcome`] into a CLI-facing |
| 2 | //! result: the commit-building mechanism itself |
| 3 | //! ([`ents_receive::propose_entity`], [`ents_receive::propose_delete`], |
| 4 | //! [`ents_receive::Identity`]) moved to `ents-receive` (kernel material, |
| 5 | //! shared by every mutation frontend — including `ents-forge`'s comment |
| 6 | //! command — not CLI-specific); this module keeps only the one thing that |
| 7 | //! is genuinely CLI-only: rendering a failure for a human. |
| 8 | |
| 9 | use ents_receive::{Outcome, TxResult}; |
| 10 | use gix_hash::ObjectId; |
| 11 | |
| 12 | use crate::error::{Error, Result}; |
| 13 | |
| 14 | /// Translate a reached [`Outcome`] into `Ok(tip)` on success or a |
| 15 | /// user-facing [`Error`] otherwise — the one place every command renders |
| 16 | /// `receive`'s result the same way. |
| 17 | /// |
| 18 | /// # Errors |
| 19 | /// |
| 20 | /// [`Error::Refused`] for a gate refusal (`gate.mandatory-hosted` |
| 21 | /// aborting on any failed verdict, or an advisory root's failed verdict a |
| 22 | /// caller chose to treat as fatal); [`Error::Stale`] for a compare-and-swap |
| 23 | /// rejection; [`Error::Redacted`] if a redacted object was refused. |
| 24 | pub fn outcome_to_result(outcome: Outcome, tip: Option<ObjectId>) -> Result<Option<ObjectId>> { |
| 25 | match outcome.result { |
| 26 | TxResult::Applied => Ok(tip), |
| 27 | TxResult::Refused => { |
| 28 | let reasons = outcome |
| 29 | .verdicts |
| 30 | .iter() |
| 31 | .filter_map(|(_, verdict)| match verdict { |
| 32 | ents_gate::Verdict::Fail(refusal) => Some(refusal.to_string()), |
| 33 | ents_gate::Verdict::Pass(_) => None, |
| 34 | }) |
| 35 | .collect::<Vec<_>>() |
| 36 | .join("; "); |
| 37 | Err(Error::Refused(reasons)) |
| 38 | } |
| 39 | TxResult::Rejected { name } => Err(Error::Stale { |
| 40 | name: name.as_bstr().to_string(), |
| 41 | }), |
| 42 | TxResult::Redacted { oid } => Err(Error::Redacted { oid }), |
| 43 | } |
| 44 | } |