epkard 0.1.1

A generalized framework for creating branching narratives
Documentation
use crate::*;

/**

A macro for creating sequential paragraph [`Node`](trait.Node.html)s

# Example
```
use epkard::*;

paragraphs! {
    #[derive(Serialize, Deserialize)]
    Example {
        "This is the first paragraph."
        "This is the second paragraph."
        "This is the third paragraph."
    }
}

impl Node for Example {
    type State = ();
    fn next(self, rt: &mut Runtime<Self>) -> Control<Self> {
        // Print the text
        rt.println(self.text());

        // Wait for the user to continue
        // rt.wait()?;

        // Get the next paragraph
        self.next()
    }
}

run_default::<Example>();
```
*/
#[macro_export]
macro_rules! paragraphs {
    ($(#[$attr:meta])* $name:ident $(#[$impl_attr:meta])* { $($par:expr)* }) => {
        $(#[$attr])*
        #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
        pub struct $name {
            index: usize,
        }
        $(#[$impl_attr])*
        impl $name {
            /// Create a new paragraphs node
            pub fn new() -> Self {
                $name {
                    index: 0,
                }
            }
            /// Get the text of the current paragraph
            #[allow(unused_assignments)]
            #[allow(unused_variables)]
            #[allow(unused_mut)]
            #[allow(clippy::useless_format)]
            pub fn text(&self) -> String {
                let mut curr = 0;
                $({
                    if curr == self.index {
                        return $par.to_string();
                    } else {
                        curr += 1;
                    }
                })*
                String::new()
            }
            /// Get the next paragraphs node
            #[allow(unused_mut)]
            pub fn next(self) -> Control<Self> {
                let mut max = 0;
                $({
                    stringify!($par);
                    max += 1;
                })*
                if max > 0 && self.index < max - 1 {
                    next($name {
                        index: self.index + 1
                    })
                } else {
                    exit()
                }
            }
        }
    };
}

paragraphs! {
    #[doc = "An example struct generated by the [`paragraphs!`](macro.paragraphs.html) macro."]
    #[doc = ""]
    #[doc = "All structs generated by [`paragraphs!`](macro.paragraphs.html) will have the same functions."]
    ParagraphsExample #[allow(dead_code)] {}
}

fn _compiles() {
    paragraphs! {
        Test {
            "This is a test paragraph."
            "The is paragraph number 2."
        }
    }
}