i8051_disassembler/commands/
comment.rs1use crate::address::{SpaceAddressSet, SpaceAddressValue};
2use crate::db::{Db, Error};
3
4use super::{Apply, Command, Environment, boxed};
5
6#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
7pub struct SetComment {
8 pub address: SpaceAddressValue,
9 pub comment: String,
10}
11
12register!(SetComment(
13 address: SpaceAddressValue,
15 comment: String,
16));
17
18impl Apply for SetComment {
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, comment } = self;
25 let SpaceAddressValue { space, offset } = address;
26 let region = db.region_mut(space);
27 let before = region.get_comment(offset).map(str::to_owned);
28 region.set_comment(offset, &comment);
29 Ok(match before {
30 Some(comment) => vec![boxed(SetComment { address, comment })],
31 None => vec![boxed(ClearComment::new((space, offset)))],
32 })
33 }
34}
35
36#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
37pub struct ClearComment {
38 pub addresses: SpaceAddressSet,
39}
40
41register!(ClearComment(
42 addresses: SpaceAddressSet,
44));
45
46impl Apply for ClearComment {
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, comment) in region.clear_comments_in(range) {
58 undo.push(boxed(SetComment::new((space, offset), comment)));
59 }
60 }
61 Ok(undo)
62 }
63}