Skip to content

Commit

Permalink
Merge pull request #9 from BrettDong/msgctxt-optional
Browse files Browse the repository at this point in the history
MessageView::msgctxt() should return Option<&str>
  • Loading branch information
BrettDong authored Oct 7, 2023
2 parents 21bc9fa + bbaf3fa commit 3dd5f18
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/catalog/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<'a> MessageView for MessageMutProxy<'a> {
self.message().flags()
}

fn msgctxt(&self) -> &str {
fn msgctxt(&self) -> Option<&str> {
self.message().msgctxt()
}

Expand Down
14 changes: 9 additions & 5 deletions src/message/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub trait MessageView {
fn flags(&self) -> &MessageFlags;

/// Get context field of the message.
fn msgctxt(&self) -> &str;
fn msgctxt(&self) -> Option<&str>;

/// Get msgid field of the message.
fn msgid(&self) -> &str;
Expand Down Expand Up @@ -132,8 +132,12 @@ impl MessageView for Message {
&self.flags
}

fn msgctxt(&self) -> &str {
&self.msgctxt
fn msgctxt(&self) -> Option<&str> {
if self.msgctxt.is_empty() {
None
} else {
Some(&self.msgctxt)
}
}

fn msgid(&self) -> &str {
Expand Down Expand Up @@ -239,7 +243,7 @@ impl ToOwned for dyn MessageView {
comments: self.comments().to_string(),
source: self.source().to_string(),
flags: self.flags().clone(),
msgctxt: self.msgctxt().to_string(),
msgctxt: self.msgctxt().unwrap_or("").to_string(),
msgid: self.msgid().to_string(),
msgid_plural: String::default(),
msgstr: self.msgstr().unwrap().to_string(),
Expand All @@ -251,7 +255,7 @@ impl ToOwned for dyn MessageView {
comments: self.comments().to_string(),
source: self.source().to_string(),
flags: self.flags().clone(),
msgctxt: self.msgctxt().to_string(),
msgctxt: self.msgctxt().unwrap_or("").to_string(),
msgid: self.msgid().to_string(),
msgid_plural: self.msgid_plural().unwrap().to_string(),
msgstr: String::default(),
Expand Down
6 changes: 2 additions & 4 deletions src/mo_file/mo_file_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use crate::{catalog::Catalog, message::MessageView};

fn original_repr_len(message: &dyn MessageView) -> usize {
let mut result = 0usize;
let ctxt = message.msgctxt();
if !ctxt.is_empty() {
if let Some(ctxt) = message.msgctxt() {
result += ctxt.len() + 1;
}
result += message.msgid().len();
Expand All @@ -25,8 +24,7 @@ fn write_original_repr(
writer: &mut BufWriter<std::fs::File>,
message: &dyn MessageView,
) -> Result<(), std::io::Error> {
let ctxt = message.msgctxt();
if !ctxt.is_empty() {
if let Some(ctxt) = message.msgctxt() {
writer.write_all(ctxt.as_bytes())?;
writer.write_all(&[4u8])?;
}
Expand Down
4 changes: 2 additions & 2 deletions src/po_file/po_file_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ pub fn write(catalog: &Catalog, path: &Path) -> Result<(), std::io::Error> {
writer.write_all(message.flags().to_string().as_bytes())?;
writer.write_all(b"\n")?;
}
if !message.msgctxt().is_empty() {
write_field(&mut writer, "msgctxt", message.msgctxt())?;
if let Some(ctxt) = message.msgctxt() {
write_field(&mut writer, "msgctxt", ctxt)?;
}
if message.is_singular() {
write_field(&mut writer, "msgid", message.msgid())?;
Expand Down

0 comments on commit 3dd5f18

Please sign in to comment.