i8051_disassembler/commands/
label.rs1use crate::address::{AddressSpace, AddressValue, SpaceAddressValue};
2use crate::db::{Db, Error};
3
4use super::{Apply, Command, Environment, boxed};
5
6register_commands!(SetLabel, ClearLabel);
7
8#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
9pub struct SetLabel {
10 pub address: SpaceAddressValue,
11 pub label: String,
12}
13
14impl SetLabel {
15 pub fn new(space: AddressSpace, offset: AddressValue, label: impl Into<String>) -> Self {
16 Self {
17 address: (space, offset).into(),
18 label: label.into(),
19 }
20 }
21}
22
23impl Apply for SetLabel {
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, label } = self;
30 let SpaceAddressValue { space, offset } = address;
31 let region = db.region_mut(space);
32 let before = region.get_label(offset).map(str::to_owned);
33 region.set_label(offset, &label);
34 Ok(match before {
35 Some(label) => vec![boxed(SetLabel { address, label })],
36 None => vec![boxed(ClearLabel { address })],
37 })
38 }
39}
40
41#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
42pub struct ClearLabel {
43 pub address: SpaceAddressValue,
44}
45
46impl ClearLabel {
47 pub fn new(space: AddressSpace, offset: AddressValue) -> Self {
48 Self {
49 address: (space, offset).into(),
50 }
51 }
52}
53
54impl Apply for ClearLabel {
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_label(offset).map(str::to_owned);
64 region.clear_label(offset);
65 Ok(match before {
66 Some(label) => vec![boxed(SetLabel { address, label })],
67 None => vec![],
68 })
69 }
70}