shared/domain/module/body/
memory.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use crate::domain::module::{
    body::{Body, BodyConvert, BodyExt, ThemeId, _groups::cards::*},
    ModuleKind,
};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

/// The body for [`Memory`](crate::domain::module::ModuleKind::Memory) modules.
#[derive(Default, Clone, Serialize, Deserialize, Debug)]
pub struct ModuleData {
    /// The content
    pub content: Option<Content>,
}

/// The content for [`Memory`](crate::domain::module::ModuleKind::Memory) modules.
#[derive(Default, Clone, Serialize, Deserialize, Debug)]
pub struct Content {
    /// The base content for all cards modules
    pub base: BaseContent,
    /// Settings for playback
    pub player_settings: PlayerSettings,
}

/// Player settings
#[derive(Default, Clone, Serialize, Deserialize, Debug)]
pub struct PlayerSettings {
    /// time limit in minutes
    pub time_limit: Option<u32>,
    /// amount of pairs to render
    pub pairs_to_display: Option<u32>,
}

impl BodyExt<Mode, Step> for ModuleData {
    fn as_body(&self) -> Body {
        Body::MemoryGame(self.clone())
    }

    fn is_complete(&self) -> bool {
        self.content
            .as_ref()
            .map_or(false, |content| content.base.is_valid())
    }

    fn kind() -> ModuleKind {
        ModuleKind::Memory
    }

    fn new_with_mode_and_theme(mode: Mode, theme: ThemeId) -> Self {
        ModuleData {
            content: Some(Content {
                base: BaseContent {
                    mode,
                    theme,
                    ..Default::default()
                },
                ..Default::default()
            }),
        }
    }

    fn mode(&self) -> Option<Mode> {
        self.content.as_ref().map(|c| c.base.mode.clone())
    }

    fn requires_choose_mode(&self) -> bool {
        self.content.is_none()
    }

    fn set_editor_state_step(&mut self, step: Step) {
        if let Some(content) = self.content.as_mut() {
            content.base.editor_state.step = step;
        }
    }
    fn set_editor_state_steps_completed(&mut self, steps_completed: HashSet<Step>) {
        if let Some(content) = self.content.as_mut() {
            content.base.editor_state.steps_completed = steps_completed;
        }
    }

    fn get_editor_state_step(&self) -> Option<Step> {
        self.content
            .as_ref()
            .map(|content| content.base.editor_state.step)
    }

    fn get_editor_state_steps_completed(&self) -> Option<HashSet<Step>> {
        self.content
            .as_ref()
            .map(|content| content.base.editor_state.steps_completed.clone())
    }

    fn set_theme(&mut self, theme_id: ThemeId) {
        if let Some(content) = self.content.as_mut() {
            content.base.theme = theme_id;
        }
    }

    fn get_theme(&self) -> Option<ThemeId> {
        self.content.as_ref().map(|content| content.base.theme)
    }
}

impl BodyConvert for ModuleData {
    fn convertable_list() -> Vec<ModuleKind> {
        vec![
            ModuleKind::Matching,
            ModuleKind::Flashcards,
            ModuleKind::CardQuiz,
        ]
    }
    fn convert_to_matching(&self) -> Result<super::matching::ModuleData, &'static str> {
        Ok(super::matching::ModuleData {
            content: self
                .content
                .as_ref()
                .map(|content| super::matching::Content {
                    base: content.base.clone(),
                    player_settings: super::matching::PlayerSettings::default(),
                }),
        })
    }
    fn convert_to_flashcards(&self) -> Result<super::flashcards::ModuleData, &'static str> {
        Ok(super::flashcards::ModuleData {
            content: self
                .content
                .as_ref()
                .map(|content| super::flashcards::Content {
                    base: content.base.clone(),
                    player_settings: super::flashcards::PlayerSettings::default(),
                }),
        })
    }

    fn convert_to_card_quiz(&self) -> Result<super::card_quiz::ModuleData, &'static str> {
        Ok(super::card_quiz::ModuleData {
            content: self
                .content
                .as_ref()
                .map(|content| super::card_quiz::Content {
                    base: content.base.clone(),
                    player_settings: super::card_quiz::PlayerSettings::default(),
                }),
        })
    }
}

impl TryFrom<Body> for ModuleData {
    type Error = &'static str;

    fn try_from(body: Body) -> Result<Self, Self::Error> {
        match body {
            Body::MemoryGame(data) => Ok(data),
            _ => Err("cannot convert body to memory game!"),
        }
    }
}