use endpoint::message::Command;
use utility::lalrpop::Slice;

grammar;

pub Command: Command<'input> = {
    <c:AppendCommand> => c,
    <c:CreateJournalCommand> => c,
    <c:EnumerateCommand> => c,
    <c:FoldlCommand> => c,
    <c:LoadCommand> => c,
    <c:VersionCommand> => c,
};

pub AppendCommand: Command<'input> = {
    "APPEND" "ONTO" <journal:String> "ENTRY" <entry:String> =>
        Command::Append{journal, entry},
};

pub CreateJournalCommand: Command<'input> = {
    "CREATE" "JOURNAL" <journal:String> =>
        Command::CreateJournal{journal},
};

pub EnumerateCommand: Command<'input> = {
    "ENUMERATE" <journal:String> =>
        Command::Enumerate{journal},
};

pub FoldlCommand: Command<'input> = {
    "FOLDL" <journal:String> "USING" <using:String> "FROM" <extension:String> =>
        Command::Foldl{journal, using, extension},
};

pub LoadCommand: Command<'input> = {
    "LOAD" <extension:String> "FROM" <path:String> =>
        Command::Load{extension, path},
};

pub VersionCommand: Command<'input> = {
    "VERSION" =>
        Command::Version,
};

// BUG: This should support non-UTF-8 input. See the issue [1] for more
// information on the implementation status in LALRPOP.
//
// [1]: https://github.com/lalrpop/lalrpop/issues/229.
pub String: Slice<'input, u8> =
    <string:r"'[^']*'"> => &string.as_bytes()[1 .. string.len() - 1];
