From 6dc9bbcc2dcf974e2cf15aa7e1ad5e7ae065ab31 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Mon, 13 Apr 2020 19:12:21 +0100 Subject: [PATCH] dedupe optional lines for item --- src/item.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/item.rs b/src/item.rs index 8d0589c..940eaf5 100644 --- a/src/item.rs +++ b/src/item.rs @@ -1,4 +1,4 @@ -use crate::{AnimationFrames, Image, mock, from_base36, ToBase36}; +use crate::{AnimationFrames, Image, mock, from_base36, ToBase36, optional_data_line}; #[derive(Debug, Eq, PartialEq)] pub struct Item { @@ -8,6 +8,15 @@ pub struct Item { pub dialogue_id: Option, } +impl Item { + fn name_line(&self) -> String { + optional_data_line("NAME", self.name.as_ref()) + } + fn dialogue_line(&self) -> String { + optional_data_line("DLG", self.dialogue_id.as_ref()) + } +} + impl From for Item { fn from(string: String) -> Item { let mut lines: Vec<&str> = string.lines().collect(); @@ -47,8 +56,8 @@ impl ToString for Item { "ITM {}\n{}{}{}", self.id.to_base36(), self.animation_frames.to_string(), - if self.name.is_some() { format!("\nNAME {}", self.name.as_ref().unwrap()) } else { "".to_string() }, - if self.dialogue_id.is_some() { format!("\nDLG {}", self.dialogue_id.as_ref().unwrap()) } else { "".to_string() }, + self.name_line(), + self.dialogue_line(), ) } }