Skip to content

Commit

Permalink
Use CompactString in CustomSwitch
Browse files Browse the repository at this point in the history
  • Loading branch information
russellbanks committed Feb 7, 2025
1 parent bec35ae commit 62f2b9d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ clap = { version = "4.5.28", features = ["derive", "cargo", "env"] }
clap_complete = "4.5.44"
codepage = "0.1.2"
color-eyre = { version = "0.6.3", default-features = false }
compact_str = "0.8.1"
const_format = { version = "0.2.34", features = ["derive"] }
crc32fast = "1.4.2"
crossterm = "0.28.1"
Expand Down
25 changes: 13 additions & 12 deletions src/types/custom_switch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::prompts::prompt::Prompt;
use compact_str::CompactString;
use derive_more::IntoIterator;
use serde_with::{DeserializeFromStr, SerializeDisplay};
use smallvec::SmallVec;
Expand Down Expand Up @@ -31,7 +32,7 @@ pub enum CustomSwitchError {
)]
pub struct CustomSwitch(
// Most custom switches only ever have 1 or (rarely) 2 parts
#[into_iterator(owned, ref, ref_mut)] SmallVec<[String; 2]>,
#[into_iterator(owned, ref, ref_mut)] SmallVec<[CompactString; 2]>,
);

impl CustomSwitch {
Expand All @@ -47,8 +48,8 @@ impl CustomSwitch {
"/CURRENTUSER".parse().unwrap()
}

pub fn push(&mut self, other: String) {
self.0.push(other);
pub fn push<S: Into<CompactString>>(&mut self, other: S) {
self.0.push(other.into());
}

pub fn contains(&self, other: &str) -> bool {
Expand All @@ -58,7 +59,7 @@ impl CustomSwitch {

impl Display for CustomSwitch {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
for part in itertools::intersperse(self.0.iter().map(String::as_str), " ") {
for part in itertools::intersperse(self.0.iter().map(CompactString::as_str), " ") {
f.write_str(part)?;
}
Ok(())
Expand All @@ -75,13 +76,12 @@ impl FromStr for CustomSwitch {
return Err(CustomSwitchError::TooLong);
}

let switches = s
.split(Self::DELIMITERS)
.filter(|switch| !switch.is_empty())
.map(str::to_owned)
.collect::<SmallVec<_>>();

Ok(Self(switches))
Ok(Self(
s.split(Self::DELIMITERS)
.filter(|switch| !switch.is_empty())
.map(CompactString::new)
.collect::<SmallVec<_>>(),
))
}
}

Expand All @@ -94,6 +94,7 @@ impl Prompt for CustomSwitch {
#[cfg(test)]
mod tests {
use crate::types::custom_switch::{CustomSwitch, CustomSwitchError};
use compact_str::CompactString;
use const_format::str_repeat;
use smallvec::{smallvec, SmallVec};

Expand Down Expand Up @@ -168,7 +169,7 @@ mod tests {

let mut custom_switch = ALL_USERS.parse::<CustomSwitch>().unwrap();

custom_switch.push(NO_RESTART.to_owned());
custom_switch.push(NO_RESTART);

assert!(custom_switch.contains(NO_RESTART));

Expand Down

0 comments on commit 62f2b9d

Please sign in to comment.