pub struct Problem {
pub name: String,
pub vars: Vars,
pub choices: Option<Vec<String>>,
pub solutions: Option<Solutions>,
}
Fields§
§name: String
§vars: Vars
§choices: Option<Vec<String>>
§solutions: Option<Solutions>
Implementations§
Source§impl Problem
impl Problem
Sourcepub fn try_filter(&self, filter: &Filter) -> bool
pub fn try_filter(&self, filter: &Filter) -> bool
Checks if a problem passes or fails a certain filter
For Gt, it checks if the value of the problem is greater than the value passed in the filter (not the other way around). The same is true of Lt, Ge, Le.
§Usage
use mapm::problem::Problem;
use mapm::problem::Filter;
use std::collections::HashMap;
let mut vars: HashMap<String, String> = HashMap::new();
vars.insert(String::from("problem"), String::from("What is $1+1$?"));
vars.insert(String::from("author"), String::from("Dennis Chen"));
vars.insert(String::from("difficulty"), String::from("5"));
let mut solution_one: HashMap<String, String> = HashMap::new();
solution_one.insert(String::from("text"), String::from("It's probably $2$."));
solution_one.insert(String::from("author"), String::from("Dennis Chen"));
let mut solution_two = HashMap::new();
solution_two.insert(String::from("text"), String::from("The answer is $2$, but my proof is too small to fit into the margin."));
solution_two.insert(String::from("author"), String::from("Pierre de Fermat"));
let mut solutions: Option<Vec<HashMap<String, String>>> = Some(vec![solution_one, solution_two]);
let problem = Problem { name: String::from("problem"), vars, choices: None, solutions };
assert_eq!(problem.try_filter(&Filter::Exists{key: String::from("subject")}), false);
assert_eq!(problem.try_filter(&Filter::Exists{key: String::from("solutions")}), true);
assert_eq!(problem.try_filter(&Filter::Gt{key: String::from("difficulty"), val: 5}), false);
assert_eq!(problem.try_filter(&Filter::Ge{key: String::from("difficulty"), val: 5}), true);
assert_eq!(problem.try_filter(&Filter::Lt{key: String::from("difficulty"), val: 5}), false);
assert_eq!(problem.try_filter(&Filter::Le{key: String::from("difficulty"), val: 5}), true);
Sourcepub fn filter(&self, filters: &[FilterAction]) -> Option<Self>
pub fn filter(&self, filters: &[FilterAction]) -> Option<Self>
Checks whether a problem satisfies a set of filters, and if it does, return it; otherwise return None
If filters
is empty, then every problem will pass the filter.
Sourcepub fn filter_keys(&mut self, views: &Views) -> Self
pub fn filter_keys(&mut self, views: &Views) -> Self
Show only the keys passed in or everything but the keys passed in to a problem
§Usage
use mapm::problem::Views;
use mapm::problem::Views::{Show, Hide};
use mapm::problem::Problem;
use mapm::problem::Filter;
use std::collections::HashMap;
let problem = Problem {
name: String::from("problem"),
vars: HashMap::from([
(String::from("problem"), String::from("What is $1+1$?")),
(String::from("author"), String::from("Dennis Chen")),
(String::from("answer"), String::from("2"))
]),
choices: None,
solutions: Some(vec![HashMap::from([
(String::from("text"), String::from("It's $2$.")),
(String::from("author"), String::from("Alexander")),
])])
};
let show: Views = Show(vec![String::from("author"), String::from("answer"), String::from("solutions")]);
let show_filtered_problem = problem.clone().filter_keys(&show);
assert_eq!(show_filtered_problem.vars, HashMap::from([
(String::from("author"), String::from("Dennis Chen")),
(String::from("answer"), String::from("2"))
]));
assert_eq!(show_filtered_problem.solutions, Some(vec![
HashMap::from([
(String::from("text"), String::from("It's $2$.")),
(String::from("author"), String::from("Alexander"))
])
]));
let hide: Views = Hide(vec![String::from("solutions"), String::from("author")]);
let hide_filtered_problem = problem.clone().filter_keys(&hide);
assert_eq!(hide_filtered_problem.vars, HashMap::from([
(String::from("problem"), String::from("What is $1+1$?")),
(String::from("answer"), String::from("2")),
]));
assert_eq!(hide_filtered_problem.solutions, None);
Sourcepub fn check_template(&self, template: &Template) -> Result<(), Vec<MapmErr>>
pub fn check_template(&self, template: &Template) -> Result<(), Vec<MapmErr>>
Checks if a problem contains all the variables in a template
Returns None if the problem successfully passes the test, returns Some(MapmErr) otherwise
§Usage
§Expected success
use mapm::problem::Problem;
use mapm::template::Template;
use std::collections::HashMap;
let problem = Problem {
name: String::from("problem"),
vars: HashMap::from([
(String::from("problem"), String::from("What is $1+1?$"))
]),
choices: None,
solutions: Some(vec![HashMap::from([
(String::from("text"), String::from("Some say the answer is $2$.")),
(String::from("author"), String::from("Dennis Chen")),
])])
};
let template = Template {
name: String::from("template"),
engine: String::from("pdflatex"),
problem_count: Some(1),
texfiles: HashMap::from([
(String::from("problems.tex"), String::from("problems.pdf"))
]),
choices: None,
vars: vec![String::from("title"), String::from("year")],
problemvars: vec![String::from("problem")],
solutionvars: vec![String::from("text"), String::from("author")],
};
assert!(problem.check_template(&template).is_ok());
§Expected failure
use mapm::problem::Problem;
use mapm::template::Template;
use mapm::result::MapmErr;
use mapm::result::MapmErr::*;
use std::collections::HashMap;
let problem = Problem {
name: String::from("problem"),
vars: HashMap::from([(String::from("problem"), String::from("What is $1+1?$"))]),
choices: None,
solutions: Some(vec![HashMap::from([
(String::from("text"), String::from("Some say the answer is $2$.")),
(String::from("author"), String::from("Dennis Chen")),
])])
};
let template = Template {
name: String::from("template"),
engine: String::from("pdflatex"),
problem_count: Some(1),
texfiles: HashMap::from([(
String::from("problems.tex"), String::from("problems.pdf")
)]),
choices: None,
vars: vec!(String::from("title"), String::from("year")) ,
problemvars: vec!(String::from("problem"), String::from("author")),
solutionvars: vec!(String::from("text"), String::from("author")),
};
let template_check = problem.check_template(&template).unwrap_err();
assert_eq!(template_check.len(), 1);
match &template_check[0] {
ProblemErr(err) => {
assert_eq!(err, "Does not contain key `author`");
}
_ => {
panic!("MapmErr type is not ProblemErr");
}
}
Trait Implementations§
impl StructuralPartialEq for Problem
Auto Trait Implementations§
impl Freeze for Problem
impl RefUnwindSafe for Problem
impl Send for Problem
impl Sync for Problem
impl Unpin for Problem
impl UnwindSafe for Problem
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more