use crate::backend_ops::BackendOps;
use coeus_core::{BackendError, Scalar};
pub(super) fn parse_subscript(subscript: &str) -> (Vec<&str>, &str) {
let subscript = subscript.trim();
let (lhs, rhs) = if let Some(pos) = subscript.find("->") {
(&subscript[..pos], &subscript[pos + 2..])
} else {
(subscript, "")
};
let operands: Vec<&str> = lhs.split(',').map(str::trim).collect();
(operands, rhs.trim())
}
pub(super) fn check_operand_count<T: Scalar, B: BackendOps<T>>(
subscript: &str,
subscript_operands: usize,
provided: usize,
) -> Result<(), B::Error> {
if subscript_operands == provided {
return Ok(());
}
Err(B::Error::from(BackendError::EinsumOperandCount {
subscript: subscript.to_string(),
subscript_operands,
provided,
}))
}
pub(super) fn check_rank<T: Scalar, B: BackendOps<T>>(
subscript: &str,
operand: usize,
actual: usize,
expected: usize,
) -> Result<(), B::Error> {
if actual == expected {
return Ok(());
}
Err(B::Error::from(BackendError::EinsumRank {
subscript: subscript.to_string(),
operand,
expected,
actual,
}))
}
pub(super) fn check_dim<T: Scalar, B: BackendOps<T>>(
subscript: &str,
operand: usize,
actual: usize,
expected: usize,
) -> Result<(), B::Error> {
if actual == expected {
return Ok(());
}
Err(B::Error::from(BackendError::EinsumDim {
subscript: subscript.to_string(),
operand,
expected,
actual,
}))
}
pub(super) fn unsupported<T: Scalar, B: BackendOps<T>>(
subscript: &str,
operand_count: usize,
) -> B::Error {
B::Error::from(BackendError::EinsumUnsupportedPattern {
subscript: subscript.to_string(),
operand_count,
})
}