use std::sync::Arc;
use crate::util::{Utf16String, ValidateError};
use super::{IStandardExpression, StandardExpressionResult};
pub struct Assignation {
left: Arc<dyn IStandardExpression>,
right: Option<Arc<dyn IStandardExpression>>,
}
impl Assignation {
pub(crate) fn new(
left: Option<Arc<dyn IStandardExpression>>,
right: Option<Arc<dyn IStandardExpression>>,
) -> Result<Self, ValidateError> {
let left = left.ok_or_else(|| ValidateError::IllegalArgument {
message: Some("Assignation left side cannot be null".to_owned()),
})?;
Ok(Self { left, right })
}
pub fn get_left(&self) -> &dyn IStandardExpression {
self.left.as_ref()
}
pub fn get_right(&self) -> Option<&dyn IStandardExpression> {
self.right.as_deref()
}
pub fn get_string_representation(&self) -> StandardExpressionResult<Utf16String> {
let mut units = self.left.get_string_representation()?.as_utf16().to_vec();
if let Some(right) = &self.right {
units.push(b'=' as u16);
if right.is_complex() {
units.push(b'(' as u16);
}
units.extend_from_slice(right.get_string_representation()?.as_utf16());
if right.is_complex() {
units.push(b')' as u16);
}
}
Ok(Utf16String::from_utf16(units))
}
}