|
| 1 | +//! This module contains types and functions to support formatting specific macros. |
| 2 | +
|
| 3 | +use itertools::Itertools; |
| 4 | +use std::{fmt, str}; |
| 5 | + |
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | +use serde_json as json; |
| 8 | +use thiserror::Error; |
| 9 | + |
| 10 | +/// Defines the name of a macro. |
| 11 | +#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd, Deserialize, Serialize)] |
| 12 | +pub struct MacroName(String); |
| 13 | + |
| 14 | +impl MacroName { |
| 15 | + pub fn new(other: String) -> Self { |
| 16 | + Self(other) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +impl fmt::Display for MacroName { |
| 21 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 22 | + self.0.fmt(f) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl From<MacroName> for String { |
| 27 | + fn from(other: MacroName) -> Self { |
| 28 | + other.0 |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/// Defines a selector to match against a macro. |
| 33 | +#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd, Deserialize, Serialize)] |
| 34 | +pub enum MacroSelector { |
| 35 | + Name(MacroName), |
| 36 | + All, |
| 37 | +} |
| 38 | + |
| 39 | +impl fmt::Display for MacroSelector { |
| 40 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 41 | + match self { |
| 42 | + Self::Name(name) => name.fmt(f), |
| 43 | + Self::All => write!(f, "*"), |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl str::FromStr for MacroSelector { |
| 49 | + type Err = std::convert::Infallible; |
| 50 | + |
| 51 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 52 | + Ok(match s { |
| 53 | + "*" => MacroSelector::All, |
| 54 | + name => MacroSelector::Name(MacroName(name.to_owned())), |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/// A set of macro selectors. |
| 60 | +#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)] |
| 61 | +pub struct MacroSelectors(pub Vec<MacroSelector>); |
| 62 | + |
| 63 | +impl fmt::Display for MacroSelectors { |
| 64 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 65 | + write!(f, "{}", self.0.iter().format(", ")) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +#[derive(Error, Debug)] |
| 70 | +pub enum MacroSelectorsError { |
| 71 | + #[error("{0}")] |
| 72 | + Json(json::Error), |
| 73 | +} |
| 74 | + |
| 75 | +// This impl is needed for `Config::override_value` to work for use in tests. |
| 76 | +impl str::FromStr for MacroSelectors { |
| 77 | + type Err = MacroSelectorsError; |
| 78 | + |
| 79 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 80 | + let raw: Vec<&str> = json::from_str(s).map_err(MacroSelectorsError::Json)?; |
| 81 | + Ok(Self( |
| 82 | + raw.into_iter() |
| 83 | + .map(|raw| { |
| 84 | + MacroSelector::from_str(raw).expect("MacroSelector from_str is infallible") |
| 85 | + }) |
| 86 | + .collect(), |
| 87 | + )) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +#[cfg(test)] |
| 92 | +mod test { |
| 93 | + use super::*; |
| 94 | + use std::str::FromStr; |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn macro_names_from_str() { |
| 98 | + let macro_names = MacroSelectors::from_str(r#"["foo", "*", "bar"]"#).unwrap(); |
| 99 | + assert_eq!( |
| 100 | + macro_names, |
| 101 | + MacroSelectors( |
| 102 | + [ |
| 103 | + MacroSelector::Name(MacroName("foo".to_owned())), |
| 104 | + MacroSelector::All, |
| 105 | + MacroSelector::Name(MacroName("bar".to_owned())) |
| 106 | + ] |
| 107 | + .into_iter() |
| 108 | + .collect() |
| 109 | + ) |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + #[test] |
| 114 | + fn macro_names_display() { |
| 115 | + let macro_names = MacroSelectors::from_str(r#"["foo", "*", "bar"]"#).unwrap(); |
| 116 | + assert_eq!(format!("{}", macro_names), "foo, *, bar"); |
| 117 | + } |
| 118 | +} |
0 commit comments