Struct Problem

Source
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

Source

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);
Source

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.

Source

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);
Source

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§

Source§

impl Clone for Problem

Source§

fn clone(&self) -> Problem

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Problem

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Problem

Source§

fn eq(&self, other: &Problem) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Problem

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.