-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: separate commands for changing transparency by value and delta (#…
…939)
- Loading branch information
1 parent
f564ed5
commit 110ed89
Showing
8 changed files
with
143 additions
and
114 deletions.
There are no files selected for viewing
This file contains 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 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,35 @@ | ||
use std::str::FromStr; | ||
|
||
use anyhow::bail; | ||
use serde::Serialize; | ||
|
||
/// A wrapper that indicates a value should be interpreted as a delta | ||
/// (relative change). | ||
#[derive(Debug, Clone, Copy, PartialEq, Serialize)] | ||
pub struct Delta<T> { | ||
pub inner: T, | ||
pub is_negative: bool, | ||
} | ||
|
||
impl<T: FromStr<Err = anyhow::Error>> FromStr for Delta<T> { | ||
type Err = anyhow::Error; | ||
|
||
fn from_str(unparsed: &str) -> anyhow::Result<Self> { | ||
let unparsed = unparsed.trim(); | ||
|
||
let (raw, is_negative) = match unparsed.chars().next() { | ||
Some('+') => (&unparsed[1..], false), | ||
Some('-') => (&unparsed[1..], true), | ||
// No sign means positive. | ||
_ => (unparsed, false), | ||
}; | ||
|
||
if raw.is_empty() { | ||
bail!("Empty value."); | ||
} | ||
|
||
let inner = T::from_str(raw)?; | ||
|
||
Ok(Self { inner, is_negative }) | ||
} | ||
} |
This file contains 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 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 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 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 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.