i8051_disassembler/commands/
function.rs1use crate::address::{AddressSpace, AddressValue, SpaceAddressValue};
2use crate::db::{Db, Error, Function};
3
4use super::{Apply, Command, Environment, boxed};
5
6register_commands!(SetFunction, ClearFunction);
7
8#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
9pub struct SetFunction {
10 pub address: SpaceAddressValue,
11 pub function: Function,
12}
13
14impl SetFunction {
15 pub fn new(space: AddressSpace, offset: AddressValue, function: Function) -> Self {
16 Self {
17 address: (space, offset).into(),
18 function,
19 }
20 }
21}
22
23impl Apply for SetFunction {
24 fn apply(
25 self,
26 db: &mut Db,
27 _env: Option<&dyn Environment>,
28 ) -> Result<Vec<Box<dyn Command>>, Error> {
29 let Self { address, function } = self;
30 let SpaceAddressValue { space, offset } = address;
31 let region = db.region_mut(space);
32 let before = region.get_function(offset).cloned();
33 region.set_function(function);
34 Ok(match before {
35 Some(function) => vec![boxed(SetFunction { address, function })],
36 None => vec![boxed(ClearFunction { address })],
37 })
38 }
39}
40
41#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
42pub struct ClearFunction {
43 pub address: SpaceAddressValue,
44}
45
46impl ClearFunction {
47 pub fn new(space: AddressSpace, offset: AddressValue) -> Self {
48 Self {
49 address: (space, offset).into(),
50 }
51 }
52}
53
54impl Apply for ClearFunction {
55 fn apply(
56 self,
57 db: &mut Db,
58 _env: Option<&dyn Environment>,
59 ) -> Result<Vec<Box<dyn Command>>, Error> {
60 let Self { address } = self;
61 let SpaceAddressValue { space, offset } = address;
62 let region = db.region_mut(space);
63 let before = region.get_function(offset).cloned();
64 region.clear_function(offset);
65 Ok(match before {
66 Some(function) => vec![boxed(SetFunction { address, function })],
67 None => vec![],
68 })
69 }
70}