#![feature(custom_test_frameworks)]
#![test_runner(crate::testexec)]
#![rexport_test_harness_main = "testmain"]
#![no_main]
#![no_std]

use core::panic::PanicInfo;
mod ser;
mod vgabuff;

// Executed upon startup:
#[no_mangle]
pub extern "C" fn _start() -> !
{
	println!("WELCOME TO LIBERTYOS");

	#[cfg(test)]
	testmain();
	loop {}
}



pub trait CanTest
{
	fn run(&self) -> ();
}

impl<T> CanTest for T
where
	T: Fn(),
{
	fn run(&self)
	{
		serprint("{}..\t", core::any::type_name::<T>());
		self();
		serprintln!("[SUCCESS]");
	}
}


#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum QEMUExitCode
{
	Success = 0x10,
	Failure = 0x11,
}

pub fn exitqemu(exitcode: QEMUExitCode)
{
	use x86_64::instructions::port::Port;
	unsafe
	{
		let mut port = Port::new(0xf4);
		port.write(exitcode as u32);
	}
}


// PANIC HANDLING:
#[cfg(test)] // Used in testing/debug
#[panic_handler]
fn panic(info: &PanicInfo) -> !
{
	serprintln!("[PANIC]\n");
	serprintln!("[ERR] {}\n", info);
	exitqemu(QEMUExitCode::Failure);
	loop {}
}

#[cfg(not(test))] // Used in release
#[panic_handler]
fn panic(info: &PanicInfo) -> !
{
	println!("{}", info);
	loop {}
}
