reifydb-core 0.10.1

Core database interfaces and data structures for ReifyDB
Documentation
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 ReifyDB

use reifydb_value::{
	error::{Diagnostic, Error, IntoDiagnostic},
	fragment::Fragment,
	value::value_type::ValueType,
};

#[derive(Debug, thiserror::Error)]
pub enum CastError {
	#[error("failed to cast to {target}")]
	InvalidNumber {
		fragment: Fragment,
		target: ValueType,
		cause: Diagnostic,
	},

	#[error("failed to cast to bool")]
	InvalidBoolean {
		fragment: Fragment,
		cause: Diagnostic,
	},

	#[error("failed to cast to {target}")]
	InvalidUuid {
		fragment: Fragment,
		target: ValueType,
		cause: Diagnostic,
	},

	#[error("failed to cast to {target}")]
	InvalidTemporal {
		fragment: Fragment,
		target: ValueType,
		cause: Diagnostic,
	},

	#[error("failed to cast BLOB to UTF8")]
	InvalidBlobToUtf8 {
		fragment: Fragment,
		cause: Diagnostic,
	},
}

impl IntoDiagnostic for CastError {
	fn into_diagnostic(self) -> Diagnostic {
		match self {
			CastError::InvalidNumber { fragment, target, cause } => Diagnostic {
				code: "CAST_002".to_string(),
				rql: None,
				message: format!("failed to cast to {}", target),
				fragment,
				label: Some(format!("failed to cast to {}", target)),
				help: None,
				notes: vec![],
				column: None,
				cause: Some(Box::from(cause)),
				operator_chain: None,
			},
			CastError::InvalidBoolean { fragment, cause } => Diagnostic {
				code: "CAST_004".to_string(),
				rql: None,
				message: "failed to cast to bool".to_string(),
				fragment,
				label: Some("failed to cast to bool".to_string()),
				help: None,
				notes: vec![],
				column: None,
				cause: Some(Box::from(cause)),
				operator_chain: None,
			},
			CastError::InvalidUuid { fragment, target, cause } => Diagnostic {
				code: "CAST_005".to_string(),
				rql: None,
				message: format!("failed to cast to {}", target),
				fragment,
				label: Some(format!("failed to cast to {}", target)),
				help: None,
				notes: vec![],
				column: None,
				cause: Some(Box::from(cause)),
				operator_chain: None,
			},
			CastError::InvalidTemporal { fragment, target, cause } => Diagnostic {
				code: "CAST_003".to_string(),
				rql: None,
				message: format!("failed to cast to {}", target),
				fragment,
				label: Some(format!("failed to cast to {}", target)),
				help: None,
				notes: vec![],
				column: None,
				cause: Some(Box::from(cause)),
				operator_chain: None,
			},
			CastError::InvalidBlobToUtf8 { fragment, cause } => Diagnostic {
				code: "CAST_006".to_string(),
				rql: None,
				message: "failed to cast BLOB to UTF8".to_string(),
				fragment,
				label: Some("failed to cast BLOB to UTF8".to_string()),
				help: Some("BLOB contains invalid UTF-8 bytes. Consider using to_utf8_lossy() function instead"
					.to_string()),
				notes: vec![],
				column: None,
				cause: Some(Box::from(cause)),
				operator_chain: None,
			},
		}
	}
}

impl From<CastError> for Error {
	fn from(err: CastError) -> Self {
		Error(Box::new(err.into_diagnostic()))
	}
}