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