Skip to content

Commit

Permalink
feat: fix/add various structs
Browse files Browse the repository at this point in the history
Fix `Tip` struct.
Fix `DidYouMean` type by replacing it with a struct.
Fix `Warning` enum by replacing it with a set of structs.
Add tests for all new/fixed structs.
  • Loading branch information
indiv0 committed Dec 14, 2016
1 parent 47c7d0e commit f93151b
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 33 deletions.
84 changes: 51 additions & 33 deletions src/model.in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ pub struct Tips {
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
pub struct Tip {
// Attributes
pub count: u32,

pub tip: Vec<Tip>,
pub text: String,
}

/// A series of `DidYouMean` elements.
Expand All @@ -89,7 +87,15 @@ pub struct DidYouMeans {
}

/// Provides a suggestion for a different query than the one provided.
pub type DidYouMean = String;
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
pub struct DidYouMean {
// Attributes
pub score: String, // TODO: find a way to put a floating point here.
pub level: String,

#[serde(rename="$value")]
pub value: String,
}

/// Generated when a foreign language is detected in a query.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
Expand Down Expand Up @@ -142,35 +148,47 @@ pub struct Warnings {
// Attributes
pub count: u32,

pub warning: Vec<Warnings>,
}

/// An enum representing all possible warning types.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
pub enum Warning {
Spellcheck {
// Attributes
word: String,
suggestion: String,
text: String,
},
Delimeters {
// Attributes
text: String,
},
Translation {
// Attributes
phrase: String,
trans: String,
lang: String,
text: String,
},
Reinterpret {
// Attributes
text: String,

alternative: Vec<Alternative>,
},
// TODO: find a way to merge these into an enum?
pub spellcheck: Option<Vec<Spellcheck>>,
pub delimeters: Option<Vec<Delimeters>>,
pub translation: Option<Vec<Translation>>,
pub reinterpret: Option<Vec<Reinterpret>>,
}

/// Provides word and suggestion attributes as alternative spellings for a word
/// in the query.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
pub struct Spellcheck {
// Attributes
word: String,
suggestion: String,
text: String,
}

/// Represents a warning regarding mismatched delimiters in a query.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
pub struct Delimeters {
// Attributes
text: String,
}

/// Represents a word or a phrase which was translated in the query.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
pub struct Translation {
// Attributes
phrase: String,
trans: String,
lang: String,
text: String,
}

/// Represents a warning that the query was reinterpred.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
pub struct Reinterpret {
// Attributes
text: String,

alternative: Vec<Alternative>,
}

/// An alternative interpretation of an element in a query.
Expand Down
31 changes: 31 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@ mod tests {
use serde_xml::from_str;

use super::{
DidYouMean,
DidYouMeans,
Img,
Infos,
Plaintext,
Pod,
QueryResult,
Spellcheck,
State,
Statelist,
States,
Subpod,
Tips,
Warnings,
};

fn read_sample_data_from_path<P>(path: P) -> String
Expand All @@ -59,6 +64,32 @@ mod tests {
//from_str::<QueryResult>(&read_sample_data_from_path("tests/sample-data/query_result_3.xml")).unwrap();
from_str::<QueryResult>(&read_sample_data_from_path("tests/sample-data/query_result_4.xml")).unwrap();
from_str::<QueryResult>(&read_sample_data_from_path("tests/sample-data/query_result_5.xml")).unwrap();
from_str::<QueryResult>(&read_sample_data_from_path("tests/sample-data/query_result_6.xml")).unwrap();
}

#[test]
fn test_didyoumean_deserializer() {
from_str::<DidYouMean>(&read_sample_data_from_path("tests/sample-data/didyoumean.xml")).unwrap();
}

#[test]
fn test_didyoumeans_deserializer() {
from_str::<DidYouMeans>(&read_sample_data_from_path("tests/sample-data/didyoumeans.xml")).unwrap();
}

#[test]
fn test_warning_deserializer() {
from_str::<Spellcheck>(&read_sample_data_from_path("tests/sample-data/spellcheck.xml")).unwrap();
}

#[test]
fn test_warnings_deserializer() {
from_str::<Warnings>(&read_sample_data_from_path("tests/sample-data/warnings.xml")).unwrap();
}

#[test]
fn test_tips_deserializer() {
from_str::<Tips>(&read_sample_data_from_path("tests/sample-data/tips.xml")).unwrap();
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions tests/sample-data/didyoumean.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<didyoumean score='0.796774' level='high'>batman graphing calculator</didyoumean>
3 changes: 3 additions & 0 deletions tests/sample-data/didyoumeans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<didyoumeans count='1'>
<didyoumean score='0.796774' level='high'>batman graphing calculator</didyoumean>
</didyoumeans>
20 changes: 20 additions & 0 deletions tests/sample-data/query_result_6.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version='1.0' encoding='UTF-8'?>
<queryresult success='false'
error='false'
numpods='0'
datatypes=''
timedout=''
timedoutpods=''
timing='1.822'
parsetiming='0.609'
parsetimedout='false'
recalculate=''
id=''
host='http://www4b.wolframalpha.com'
server='28'
related=''
version='2.6'>
<didyoumeans count='1'>
<didyoumean score='0.796774' level='high'>batman graphing calculator</didyoumean>
</didyoumeans>
</queryresult>
3 changes: 3 additions & 0 deletions tests/sample-data/spellcheck.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<spellcheck word='airort'
suggestion='airport'
text='Interpreting &quot;airort&quot; as &quot;airport&quot;' />
4 changes: 4 additions & 0 deletions tests/sample-data/tips.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<tips count='2'>
<tip text='Avoid concatenation in math expressions' />
<tip text='Use r*x rather than rx, and q*x^2 rather than qx2' />
</tips>
8 changes: 8 additions & 0 deletions tests/sample-data/warnings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<warnings count='2'>
<spellcheck word='airort'
suggestion='airport'
text='Interpreting &quot;airort&quot; as &quot;airport&quot;' />
<spellcheck word='JFK airort'
suggestion='airports'
text='Interpreting &quot;JFK airort&quot; as &quot;airports&quot;' />
</warnings>

0 comments on commit f93151b

Please sign in to comment.