diff --git a/src/database.rs b/src/database.rs index 6cf8dab..3617cd6 100644 --- a/src/database.rs +++ b/src/database.rs @@ -55,24 +55,11 @@ pub struct Database { pub blocks: Vec, pub bookmarks: Vec, - // `comment_name_list` is used to show comments in Names list - // and also on the conversion from selected item on the list - // to file offset passed to goto() - pub comment_name_list: Vec, - - // TODO: comments and comment_name_list are redundant, we should only store one of them - // but to avoid breaking existing .dz6 files, we will keep both for now - // The `comments` format is more compact and doesn't require a new type - // so maybe later we default to using only it + /// offset -> comment map + /// Used to populate the comment_name_list as well pub comments: BTreeMap, } -#[derive(Debug, Serialize, Deserialize, PartialEq)] -pub struct DbComment { - pub offset: usize, - pub comment: String, -} - #[derive(Debug, Serialize, Deserialize, PartialEq)] pub struct DbColoredBlock { pub start: usize, @@ -86,9 +73,9 @@ fn hex_view_from_db(db: Database) -> HexView { blocks: db.blocks.into_iter().map(colored_block_from_db).collect(), bookmarks: db.bookmarks, comment_name_list: db - .comment_name_list - .into_iter() - .map(comment_from_db) + .comments + .iter() + .map(|(&k, v)| Comment::new(k, v)) .collect(), comments: db.comments.into_iter().collect(), editing_hex: true, // otherwise it defaults to false if a .dz6 file exists for the target @@ -103,12 +90,6 @@ fn colored_block_from_db(db_block: DbColoredBlock) -> ColoredBlock { fg_color: db_block.fg_color, } } -fn comment_from_db(db_comment: DbComment) -> Comment { - Comment { - offset: db_comment.offset, - comment: db_comment.comment, - } -} /// HexView to Database conversion, /// takes a reference because we don't want to consume the live HexView that is @@ -117,11 +98,6 @@ fn hex_view_to_db(hex_view: &HexView) -> Database { Database { blocks: hex_view.blocks.iter().map(colored_block_to_db).collect(), bookmarks: hex_view.bookmarks.clone(), - comment_name_list: hex_view - .comment_name_list - .iter() - .map(comment_to_db) - .collect(), comments: hex_view .comments .iter() @@ -137,12 +113,6 @@ fn colored_block_to_db(block: &ColoredBlock) -> DbColoredBlock { fg_color: block.fg_color, } } -fn comment_to_db(comment: &Comment) -> DbComment { - DbComment { - offset: comment.offset, - comment: comment.comment.clone(), - } -} #[cfg(test)] mod tests { @@ -166,4 +136,34 @@ mod tests { toml::from_str(&serialized).expect("Failed to deserialize database"); assert_eq!(db, re_deserialized); } + + #[test] + fn test_old_database_still_parses() { + // db file generated with the hexview serialization + let db_file = "test_data/old_db.toml"; + let db_str = fs::read_to_string(db_file).expect("Failed to read test database file"); + + let db: Database = toml::from_str(&db_str).expect("Failed to deserialize database"); + let serialized = toml::to_string_pretty(&db).expect("Failed to serialize database"); + + // tests that we can deserialize the serialized string and get the same data back + let re_deserialized: Database = + toml::from_str(&serialized).expect("Failed to deserialize database"); + assert_eq!(db, re_deserialized); + } + + #[test] + fn test_comment_name_list_gets_populated() { + let list = vec![ + Comment::new(21, " one comment"), + Comment::new(101, "comment"), + ]; + + let db_file = "test_data/db.toml"; + let db_str = fs::read_to_string(db_file).expect("Failed to read test database file"); + let db: Database = toml::from_str(&db_str).expect("Failed to deserialize databse"); + let hex_view = hex_view_from_db(db); + + assert_eq!(hex_view.comment_name_list, list); + } } diff --git a/src/hex/comment.rs b/src/hex/comment.rs index caadb1c..50f3833 100644 --- a/src/hex/comment.rs +++ b/src/hex/comment.rs @@ -11,12 +11,21 @@ use tui_input::backend::crossterm::EventHandler; use crate::{app::App, commands::Commands, editor::UIState}; -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct Comment { pub offset: usize, pub comment: String, } +impl Comment { + pub fn new(offset: usize, comment: impl Into) -> Self { + Comment { + offset, + comment: comment.into(), + } + } +} + pub fn dialog_comment_draw(app: &mut App, frame: &mut Frame) { let para = Paragraph::new(format!(";{}", app.hex_view.comment_input.value())); diff --git a/test_data/db.toml b/test_data/db.toml index d2fbbac..c6fcba3 100644 --- a/test_data/db.toml +++ b/test_data/db.toml @@ -21,14 +21,6 @@ end = 214 bg_color = 4160309102 fg_color = 4169677532 -[[comment_name_list]] -offset = 21 -comment = " one comment" - -[[comment_name_list]] -offset = 101 -comment = "comment" - [comments] 21 = " one comment" 101 = "comment" diff --git a/test_data/old_db.toml b/test_data/old_db.toml new file mode 100644 index 0000000..d2fbbac --- /dev/null +++ b/test_data/old_db.toml @@ -0,0 +1,34 @@ +bookmarks = [ + 288, + 325, +] + +[[blocks]] +start = 16 +end = 21 +bg_color = 446956584 +fg_color = 2910089301 + +[[blocks]] +start = 128 +end = 134 +bg_color = 3843912424 +fg_color = 2232455935 + +[[blocks]] +start = 182 +end = 214 +bg_color = 4160309102 +fg_color = 4169677532 + +[[comment_name_list]] +offset = 21 +comment = " one comment" + +[[comment_name_list]] +offset = 101 +comment = "comment" + +[comments] +21 = " one comment" +101 = "comment"