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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
use crate::ErrorList;
use crate::Output;
use crate::Passage;
use crate::PassageContent;
use crate::StoryData;
use crate::StoryPassages;
use std::collections::HashMap;
use std::path::Path;

/// A parsed Twee story
///
/// This is the primary interface for tweep. The provided utility functions
/// allow a Twee 3 story to be parsed from a `String`, a directory or file
/// `Path`, or a slice of string slices, representing the lines of input. The
/// output is an `Output<Result<Story, ErrorList>>` which is either the parsed
/// `Story` or an [`ErrorList`] if the parse failed, along with a list of any
/// [`Warning`]s generated during parsing. The fields in this struct provide
/// access to all necessary components of the parsed story.
///
/// # Parse Errors
/// * [`BadInputPath`] - The given `Path` cannot be used to parse a story
/// See [`Passage`] for other errors that can occur during parsing
///
/// # Parse Warnings
/// * [`DuplicateStoryTitle`] - More than one `StoryTitle` passage found
/// * [`DuplicateStoryData`] - More than one `StoryData` passage found
/// * [`MissingStoryTitle`] - No `StoryTitle` passage found
/// * [`MissingStoryData`] - No `StoryData` passage found
/// * [`DeadLink`] - Found a link to a non-existent passage
/// * [`MissingStartPassage`] - No `Start` passage found and no alternate
///   passage set in `StoryData`
/// * [`DeadStartPassage`] - Alternate start passage set in `StoryData`, but
///   no such passage found in parsing
/// See [`Passage`] for other warnings that can occur during parsing
///
///
/// # Examples
/// ```
/// use tweep::Story;
/// let input = r#":: StoryTitle
///RustDoc Sample Story
///
///:: StoryData
///{
///  "ifid": "D674C58C-DEFA-4F70-B7A2-27742230C0FC",
///  "format": "SugarCube",
///  "format-version": "2.28.2",
///  "start": "My Starting Passage",
///  "tag-colors": {
///    "tag1": "green",
///    "tag2": "red",
///    "tag3": "blue"
///  },
///  "zoom": 0.25
///}
///
///:: My Starting Passage [ tag1 tag2 ]
///This is the starting passage, specified by the start attribute of StoryData.
///Alternately, we could remove that attribute and rename the passage to Start.
///
///It has tags and links to:
///  [[Another passage]]
///  [[Here too!|Another passage]]
///  [[A third passage<-And a different passage]]
///
///:: Another passage {"position":"600,400","size":"100,200"}
///This passage has some metadata attached to it
///
///:: A third passage [tag3] { "position": "400,600" }
///This passage has both tags and metadata. The size attribute of the metadata
///isn't overridden, so it will be set to the default value.
///"#.to_string();
///
///// Parse the input into an Output<Result<Story, ErrorList>>
///let out = Story::from_string(input);
///assert!(!out.has_warnings());
///
///// Move the Result out of the Output
///let (res, _) = out.take();
///assert!(res.is_ok());
///
///// Get the Story object
///let story = res.ok().unwrap();
///
///// StoryTitle and StoryData contents are parsed into special fields
///assert_eq!(story.title.unwrap(), "RustDoc Sample Story");
///assert_eq!(story.data.unwrap().ifid, "D674C58C-DEFA-4F70-B7A2-27742230C0FC");
///
///// Other passages are parsed into a map, keyed by the passage name
///assert_eq!(story.passages["My Starting Passage"].tags(), &vec!["tag1", "tag2"]);
///let metadata = story.passages["A third passage"].metadata();
///assert_eq!(metadata["size"], "100,100");
///assert_eq!(metadata["position"], "400,600");
/// ```
///
/// [`DuplicateStoryTitle`]: enum.WarningType.html#variant.DuplicateStoryTitle
/// [`DuplicateStoryData`]: enum.WarningType.html#variant.DuplicateStoryData
/// [`MissingStoryTitle`]: enum.WarningType.html#variant.MissingStoryTitle
/// [`MissingStoryData`]: enum.WarningType.html#variant.MissingStoryData
/// [`DeadLink`]: enum.WarningType.html#variant.DeadLink
/// [`MissingStartPassage`]: enum.WarningType.html#variant.MissingStartPassage
/// [`DeadStartPassage`]: enum.WarningType.html#variant.DeadStartPassage
/// [`BadInputPath`]: enum.ErrorType.html#variant.BadInputPath
/// [`Passage`]: struct.Passage.html
#[derive(Default)]
pub struct Story {
    /// The story title
    pub title: Option<String>,

    /// The story data as defined by the specification
    pub data: Option<StoryData>,

    /// Map from passage name to `Passage` for any non-special passages
    pub passages: HashMap<String, Passage>,

    /// A list of the contents of any passages tagged with `script`
    pub scripts: Vec<String>,

    /// A list of the contents of any passages tagged with `stylesheet`
    pub stylesheets: Vec<String>,
}

impl Story {
    /// Parses an input `String` and returns the result or a list of errors,
    /// along with a list of any [`Warning`]s
    ///
    /// [`Warning`]: struct.Warning.html
    pub fn from_string(input: String) -> Output<Result<Self, ErrorList>> {
        StoryPassages::from_string(input).into_result()
    }

    /// Parses an input `&[&str]` and returns the result or a list of errors,
    /// along with a list of any [`Warning`]s
    ///
    /// [`Warning`]: struct.Warning.html
    pub fn from_slice(input: &[&str]) -> Output<Result<Self, ErrorList>> {
        StoryPassages::from_slice(input).into_result()
    }

    /// Parses a `Story` from the given [`Path`]. If the given path is a file,
    /// parses that file and returns the `Story`. If it is a directory, it looks
    /// for any files with `.tw` or `.twee` extensions and parses them. Returns
    /// the parsed output or a list of errors, along with a list of any
    /// [`Warning`]s
    ///
    /// [`Path`]: std::path::Path
    /// [`Warning`]: struct.Warning.html
    pub fn from_path<P: AsRef<Path>>(input: P) -> Output<Result<Self, ErrorList>> {
        StoryPassages::from_path(input).into_result()
    }
}

impl std::convert::From<StoryPassages> for Story {
    fn from(s: StoryPassages) -> Story {
        let title = match s.title {
            Some(c) => match c.content {
                PassageContent::StoryTitle(t) => Some(t.title),
                _ => panic!("Expected title to be StoryTitle"),
            },
            None => None,
        };

        let data = match s.data {
            Some(c) => match c.content {
                PassageContent::StoryData(d, _) => d,
                _ => panic!("Expected data to be StoryData"),
            },
            None => None,
        };

        let scripts = s
            .scripts
            .into_iter()
            .map(|p| match p.content {
                PassageContent::Script(script) => script.content,
                _ => panic!("Expected script to be Script"),
            })
            .collect();

        let stylesheets = s
            .stylesheets
            .into_iter()
            .map(|p| match p.content {
                PassageContent::Stylesheet(stylesheet) => stylesheet.content,
                _ => panic!("Expected stylesheet to be Stylesheet"),
            })
            .collect();

        let passages = s.passages;

        Story {
            title,
            data,
            passages,
            scripts,
            stylesheets,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Warning;
    use crate::WarningType;
    use tempfile::tempdir;

    #[test]
    fn warning_offsets() {
        let input = r#":: A passage
This
That
The Other


:: A\[nother passage
Foo
Bar
Baz


:: StoryTitle
Test Story


"#
        .to_string();
        use crate::Positional;
        let out = Story::from_string(input);
        assert_eq!(out.has_warnings(), true);
        let (res, warnings) = out.take();
        assert_eq!(res.is_ok(), true);
        assert_eq!(
            warnings[0],
            Warning::new(WarningType::EscapedOpenSquare)
                .with_row(7)
                .with_column(5)
        );
    }

    #[test]
    fn file_input() -> Result<(), Box<dyn std::error::Error>> {
        let input = r#":: A passage
This
That
The Other


:: Another passage
Foo
Bar
Baz


:: StoryTitle
Test Story


"#
        .to_string();
        use std::fs::File;
        use std::io::Write;
        let dir = tempdir()?;
        let file_path = dir.path().join("test.twee");
        let mut file = File::create(file_path.clone())?;
        writeln!(file, "{}", input)?;

        let out = Story::from_path(file_path);
        assert_eq!(out.has_warnings(), true);
        let (res, warnings) = out.take();
        assert_eq!(res.is_ok(), true);
        let story = res.ok().unwrap();
        assert_eq!(story.title.is_some(), true);
        let title = story.title.unwrap();
        assert_eq!(title, "Test Story");
        assert_eq!(warnings[0], Warning::new(WarningType::MissingStoryData));

        Ok(())
    }

    #[test]
    fn a_test() {
        let input = r#":: A passage
This
That
The Other


:: Another passage
Foo
Bar
Baz


:: StoryTitle
Test Story


"#
        .to_string();
        let out = Story::from_string(input);
        assert_eq!(out.has_warnings(), false);
        let (res, _) = out.take();
        assert_eq!(res.is_ok(), true);
        let story = res.ok().unwrap();
        assert_eq!(story.title.is_some(), true);
        let title = story.title.unwrap();
        assert_eq!(title, "Test Story");
    }

    #[test]
    fn dir_input() -> Result<(), Box<dyn std::error::Error>> {
        use std::fs::File;
        let input_one = r#":: Start
At the start, link to [[A passage]]

:: A passage
This passage links to [[Another passage]]

:: StoryTitle
Test Story

:: Wa\{rning title one
blah blah
"#
        .to_string();

        let input_two = r#":: Another passage
Links back to [[Start]]

:: StoryData
{
"ifid": "ABC"
}

:: Warning titl\]e two
blah blah
"#
        .to_string();

        use std::io::Write;
        let dir = tempdir()?;
        let file_path_one = dir.path().join("test.twee");
        let mut file_one = File::create(file_path_one.clone())?;
        writeln!(file_one, "{}", input_one)?;
        let file_path_two = dir.path().join("test2.tw");
        let mut file_two = File::create(file_path_two.clone())?;
        writeln!(file_two, "{}", input_two)?;

        let out = Story::from_path(dir.path());
        assert_eq!(out.has_warnings(), true);
        let (res, warnings) = out.take();
        assert_eq!(warnings.len(), 2);
        assert_eq!(res.is_ok(), true);
        let story = res.ok().unwrap();
        assert_eq!(story.title, Some("Test Story".to_string()));

        use crate::Positional;
        assert!(warnings.contains(
            &Warning::new(WarningType::EscapedOpenCurly)
                .with_column(6)
                .with_row(10)
                .with_file("test.twee".to_string())
        ));

        assert!(warnings.contains(
            &Warning::new(WarningType::EscapedCloseSquare)
                .with_column(16)
                .with_row(9)
                .with_file("test2.tw".to_string())
        ));

        Ok(())
    }
}