use corevm_host::{fs as corevm_fs, PageNum};
use std::borrow::Cow;

#[derive(thiserror::Error, Debug)]
pub enum CreateWorkPackageError {
	#[error("No such page: {0}")]
	NoSuchPage(PageNum),
}

#[derive(thiserror::Error, Debug)]
pub enum SimulateError {
	#[error("Failed to join: {0}")]
	Join(#[from] tokio::task::JoinError),
	#[error("CoreVM engine init error: {0:?}")]
	EngineInit(#[from] corevm_engine::InitError),
	#[error("CoreVM engine runtime error: {0:?}")]
	EngineRun(#[from] corevm_engine::RunError),
	#[error("PolkaVM error: {0}")]
	Pvm(#[from] polkavm::Error),
	#[error("CoreVM guest panicked")]
	Panic,
	#[error("CoreVM FS error")]
	Fs(#[from] corevm_fs::Error),
}

#[derive(thiserror::Error, Debug)]
pub enum PackageError {
	#[error("Node error: {0}")]
	Node(#[from] jam_std_common::NodeError),
	#[error("Work package failed: {0}")]
	Failed(Cow<'static, str>),
}

impl From<PackageError> for jam_std_common::NodeError {
	fn from(e: PackageError) -> Self {
		match e {
			PackageError::Node(e) => e,
			PackageError::Failed(message) => jam_std_common::NodeError::Other(message.into()),
		}
	}
}
