Documentation
#![no_std]

use core::fmt;

#[doc(hidden)]
pub use kons_macros::kons_impl;

#[doc(hidden)]
#[macro_export]
macro_rules! list_impl {
	( $(($($done:tt)*))* @ ) => {
		$crate::kons_impl![ ($crate::) ($crate::Nil) $( ($($done)*) )* ]
	};
	( $(($($done:tt)*))* @ ..$(,)? ) => {
		$crate::kons_impl![ ($crate::) (_) $( ($($done)*) )* ]
	};
	( $(($($done:tt)*))* @ ..$here:expr$(,)? ) => {
		$crate::kons_impl![ ($crate::) ($here) $( ($($done)*) )* ]
	};

	( $(($($done:tt)*))* @ $here:expr $(,)? ) => {
		$crate::list_impl![ $( ($($done)*) )* ($here) @ ]
	};
	( $(($($done:tt)*))* @ $here:expr, $($there:tt)+) => {
		$crate::list_impl![ $( ($($done)*) )* ($here) @ $($there)+ ]
	};

	( $(($($done:tt)*))* @ $here:pat $(,)? ) => {
		$crate::list_impl![ $( ($($done)*) )* ($here) @ ]
	};
	( $(($($done:tt)*))* @ $here:pat, $($there:tt)+) => {
		$crate::list_impl![ $( ($($done)*) )* ($here) @ $($there)+ ]
	};
}

#[macro_export]
macro_rules! list {
	($($tt:tt)*) => {
		$crate::list_impl![
			@ $($tt)*
		]
	};
}

#[macro_export]
macro_rules! List {
	() => {
		$crate::Nil
	};
	(..$(,)?) => {
		_
	};
	($ty:ty$(,)?) => {
		$crate::List::<$ty, $crate::Nil>
	};
	($ty:ty, $($tt:tt)*) => {
		$crate::List::<$ty, $crate::List![
			$($tt)*
		]>
	};
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Nil;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct List<H, T> {
	pub here: H,
	pub there: T,
}

impl fmt::Debug for Nil {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		f.debug_list().finish()
	}
}

pub trait DebugList {
	fn fmt_list(&self, list: &mut fmt::DebugList<'_, '_>);
}

impl DebugList for Nil {
	fn fmt_list(&self, _: &mut fmt::DebugList<'_, '_>) {}
}

impl<H: fmt::Debug, T: DebugList> DebugList for List<H, T> {
	fn fmt_list(&self, list: &mut fmt::DebugList<'_, '_>) {
		list.entry(&self.here);
		self.there.fmt_list(list);
	}
}

impl<H: fmt::Debug, T: DebugList> fmt::Debug for List<H, T> {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		let mut list = f.debug_list();
		self.fmt_list(&mut list);
		list.finish()
	}
}

#[cfg(test)]
mod tests {
	extern crate std;

	#[test]
	fn test_macros() {
		let x = list![1, 2, 0.1];
		let y: List![&str, u32, f64, usize, ..] = list!["hi", 1, 3.0, 0xACAB, list!["more", "stuff"], list!["even", "more", "stuff"], ..x];
		let list![_x, _y, ..] = y;
	}
}