use serde::{Deserialize, Deserializer};

pub type BoardPosition = (u32, u32);

#[derive(Debug, Deserialize, PartialEq, Eq)]
struct ParseBoardPositionError;

pub fn deserialize_position<'de, D>(deserializer: D) -> Result<BoardPosition, D::Error>
where
	D: Deserializer<'de>,
{
	let str = match String::deserialize(deserializer) {
		Ok(v) => v,
		Err(e) => {
			panic!("Tried deserializing a non-string type\nerror: {}", e);
		}
	};
	let str = str.to_uppercase();

	let mut chars = str.chars();

	let a = chars.next().unwrap_or('A');
	let b = chars.next().unwrap_or('1');

	let a = (a.to_ascii_uppercase() as u32).saturating_sub(64);
	if a == 0 {
		panic!("Second position parameter {} is 0 or lower!", str);
	}

	let b = b.to_digit(10).unwrap_or(0);

	Ok((a, b))
}
