-
-
Notifications
You must be signed in to change notification settings - Fork 49
feat(spec): add effect= to declare what a command does to the world #739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| use std::fmt::{self, Display}; | ||
|
|
||
| use serde::Serialize; | ||
| use strum::{Display as StrumDisplay, EnumString}; | ||
|
|
||
| /// What running a command does to the world. | ||
| /// | ||
| /// This is a coarse, three-way classification rather than a permission model. | ||
| /// It exists so a spec can distinguish "safe to run to find something out" from | ||
| /// "changes state" from "may destroy something", which is the distinction | ||
| /// consumers keep reinventing: | ||
| /// | ||
| /// - documentation and `--help` can mark destructive commands | ||
| /// - a shell wrapper can require confirmation | ||
| /// - an AI coding agent can be given an allowlist of read-only commands rather | ||
| /// than asking about every invocation | ||
| /// | ||
| /// It is deliberately not inherited by subcommands. `git remote` and | ||
| /// `git remote remove` do different things, and silently inheriting an effect | ||
| /// from a parent would make the strictest reading of a spec the wrong one. | ||
| #[derive(Debug, Copy, Clone, PartialEq, Eq, EnumString, StrumDisplay, Serialize)] | ||
| #[strum(serialize_all = "snake_case")] | ||
| #[serde(rename_all = "snake_case")] | ||
| pub enum SpecCommandEffect { | ||
| /// Only inspects state. Running it twice is the same as running it once, | ||
| /// and not running it changes nothing. | ||
| Read, | ||
| /// Creates or modifies state, but does not remove anything the user cannot | ||
| /// recreate by running another command. | ||
| Write, | ||
| /// May delete or irreversibly overwrite something. Deserves a confirmation | ||
| /// prompt. | ||
| Destructive, | ||
| } | ||
|
|
||
| impl SpecCommandEffect { | ||
| pub fn as_str(&self) -> &'static str { | ||
| match self { | ||
| Self::Read => "read", | ||
| Self::Write => "write", | ||
| Self::Destructive => "destructive", | ||
| } | ||
| } | ||
|
|
||
| /// Human-readable label used in generated documentation. | ||
| pub fn label(&self) -> &'static str { | ||
| match self { | ||
| Self::Read => "read-only", | ||
| Self::Write => "modifies state", | ||
| Self::Destructive => "destructive", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// The set of values accepted by `effect=`, for error messages. | ||
| pub(crate) const EFFECT_VALUES: &str = "read, write, destructive"; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct ParseEffectError(pub String); | ||
|
|
||
| impl Display for ParseEffectError { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| write!( | ||
| f, | ||
| "unknown effect {:?}, expected one of: {EFFECT_VALUES}", | ||
| self.0 | ||
| ) | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use std::str::FromStr; | ||
|
|
||
| #[test] | ||
| fn test_parse() { | ||
| assert_eq!( | ||
| SpecCommandEffect::from_str("read").unwrap(), | ||
| SpecCommandEffect::Read | ||
| ); | ||
| assert_eq!( | ||
| SpecCommandEffect::from_str("destructive").unwrap(), | ||
| SpecCommandEffect::Destructive | ||
| ); | ||
| assert!(SpecCommandEffect::from_str("readonly").is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_display_roundtrips() { | ||
| for effect in [ | ||
| SpecCommandEffect::Read, | ||
| SpecCommandEffect::Write, | ||
| SpecCommandEffect::Destructive, | ||
| ] { | ||
| assert_eq!( | ||
| SpecCommandEffect::from_str(&effect.to_string()).unwrap(), | ||
| effect | ||
| ); | ||
| assert_eq!(effect.to_string(), effect.as_str()); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new template branches for
read,write, anddestructiveare not exercised by the existing Markdown snapshots because the shared test spec declares no effects. Add cases for all three values so changes to enum serialization or template comparisons cannot silently produce incorrect labels.Knowledge Base Used: Docs generation: rendering a Spec into markdown, manpages, and CLI help
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!