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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use std::path::Path;
use serde::{Serialize, Deserialize};
use crate::error::GdlError;
use crate::utils;
use std::ffi::OsStr;

#[derive(Debug, Serialize, Deserialize)]
pub struct Dialogue {
    nodes: Vec<Node>,
}

impl Dialogue {
    pub fn new() -> Self {
        Self {
            nodes: vec![],
        }
    }

    pub fn add_new_node(&mut self, node: Node) -> Result<(), GdlError> {
        self.nodes.push(node);
        Ok(())
    }

    pub fn new_dot_image(path: &Path, format: Format) -> Result<(), GdlError> {
        let dialogue = Dialogue::read_from_file(path)?;
        let dot_script = dialogue.dot_script("Test")?;

        if cfg!(debug_assertions) {
            std::fs::write(&std::env::current_dir()?.join("out.gv"), dot_script.as_bytes())?;
        }

        dialogue.render(format, &dot_script)?;

        Ok(())
    }

    pub fn save_to_file(&self, path: &Path) -> Result<(), GdlError> {
        let rif_config = serde_json::to_string_pretty(self)?;
        std::fs::write(path, rif_config)?;

        Ok(())
    }

    pub fn read_from_file(path : &Path) -> Result<Self, GdlError> {
        Ok(serde_json::from_str(&std::fs::read_to_string(path)?)?)
    }

    pub fn dot_script(&self, name: &str) -> Result<String, GdlError> {
        let mut dot_script = format!("digraph {0} {{
", name);
        let global_attr = r#"    node [shape="record"]
"#;
        dot_script.push_str(global_attr);

        for node in &self.nodes {
            self.check_node_validation(node)?;
            // id|type|
            let mut label = format!("{{{0}|{1}}}|",node.node_type, node.id);
            let mut edges = String::new();
            let mut style: &str = "";
			match node.node_type {
				NodeType::Text => {
                    // id|type|speaker|
                    label.push_str(&format!("{0}|", node.speaker.as_ref().unwrap()));
                    if let Some(goto) = &node.goto {
                        edges = format!(r#"    {0} -> {1}
"#,node.id,goto);
                    }
                    style = "";
                }
				NodeType::Selection => {
                    if let Some(speaker) = &node.speaker {
                        // {id|type}|speaker|
                        label.push_str(&format!("{0}|", speaker));
                    }
                    style = r#"colorfill="white" color="green3";"#;
                    for sel in node.selections.as_ref().unwrap() {
                        edges.push_str(&format!(r#"    {0} -> {1}
"#, node.id, sel.goto));
                    }
                }
				NodeType::Branch => {
                    style = r#"colorfill="white" color="dodgerblue3";"#;
                    for div in node.branches.as_ref().unwrap() {
                        edges.push_str(&format!(r#"    {0} -> {1}
"#, node.id, div.goto));
                    }
                }
                NodeType::Start => {
                    label = format!("{0}|",node.text);
                        edges.push_str(&format!(r#"    start -> {0}
"#, node.goto.as_ref().unwrap()));
                }
                NodeType::End => {
                    label = format!("{0}|",node.text);
                }
                NodeType::Omit => {
                    label = format!("{{Omit|{0}}}|",node.id);
                    style = r#"colorfill="white" color="gray";"#;
                    edges.push_str(&format!(r#"    {0} -> {1}
"#, node.id,node.goto.as_ref().unwrap()));
                }
			}

            // r#"{node.id} [id="{node.id}" label="{{label.slice(0,-1)}}" {style}]\n"#, node.id, label ,style
			dot_script.push_str(&format!(r#"    {0} [id="{0}" label="{{{1}}}" {2}]
"#, node.id, &label[0..label.len() - 1],style));
			dot_script.push_str(&edges);

        }
        dot_script.push_str("}");

        Ok(dot_script)
    }

    fn check_node_validation(&self, node: &Node) -> Result<(), GdlError> {
        match node.node_type {
            NodeType::Text => {
                if let None = node.speaker { return Err(GdlError::InvalidNodeContent("Text node requires speaker")); }
            }
            NodeType::Selection => {
                if let None = node.selections { return Err(GdlError::InvalidNodeContent("Selection node requires selections array")); }
            }
            NodeType::Branch => {
                if let None = node.branches { return Err(GdlError::InvalidNodeContent("Branch node requires branches array")); }
            }
            NodeType::Start => {
                if let None = node.goto { return Err(GdlError::InvalidNodeContent("Start node requires goto id")); }
            }
            NodeType::Omit => {
                if let None = node.goto { return Err(GdlError::InvalidNodeContent("Omit node requires goto id")); }
            }
            NodeType::End => ()
        }
        Ok(())
    }

    fn render(&self,format : Format, dot_script: &str) -> Result<(), GdlError> {
        let args: Vec<&OsStr>;
        let out_file;
        match format {
            Format::Pdf => {
                out_file = std::env::current_dir()?.join("out.pdf");
                args = vec![
                    OsStr::new("-Tpdf"),
                    OsStr::new("-o"),
                    out_file.as_os_str()
                ];
            }
            Format::Png => {
                out_file = std::env::current_dir()?.join("out.png");
                args = vec![
                    OsStr::new("-Tpng"),
                    OsStr::new("-Gdpi=300"),
                    OsStr::new("-o"),
                    out_file.as_os_str()
                ];
            }
        }
        utils::dot_exec(args, dot_script)?;
        Ok(())
    }
}

pub enum Format {
    Pdf,
    Png,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Node {
    // Global attribute
    id: String,
    node_type: NodeType,
    goto: Option<String>,
    text: String,

    // Comes with Text and selection Node
    speaker: Option<String>,
    // Comes with selection node
    selections: Option<Vec<Selection>>,
    // Comes with branch node
    branches: Option<Vec<Branch>>,
}

impl Node {
    pub fn omit_node(id: &str, goto: &str) -> Self {
        Self {
            id: id.to_owned(),
            node_type: NodeType::Omit,
            text: "".to_owned(),
            speaker : None,
            goto : Some(goto.to_owned()),
            selections : None,
            branches: None,
        }
    }
    pub fn start_node(text: &str, goto: &str) -> Self {
        Self {
            id: String::from("start"),
            node_type: NodeType::Start,
            text: text.to_owned(),
            speaker : None,
            goto : Some(goto.to_owned()),
            selections : None,
            branches: None,
        }
    }

    pub fn end_node(text: &str) -> Self {
        Self {
            id: String::from("end"),
            node_type: NodeType::End,
            text: text.to_owned(),
            speaker : None,
            goto : None,
            selections : None,
            branches: None,
        }
    }

    pub fn text_node(id: &str, speaker: &str, text: &str, goto: Option<&str>) -> Self {
        let id = id.to_owned();
        let speaker = speaker.to_owned();
        let text = text.to_owned();
        let goto = goto.map(|s| s.to_owned());
        Self {
            id,
            node_type: NodeType::Text,
            text,
            speaker : Some(speaker),
            goto,
            selections : None,
            branches: None,
        }
    }
    pub fn selection_node(id: &str, speaker: Option<&str>, text: &str, goto: Option<&str>, selections: Vec<Selection>) -> Self {
        let id = id.to_owned();
        let speaker = speaker.map(|s| s.to_owned());
        let text = text.to_owned();
        let goto = goto.map(|s| s.to_owned());
        Self {
            id,
            node_type: NodeType::Selection,
            text,
            speaker,
            goto,
            selections : Some(selections),
            branches: None,
        }
    }

    pub fn branch_node(id: &str, goto: Option<&str>, branches: Vec<Branch>) -> Self {
        let id = id.to_owned();
        let goto = goto.map(|s| s.to_owned());
        Self {
            id,
            text: String::new(),
            node_type: NodeType::Branch,
            speaker : None,
            goto,
            selections : None,
            branches: Some(branches),
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
enum NodeType {
    Start,
    Text,
    Branch,
    Selection,
    Omit,
    End,
}

impl std::fmt::Display for NodeType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let text: &str;

        match self {
            Self::Text => text = "Text",
            Self::Branch => text = "Branch",
            Self::Selection => text = "Selection",
            Self::Start => text = "Start",
            Self::End => text = "End",
            Self::Omit => text = "Omit",
        }
        write!(f,"{0}",text)
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Selection {
    text: String,
    goto: String,
}
impl Selection {
    pub fn new( text: &str, goto: &str) -> Self {
        Self { 
            text: text.to_owned(),
            goto: goto.to_owned(),
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Branch {
    target: String,
    qual: String,
    goto: String,
} 

impl Branch {
    pub fn new(target: &str, qual: &str, goto: &str) -> Self {
        Self {
            target: target.to_owned(),
            qual: qual.to_owned(),
            goto: goto.to_owned(),
        }
    }
}