From 57c0d4e130a67286f98ec0f875703e4127de8eac Mon Sep 17 00:00:00 2001 From: Paul Masurel Date: Fri, 4 Nov 2016 14:48:14 +0900 Subject: [PATCH] NOBUG Added unit test, removed explanation.rs --- src/query/explanation.rs | 88 ---------------------------------------- src/query/mod.rs | 2 - src/schema/mod.rs | 23 +++++++++-- 3 files changed, 20 insertions(+), 93 deletions(-) delete mode 100644 src/query/explanation.rs diff --git a/src/query/explanation.rs b/src/query/explanation.rs deleted file mode 100644 index 51fccefb4..000000000 --- a/src/query/explanation.rs +++ /dev/null @@ -1,88 +0,0 @@ -use std::fmt; -use std::iter; - -/// Tree representing the expression of the score of document. -/// The explanation is organized as follows. -/// -/// - **val** is the value of the expression -/// - **formula** is a string representing a math formula. -/// The formula may contain named sub expressions. These subexpression -/// should be marked ``. -/// - **description** is a short markdown text that may help the user to -/// understand the score. -/// - The explanation of the sub expression is recursively explained in the -/// children structure. -/// -#[derive(RustcEncodable)] -pub struct Explanation { - val: f32, - description: String, - formula: String, - children: Vec<(String, Explanation)>, -} - - -impl Explanation { - - /// Create an empty explanation for a given value. - pub fn with_val(val: f32) -> Explanation { - Explanation { - val: val, - description: String::new(), - formula: String::new(), - children: Vec::new(), - } - } - - /// Accessor for the value - pub fn val(&self,) -> f32 { - self.val - } - - /// Accessor for the description - pub fn description(&mut self, description: &str) { - self.description.clear(); - self.description.push_str(description); - } - - /// Sets the formula - pub fn set_formula(&mut self, formula: &str) { - self.formula.clear(); - self.formula.push_str(formula); - } - - /// Add an explanation for a component of our formula - /// - name is the name of the component. It should - /// appear as `` in the formula. - /// - val is the value of the component. - pub fn add_child(&mut self, name: &str, val: f32) -> &mut Explanation { - let explanation = Explanation::with_val(val); - let name = String::from(name); - self.children.push((name, explanation)); - let &mut (_, ref mut child_experience) = self.children.last_mut().unwrap(); - child_experience - } - - - /// Creates a `String` from the explanation. - /// The subcomponent tree is represented by increasing - /// the indentation with the tree-depth. - pub fn format_with_indent(&self, f: &mut fmt::Formatter, indent: usize) -> fmt::Result { - let padding: String = iter::repeat(' ').take(indent).collect(); - try!(write!(f, "{}{}: {}\n", padding, self.val, self.description)); - if !self.formula.is_empty() { - try!(write!(f, "{}: {}\n", padding, self.formula)); - } - for &(ref child_name, ref child) in &self.children { - try!(write!(f, "- {}:\n", child_name)); - try!(child.format_with_indent(f, indent + 2)); - } - Ok(()) - } -} - -impl fmt::Debug for Explanation { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.format_with_indent(f, 0) - } -} \ No newline at end of file diff --git a/src/query/mod.rs b/src/query/mod.rs index c494b8c68..9480145dc 100644 --- a/src/query/mod.rs +++ b/src/query/mod.rs @@ -9,7 +9,6 @@ mod multi_term_query; mod phrase_query; mod scorer; mod query_parser; -mod explanation; mod occur; mod weight; mod occur_filter; @@ -27,6 +26,5 @@ pub use self::multi_term_query::MultiTermWeight; pub use self::scorer::Scorer; pub use self::scorer::EmptyScorer; pub use self::query_parser::QueryParser; -pub use self::explanation::Explanation; pub use self::query_parser::ParsingError; pub use self::weight::Weight; diff --git a/src/schema/mod.rs b/src/schema/mod.rs index 5be6bc512..da6863824 100644 --- a/src/schema/mod.rs +++ b/src/schema/mod.rs @@ -137,13 +137,30 @@ use regex::Regex; /// Validator for a potential `field_name`. /// Returns true iff the name can be use for a field name. /// -/// Anything containing more than one (alphanumeric or `_`) character -/// is a valid field name. +/// A field name must start by a letter `[a-zA-Z]`. +/// The other characters can be any alphanumic character `[a-ZA-Z0-9]` or `_`. pub fn is_valid_field_name(field_name: &str) -> bool { lazy_static! { - static ref FIELD_NAME_PTN: Regex = Regex::new("[_a-zA-Z0-9]+").unwrap(); + static ref FIELD_NAME_PTN: Regex = Regex::new("^[a-zA-Z][_a-zA-Z0-9]*$").unwrap(); } FIELD_NAME_PTN.is_match(field_name) } + +#[cfg(test)] +mod tests { + + use super::is_valid_field_name; + + #[test] + fn test_is_valid_name() { + assert!(is_valid_field_name("text")); + assert!(is_valid_field_name("text0")); + assert!(!is_valid_field_name("0text")); + assert!(!is_valid_field_name("")); + assert!(!is_valid_field_name("シャボン玉")); + assert!(is_valid_field_name("my_text_field")); + } + +} \ No newline at end of file