Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 35 additions & 35 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,11 @@ pub struct Database {
pub blocks: Vec<DbColoredBlock>,
pub bookmarks: Vec<usize>,

// `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<DbComment>,

// 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<usize, String>,
}

#[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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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);
}
}
11 changes: 10 additions & 1 deletion src/hex/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) -> 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()));

Expand Down
8 changes: 0 additions & 8 deletions test_data/db.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
34 changes: 34 additions & 0 deletions test_data/old_db.toml
Original file line number Diff line number Diff line change
@@ -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"
Loading