Skip to content

Commit

Permalink
refactor: Avoid duplicating PascalCase formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Westerlind committed Nov 1, 2021
1 parent 2877b6a commit e02ce3b
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ use serde::de;
use serde::de::Error as Error_;
use serde_json::Value;

fn fmt_pascal_case(f: &mut std::fmt::Formatter<'_>, name: &str) -> std::fmt::Result {
for word in name.split('_') {
let mut chars = word.chars();
let first = chars.next().unwrap();
write!(f, "{}", first)?;
for rest in chars {
write!(f, "{}", rest.to_lowercase())?;
}
}
Ok(())
}

macro_rules! lsp_enum {
(impl $typ: ty { $( $(#[$attr:meta])* pub const $name: ident : $enum_type: ty = $value: expr; )* }) => {
impl $typ {
Expand All @@ -43,20 +55,10 @@ macro_rules! lsp_enum {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
$(
Self::$name => {
for word in stringify!($name).split('_') {
let mut chars = word.chars();
let first = chars.next().unwrap();
write!(f, "{}", first)?;
for rest in chars {
write!(f, "{}", rest.to_lowercase())?;
}
}
}
Self::$name => crate::fmt_pascal_case(f, stringify!($name)),
)*
_ => write!(f, "{}({})", stringify!($typ), self.0)?,
_ => write!(f, "{}({})", stringify!($typ), self.0),
}
Ok(())
}
}
}
Expand Down

0 comments on commit e02ce3b

Please sign in to comment.