- input.chars().filter(|&c| c == '\n').count()
-}
-
-// Macro for deriving implementations of Serialize/Deserialize for enums
-#[macro_export]
-macro_rules! impl_enum_serialize_and_deserialize {
- ( $e:ident, $( $x:ident ),* ) => {
- impl ::serde::ser::Serialize for $e {
- fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
- where S: ::serde::ser::Serializer
- {
- use serde::ser::Error;
-
- // We don't know whether the user of the macro has given us all options.
- #[allow(unreachable_patterns)]
- match *self {
- $(
- $e::$x => serializer.serialize_str(stringify!($x)),
- )*
- _ => {
- Err(S::Error::custom(format!("Cannot serialize {:?}", self)))
- }
- }
- }
- }
-
- impl<'de> ::serde::de::Deserialize<'de> for $e {
- fn deserialize<D>(d: D) -> Result<Self, D::Error>
- where D: ::serde::Deserializer<'de> {
- use serde::de::{Error, Visitor};
- use std::marker::PhantomData;
- use std::fmt;
- struct StringOnly<T>(PhantomData<T>);
- impl<'de, T> Visitor<'de> for StringOnly<T>
- where T: ::serde::Deserializer<'de> {
- type Value = String;
- fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
- formatter.write_str("string")
- }
- fn visit_str<E>(self, value: &str) -> Result<String, E> {
- Ok(String::from(value))
- }
- }
- let s = d.deserialize_string(StringOnly::<D>(PhantomData))?;
- $(
- if stringify!($x).eq_ignore_ascii_case(&s) {
- return Ok($e::$x);
- }
- )*
- static ALLOWED: &'static[&str] = &[$(stringify!($x),)*];
- Err(D::Error::unknown_variant(&s, ALLOWED))
- }
- }
-
- impl ::std::str::FromStr for $e {
- type Err = &'static str;
-
- fn from_str(s: &str) -> Result<Self, Self::Err> {
- $(
- if stringify!($x).eq_ignore_ascii_case(s) {
- return Ok($e::$x);
- }
- )*
- Err("Bad variant")
- }
- }
-
- impl ::config::ConfigType for $e {
- fn doc_hint() -> String {
- let mut variants = Vec::new();
- $(
- variants.push(stringify!($x));
- )*
- format!("[{}]", variants.join("|"))
- }
- }
- };
-}
-
-macro_rules! msg {
- ($($arg:tt)*) => (
- match writeln!(&mut ::std::io::stderr(), $($arg)* ) {
- Ok(_) => {},
- Err(x) => panic!("Unable to write to stderr: {}", x),
- }
- )