Skip to main content

i8051_disassembler/commands/
comment.rs

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