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
use crate::Position;
use crate::Positional;
use crate::ScriptContent;
use crate::StoryData;
use crate::StoryTitle;
use crate::StylesheetContent;
use crate::TwineContent;

/// An enum of the types of content that can be inside a [`Passage`]
///
/// [`Passage`]: struct.Passage.html
#[derive(Debug)]
pub enum PassageContent {
    /// A non-special passage that contains Twine content
    Normal(TwineContent),

    /// A passage that contains the title of the story
    StoryTitle(StoryTitle),

    /// A passage that contains the story data defined by the specification
    StoryData(Option<StoryData>, Position),

    /// A passage that is tagged with `script` and contains a script
    Script(ScriptContent),

    /// A passage that is tagged with `stylesheet` and contains CSS
    Stylesheet(StylesheetContent),
}

impl Positional for PassageContent {
    fn get_position(&self) -> &Position {
        match self {
            PassageContent::Normal(p) => p.get_position(),
            PassageContent::StoryTitle(t) => t.get_position(),
            PassageContent::StoryData(d, p) => match d {
                Some(d) => &d.position,
                None => p,
            },
            PassageContent::Script(p) => p.get_position(),
            PassageContent::Stylesheet(p) => p.get_position(),
        }
    }

    fn mut_position(&mut self) -> &mut Position {
        match self {
            PassageContent::Normal(p) => p.mut_position(),
            PassageContent::StoryTitle(t) => t.mut_position(),
            PassageContent::StoryData(_, p) => p,
            PassageContent::Script(p) => p.mut_position(),
            PassageContent::Stylesheet(p) => p.mut_position(),
        }
    }
}

impl std::convert::From<TwineContent> for PassageContent {
    fn from(p: TwineContent) -> PassageContent {
        PassageContent::Normal(p)
    }
}

impl std::convert::From<StoryTitle> for PassageContent {
    fn from(t: StoryTitle) -> PassageContent {
        PassageContent::StoryTitle(t)
    }
}

impl std::convert::From<Option<StoryData>> for PassageContent {
    fn from(d: Option<StoryData>) -> PassageContent {
        let pos = match &d {
            Some(data) => data.position.clone(),
            None => Position::default(),
        };
        PassageContent::StoryData(d, pos)
    }
}

impl std::convert::From<StoryData> for PassageContent {
    fn from(d: StoryData) -> PassageContent {
        let pos = d.position.clone();
        PassageContent::StoryData(Some(d), pos)
    }
}

impl std::convert::From<ScriptContent> for PassageContent {
    fn from(s: ScriptContent) -> PassageContent {
        PassageContent::Script(s)
    }
}

impl std::convert::From<StylesheetContent> for PassageContent {
    fn from(s: StylesheetContent) -> PassageContent {
        PassageContent::Stylesheet(s)
    }
}