Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic on missing metadata. #5

Open
ogoffart opened this issue Jun 9, 2023 · 1 comment
Open

Panic on missing metadata. #5

ogoffart opened this issue Jun 9, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@ogoffart
Copy link

ogoffart commented Jun 9, 2023

Metadata::parse panics if some metadata header is not present because it unwraps the result of get()

polib/src/metadata.rs

Lines 69 to 81 in a1b9b57

project_id_version: key_values.get("Project-Id-Version").unwrap().to_string(),
pot_creation_date: key_values.get("POT-Creation-Date").unwrap().to_string(),
po_revision_date: key_values.get("PO-Revision-Date").unwrap().to_string(),
last_translator: key_values.get("Last-Translator").unwrap_or(&"").to_string(),
language_team: key_values.get("Language-Team").unwrap().to_string(),
mime_version: key_values.get("MIME-Version").unwrap().to_string(),
content_type: key_values.get("Content-Type").unwrap().to_string(),
content_transfer_encoding: key_values
.get("Content-Transfer-Encoding")
.unwrap()
.to_string(),
language: key_values.get("Language").unwrap().to_string(),
plural_rules: CatalogPluralRules::parse(key_values.get("Plural-Forms").unwrap())?,

Example: parsing this basic file generated with xgettext with default options
Notice that it doesn't contains Plural-Forms

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-06-09 17:34+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"

#: main.cpp:28
msgid "item"
msgstr ""

And parsing this files with polib::po_file::parse will panics with

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/polib-0.2.0/src/metadata.rs:81:84

It should use unwrap_or_default or something like that instead

Ideally, the MetaData should also keep the metadata as is so i can add messages to a .pot file without changing most metadata.

@BrettDong
Copy link
Owner

Thank you for reporting the issue. When I wrote the library I was assuming input data is a .po file of translation data for a particular language, so Plural-Forms is always present in the metadata. Some code changes in polib is necessary to accommodate .pot files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants