refactor(FromStr): move from_str to impl FromStr where it belongs.

This commit is contained in:
Mark Schmale 2018-09-11 18:11:53 +02:00
parent 25a987e0cc
commit 9968d88d61
2 changed files with 26 additions and 15 deletions

View File

@ -1,6 +1,7 @@
extern crate nginx_log_parser; extern crate nginx_log_parser;
use nginx_log_parser::Format; use nginx_log_parser::Format;
use std::str::FromStr;
fn main() { fn main() {
let lines = vec![ let lines = vec![

View File

@ -1,4 +1,5 @@
use regex::{escape, Captures, Error as RegexError, Regex}; use regex::{escape, Captures, Error as RegexError, Regex};
use std::str::FromStr;
#[derive(Debug, Fail)] #[derive(Debug, Fail)]
pub enum FormatParserError { pub enum FormatParserError {
@ -18,6 +19,7 @@ impl<'a> Entry<'a> {
/// ```rust /// ```rust
/// # extern crate nginx_log_parser; /// # extern crate nginx_log_parser;
/// # use nginx_log_parser::Format; /// # use nginx_log_parser::Format;
/// # use std::str::FromStr;
/// # /// #
/// let format = Format::from_str("$remote_addr [$time_local] $request").unwrap(); /// 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(); /// 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 { 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<Format, FormatParserError> {
read_format(input.as_bytes())
}
/// ///
/// Reads an input line returning an optional `Entry` that contains the parsed result. /// Reads an input line returning an optional `Entry` that contains the parsed result.
/// ///
@ -64,6 +51,7 @@ impl Format {
/// ```rust /// ```rust
/// # extern crate nginx_log_parser; /// # extern crate nginx_log_parser;
/// # use nginx_log_parser::Format; /// # use nginx_log_parser::Format;
/// # use std::str::FromStr;
/// # /// #
/// let format = Format::from_str("$remote_addr [$time_local] $request").unwrap(); /// 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"); /// 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 /// ```rust
/// # extern crate nginx_log_parser; /// # extern crate nginx_log_parser;
/// # use nginx_log_parser::Format; /// # use nginx_log_parser::Format;
/// # use std::str::FromStr;
/// # /// #
/// let format = Format::from_str("$remote_addr [$time_local] $request").unwrap(); /// let format = Format::from_str("$remote_addr [$time_local] $request").unwrap();
/// assert!(format.parse("this does not work").is_none()); /// 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<Format, FormatParserError> {
read_format(input.as_bytes())
}
}
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub enum FormatPart { pub enum FormatPart {
Variable(String), Variable(String),
@ -188,6 +197,7 @@ mod test {
use format::Format; use format::Format;
use format::FormatPart::Fixed; use format::FormatPart::Fixed;
use format::FormatPart::Variable; use format::FormatPart::Variable;
use std::str::FromStr;
#[test] #[test]
fn test_parse_format() { fn test_parse_format() {