i8051_disassembler/commands/
note.rs1use crate::address::{AddressRange, AddressSpace, SpaceAddressRange};
2use crate::db::{Db, Error, Note, NoteId};
3use crate::store::fields;
4
5use super::{Apply, Command, Environment, boxed};
6
7register_commands!(SetNote, ClearNote);
8
9#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
10pub struct SetNote {
11 pub address: SpaceAddressRange,
12 #[serde(with = "fields::note")]
13 pub note: Note,
14}
15
16impl SetNote {
17 pub fn new(space: AddressSpace, range: AddressRange, note: Note) -> Self {
18 Self {
19 address: (space, range).into(),
20 note,
21 }
22 }
23}
24
25impl Apply for SetNote {
26 fn apply(
27 self,
28 db: &mut Db,
29 _env: Option<&dyn Environment>,
30 ) -> Result<Vec<Box<dyn Command>>, Error> {
31 let Self { address, note } = self;
32 let SpaceAddressRange { space, range } = address;
33 let id = note.id.clone();
34 let before = db.notes.note_range(space, &id).zip(db.notes
35 .get(&id)
36 .cloned());
37 db.notes.set_address(space, range, note);
38 match before {
39 Some((old_range, old_note)) => Ok(vec![boxed(SetNote {
40 address: (space, old_range).into(),
41 note: old_note,
42 })]),
43 None => Ok(vec![boxed(ClearNote { id })]),
44 }
45 }
46}
47
48#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
49pub struct ClearNote {
50 pub id: NoteId,
51}
52
53impl Apply for ClearNote {
54 fn apply(
55 self,
56 db: &mut Db,
57 _env: Option<&dyn Environment>,
58 ) -> Result<Vec<Box<dyn Command>>, Error> {
59 let Self { id } = self;
60 let Some((space, range, note)) = db.notes.clear_address(&id) else {
61 return Ok(vec![]);
62 };
63 Ok(vec![boxed(SetNote {
64 address: (space, range).into(),
65 note,
66 })])
67 }
68}