Skip to content

Commit

Permalink
first version of presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Feb 20, 2018
1 parent ffb858e commit 87e1516
Show file tree
Hide file tree
Showing 13 changed files with 362 additions and 40 deletions.
69 changes: 69 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ structopt = "0.1.7"
structopt-derive = "0.1.6"
git2 = "0.6.11"
rand = "0.4"
regex = "0.2"
ncurses = "5.0"
string-interner = "0.6.3"
serde = "1.0.27"
Expand Down
4 changes: 2 additions & 2 deletions src/cards/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Card {
self.lines_with_kind(kind)
}

fn lines_with_kind(&self, kind: LineKind) -> impl Iterator<Item = &str> + '_ {
crate fn lines_with_kind(&self, kind: LineKind) -> impl Iterator<Item = &str> + '_ {
self.lines
.iter()
.filter(move |line| line.kind == kind)
Expand Down Expand Up @@ -147,7 +147,7 @@ impl fmt::Display for LineKind {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
LineKind::Comment => write!(fmt, "#"),
LineKind::Meaning(lang) => write!(fmt, "{}", lang),
LineKind::Meaning(lang) => write!(fmt, "{}", lang.abbreviation()),
}
}
}
Expand Down
51 changes: 50 additions & 1 deletion src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(crate) struct QuestionRecord {
pub(crate) result: QuestionResult,
}

#[derive(Copy, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) enum QuestionKind {
Translate { from: Language, to: Language },
}
Expand Down Expand Up @@ -77,6 +77,10 @@ impl Database {
crate fn card_record(&self, uuid: Uuid) -> Option<&CardRecord> {
self.user.records.get(&uuid)
}

crate fn card_record_mut(&mut self, uuid: Uuid) -> &mut CardRecord {
self.user.records.entry(uuid).or_insert(CardRecord::default())
}
}

impl CardRecord {
Expand Down Expand Up @@ -129,3 +133,48 @@ impl CardRecord {
(1..len).map(move |i| (&questions[i - 1], &questions[i])).rev()
}
}

impl QuestionKind {
/// When asking a question of this kind, what kinds of lines in the card
/// provide the "prompt" we should give the user?
crate fn prompt_line_kind(self) -> LineKind {
match self {
QuestionKind::Translate { from, to: _ } => LineKind::Meaning(from),
}
}

/// When asking a question of this kind, what kinds of lines in
/// the card provide the "response" we should expect from the
/// user?
crate fn response_line_kind(self) -> LineKind {
match self {
QuestionKind::Translate { from: _, to } => LineKind::Meaning(to),
}
}

/// When asking a question of this kind, what language should we
/// expect the user's response to be in?
crate fn response_language(self) -> Language {
match self {
QuestionKind::Translate { from: _, to } => to,
}
}

crate fn prompt_text(self) -> impl fmt::Display {
PromptText(self)
}
}

struct PromptText(QuestionKind);

impl fmt::Display for PromptText {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self.0 {
QuestionKind::Translate { from, to } => {
write!(fmt, "translate from {} to {}", from.full_name(), to.full_name())?;
}
}

Ok(())
}
}
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub(crate) enum MathemaErrorKind {

#[fail(display = "don't know how to quiz you in `{}`", language)]
DontKnowHowToQuiz {
language: Language,
language: &'static str,
},

#[fail(display = "the cards file `{}` does not appear to be in the repository directory",
Expand Down
4 changes: 4 additions & 0 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ impl MathemaRepository {
self.cards.keys().cloned()
}

crate fn card(&self, uuid: Uuid) -> &Card {
&self.cards[&uuid]
}

/// Makes a "database-relative" path into an absolute path.
fn absolute_path(&self, relative_path: impl AsRef<Path>) -> PathBuf {
self.directory_path.join(relative_path)
Expand Down
15 changes: 11 additions & 4 deletions src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ pub(crate) enum Language {
Greek,
}

impl fmt::Display for Language {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
impl Language {
crate fn abbreviation(self) -> &'static str {
match self {
Language::English => "en",
Language::Greek => "gr",
}
}

crate fn full_name(self) -> &'static str {
match self {
Language::English => write!(fmt, "en"),
Language::Greek => write!(fmt, "gr"),
Language::English => "English",
Language::Greek => "Ελληνικά",
}
}
}
Expand Down
25 changes: 13 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@
#![deny(unused_must_use)] // always a bug
#![feature(crate_in_paths, conservative_impl_trait, crate_visibility_modifier, decl_macro,
dyn_trait, /*FIXME(rust-lang/rust#47075) extern_absolute_paths,*/ extern_in_paths,
in_band_lifetimes, match_default_bindings, nll,
inclusive_range_syntax, in_band_lifetimes, match_default_bindings, nll,
termination_trait, underscore_lifetimes, universal_impl_trait)]

// FIXME can't use this because of format!
//#![deny(elided_lifetime_in_path)]

use crate::prelude::*;
use extern::{
structopt_derive::StructOpt,
structopt::{self, StructOpt},
};
use extern::{structopt::{self, StructOpt}, structopt_derive::StructOpt};

macro throw($t:expr) {
macro throw($t: expr) {
return Err($t.into());
}

Expand Down Expand Up @@ -52,9 +49,14 @@ struct MathemaOptions {

#[derive(StructOpt, Clone, Debug)]
enum MathemaCommand {
#[structopt(name = "quiz", about = "test yourself")] Quiz {
#[structopt(name = "quiz", about = "test yourself")]
Quiz {
#[structopt(help = "what language do you want to learn")]
language: String
language: String,

#[structopt(short = "d", long = "duration", help = "maximum duration in minutes",
default_value = "10")]
duration: i64,
},

#[structopt(name = "dump", about = "dump info about cards")] Dump,
Expand All @@ -65,8 +67,7 @@ enum MathemaCommand {
directory: String,
},

#[structopt(name = "status", about = "check on the status of your cards")]
Status,
#[structopt(name = "status", about = "check on the status of your cards")] Status,

#[structopt(name = "add", about = "add new cards from file")]
Add {
Expand All @@ -89,8 +90,8 @@ fn main1() -> Result<(), Error> {
let args = &MathemaOptions::from_args();

match &args.command {
MathemaCommand::Quiz { language } => {
quiz::quiz(args, language)?;
MathemaCommand::Quiz { language, duration } => {
quiz::quiz(args, language, *duration)?;
}

MathemaCommand::New { directory } => {
Expand Down
3 changes: 2 additions & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ crate use crate::{
errors::{Fallible, MathemaError, MathemaErrorKind},
git::MathemaRepository,
language::Language,
quiz::presentation::{Presentation, Prompt},
selection,
status::Status,
line_parser::LineParser,
Expand All @@ -23,7 +24,7 @@ crate use extern::{
git2,
itertools::Itertools,
rand::{self, Rng},
ncurses,
regex::Regex,
serde_derive::{Serialize, Deserialize},
std::collections::{BTreeSet, BTreeMap, HashMap},
std::env,
Expand Down
Loading

0 comments on commit 87e1516

Please sign in to comment.