use crate::python_ast::{CodeGen, CodeGenContext, ExprType, PythonOptions, SymbolTableScopes};
use proc_macro2::TokenStream;
use quote::quote;
pub trait PythonOperator: Clone + std::fmt::Debug {
fn to_rust_op(&self) -> Result<TokenStream, Box<dyn std::error::Error>>;
fn is_unknown(&self) -> bool;
}
pub trait BinaryOperation: Clone + std::fmt::Debug {
type OperatorType: PythonOperator;
fn operator(&self) -> &Self::OperatorType;
fn left(&self) -> &ExprType;
fn right(&self) -> &ExprType;
fn generate_rust_code(
&self,
ctx: CodeGenContext,
options: PythonOptions,
symbols: SymbolTableScopes,
) -> Result<TokenStream, Box<dyn std::error::Error>> {
let left = self.left().clone().to_rust(ctx.clone(), options.clone(), symbols.clone())?;
let right = self.right().clone().to_rust(ctx, options, symbols)?;
let op = self.operator().to_rust_op()?;
Ok(quote!((#left) #op (#right)))
}
}
pub trait PositionInfo {
fn position_info(&self) -> (Option<usize>, Option<usize>, Option<usize>, Option<usize>);
fn has_position(&self) -> bool {
let (lineno, col_offset, _, _) = self.position_info();
lineno.is_some() && col_offset.is_some()
}
}
pub trait DebugInfo {
fn debug_description(&self) -> String;
fn node_type(&self) -> &'static str;
}
pub trait ChainableOperation {
type Operand;
type Operator;
fn operands(&self) -> Vec<&Self::Operand>;
fn operators(&self) -> Vec<&Self::Operator>;
fn generate_chained_rust(
&self,
ctx: CodeGenContext,
options: PythonOptions,
symbols: SymbolTableScopes,
) -> Result<TokenStream, Box<dyn std::error::Error>>;
}
pub trait FromPythonString: Sized {
fn from_python_string(s: &str) -> Option<Self>;
fn unknown() -> Self;
fn parse_or_unknown(s: &str) -> Self {
Self::from_python_string(s).unwrap_or_else(Self::unknown)
}
}
pub trait ErrorContext {
fn with_context(&self, operation: &str) -> String;
}
impl<T: std::fmt::Debug> ErrorContext for T {
fn with_context(&self, operation: &str) -> String {
format!("Error during {}: {:?}", operation, self)
}
}