diff --git a/examples/example.rs b/examples/example.rs index 2db7d95..fd87108 100644 --- a/examples/example.rs +++ b/examples/example.rs @@ -1,6 +1,7 @@ extern crate nginx_log_parser; use nginx_log_parser::Format; +use std::str::FromStr; fn main() { let lines = vec![ diff --git a/src/format.rs b/src/format.rs index b2755d7..380323b 100644 --- a/src/format.rs +++ b/src/format.rs @@ -1,4 +1,5 @@ use regex::{escape, Captures, Error as RegexError, Regex}; +use std::str::FromStr; #[derive(Debug, Fail)] pub enum FormatParserError { @@ -18,6 +19,7 @@ impl<'a> Entry<'a> { /// ```rust /// # extern crate nginx_log_parser; /// # use nginx_log_parser::Format; + /// # use std::str::FromStr; /// # /// let format = Format::from_str("$remote_addr [$time_local] $request").unwrap(); /// let entry = format.parse("1.2.3.4 [11/Sep/2018:08:44:17 +0000] GET / HTTP/1.1").unwrap(); @@ -42,21 +44,6 @@ pub struct Format { } impl Format { - /// - /// Reads a format string in nginx-like syntax and creates a Format from it - /// - /// # Example - /// ```rust - /// # extern crate nginx_log_parser; - /// # use nginx_log_parser::Format; - /// # - /// let pattern = "$remote_addr [$time_local] $request"; - /// let format = Format::from_str(pattern).expect("could not parse format string"); - /// ``` - pub fn from_str(input: &str) -> Result { - read_format(input.as_bytes()) - } - /// /// Reads an input line returning an optional `Entry` that contains the parsed result. /// @@ -64,6 +51,7 @@ impl Format { /// ```rust /// # extern crate nginx_log_parser; /// # use nginx_log_parser::Format; + /// # use std::str::FromStr; /// # /// let format = Format::from_str("$remote_addr [$time_local] $request").unwrap(); /// let entry = format.parse("1.2.3.4 [11/Sep/2018:08:44:17 +0000] GET / HTTP/1.1"); @@ -76,6 +64,7 @@ impl Format { /// ```rust /// # extern crate nginx_log_parser; /// # use nginx_log_parser::Format; + /// # use std::str::FromStr; /// # /// let format = Format::from_str("$remote_addr [$time_local] $request").unwrap(); /// assert!(format.parse("this does not work").is_none()); @@ -97,6 +86,26 @@ impl Format { } } +impl FromStr for Format { + type Err = FormatParserError; + + /// + /// Reads a format string in nginx-like syntax and creates a Format from it + /// + /// # Example + /// ```rust + /// # extern crate nginx_log_parser; + /// # use nginx_log_parser::Format; + /// # use std::str::FromStr; + /// # + /// let pattern = "$remote_addr [$time_local] $request"; + /// let format = Format::from_str(pattern).expect("could not parse format string"); + /// ``` + fn from_str(input: &str) -> Result { + read_format(input.as_bytes()) + } +} + #[derive(Debug, Eq, PartialEq)] pub enum FormatPart { Variable(String), @@ -188,6 +197,7 @@ mod test { use format::Format; use format::FormatPart::Fixed; use format::FormatPart::Variable; + use std::str::FromStr; #[test] fn test_parse_format() {