fml 0.4.1

Friendly Markup Language
Documentation
mod debug;
pub use debug::*;

#[cfg(feature = "parser")]
mod try_from;
#[cfg(feature = "parser")]
pub use try_from::*;

#[cfg(feature = "parser")]
use std::str::FromStr;
use std::{
	fmt::Debug,
	io::{self, ErrorKind::Unsupported},
};

use super::data::{FmlValue::*, Content, Section, FmlValue, FmlColor, FmlCodeBlockValue, FmlFunction};

impl FmlValue {
	/// Append another fml-value to oneself.
	pub fn join(&mut self, other: Self) -> io::Result<()> {
		match self {
			Heading1(v) | Heading2(v) | Heading3(v) | Superline(v) | Subline(v) | Bold(v)
			| Italic(v) | Strikethrough(v) | Quote(v) | ListItem(v) | Superscript(v)
			| Subscript(v) | Underline(v) => {
				v.push(other);
			}

			ColorFg(FmlColor { color: _, body })
			| ColorBg(FmlColor { color: _, body })
			| ContentWarning { reason: _, body }
			| Function(FmlFunction {
				name: _,
				params: _,
				body,
			}) => {
				body.push(other);
			}

			Text(s1) => {
				if let Text(s2) = other {
					s1.push_str(&s2);
				} else {
					return Err(io::Error::from(Unsupported));
				}
			}

			_ => {
				return Err(io::Error::from(Unsupported));
			}
		}

		Ok(())
	}
}

impl FmlFunction {
	/// Interprets a generic function to one of the more specified ones.\
	/// If needed params are missing, fills them in with insane defaults chosen by me.\
	/// Generic functions stay as generic functions.
	pub fn interpret(self) -> FmlValue {
		match &self.name.to_lowercase()[..] {
			"fg" | "color" => ColorFg(FmlColor {
				color: self.params.unwrap_or("#db4e11".to_string()),
				body: self.body,
			}),
			"bg" => ColorBg(FmlColor {
				color: self.params.unwrap_or("#501c06".to_string()),
				body: self.body,
			}),
			"cw" => ContentWarning {
				reason: self.params.unwrap_or("Spoilers".to_string()),
				body: self.body,
			},
			"li" => ListItem(self.body),
			"quo" => Quote(self.body),
			_ => Function(self),
		}
	}
}