use crate::rust_gen::CodeGenContext;
use quote::quote;
pub fn generate_error_type_definitions(ctx: &CodeGenContext) -> Vec<proc_macro2::TokenStream> {
let mut definitions = Vec::new();
if ctx.needs_zerodivisionerror {
definitions.push(quote! {
#[derive(Debug, Clone)]
pub struct ZeroDivisionError {
message: String,
}
impl std::fmt::Display for ZeroDivisionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "division by zero: {}", self.message)
}
}
impl std::error::Error for ZeroDivisionError {}
impl ZeroDivisionError {
pub fn new(message: impl Into<String>) -> Self {
Self { message: message.into() }
}
}
});
}
if ctx.needs_indexerror {
definitions.push(quote! {
#[derive(Debug, Clone)]
pub struct IndexError {
message: String,
}
impl std::fmt::Display for IndexError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "index out of range: {}", self.message)
}
}
impl std::error::Error for IndexError {}
impl IndexError {
pub fn new(message: impl Into<String>) -> Self {
Self { message: message.into() }
}
}
});
}
definitions
}