use crate::{
metadata::{
method::MethodRc, tables::MethodSemanticsAttributes, token::Token,
typesystem::CilTypeReference,
},
Result,
};
pub struct MethodSemantics {
pub rid: u32,
pub token: Token,
pub offset: usize,
pub semantics: u32,
pub method: MethodRc,
pub association: CilTypeReference,
}
impl MethodSemantics {
pub fn apply(&self) -> Result<()> {
match &self.association {
CilTypeReference::Property(property) => match self.semantics {
MethodSemanticsAttributes::SETTER => property
.fn_setter
.set(self.method.clone().into())
.map_err(|_| malformed_error!("Property setter already set".to_string())),
MethodSemanticsAttributes::GETTER => property
.fn_getter
.set(self.method.clone().into())
.map_err(|_| malformed_error!("Property getter already set".to_string())),
MethodSemanticsAttributes::OTHER => property
.fn_other
.set(self.method.clone().into())
.map_err(|_| malformed_error!("Property other already set".to_string())),
_ => Err(malformed_error!("Invalid property semantics".to_string())),
},
CilTypeReference::Event(event) => match self.semantics {
MethodSemanticsAttributes::ADD_ON => event
.fn_on_add
.set(self.method.clone().into())
.map_err(|_| malformed_error!("Event add method already set".to_string())),
MethodSemanticsAttributes::REMOVE_ON => event
.fn_on_remove
.set(self.method.clone().into())
.map_err(|_| malformed_error!("Event remove method already set".to_string())),
MethodSemanticsAttributes::FIRE => event
.fn_on_raise
.set(self.method.clone().into())
.map_err(|_| malformed_error!("Event raise method already set".to_string())),
MethodSemanticsAttributes::OTHER => event
.fn_on_other
.set(self.method.clone().into())
.map_err(|_| malformed_error!("Event other method already set".to_string())),
_ => Err(malformed_error!("Invalid event semantics".to_string())),
},
_ => Err(malformed_error!("Invalid association".to_string())),
}
}
}