use crate::error::{MetaError, Result};
const TITLE: &str = "title";
const LANGUAGE: &str = "lang";
const ID: &str = "id";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Count {
One,
OneOrMore,
Optional,
Any,
}
impl Count {
fn of(key: &str) -> Self {
match key {
TITLE | ID => Self::One,
LANGUAGE => Self::OneOrMore,
"series" | "series-index" | "publisher" | "date" | "annotation" | "cover" => {
Self::Optional
}
_ => Self::Any,
}
}
fn is_required(self) -> bool {
matches!(self, Self::One | Self::OneOrMore)
}
fn is_single(self) -> bool {
matches!(self, Self::One | Self::Optional)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Meta {
rows: Vec<(String, String)>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Series<'a> {
pub name: &'a str,
pub index: Option<&'a str>,
}
impl Meta {
#[cfg(any(feature = "write", test))]
pub(crate) fn new(title: String, language: String, id: String) -> Result<Self, MetaError> {
if title.is_empty() {
return Err(MetaError::EmptyTitle);
}
Ok(Self {
rows: vec![
(TITLE.to_owned(), title),
(LANGUAGE.to_owned(), language),
(ID.to_owned(), id),
],
})
}
#[cfg(any(feature = "read", test))]
pub(crate) fn from_rows(rows: Vec<(String, String)>) -> Result<Self, MetaError> {
let meta = Self { rows };
for (key, _) in &meta.rows {
check_key(key)?;
if Count::of(key).is_single() && meta.get_all(key).nth(1).is_some() {
return Err(MetaError::SingleValued(key.clone()));
}
}
for key in [TITLE, LANGUAGE, ID] {
if meta.get(key).is_none() {
return Err(MetaError::Required(key.to_owned()));
}
}
if meta.title().is_empty() {
return Err(MetaError::EmptyTitle);
}
Ok(meta)
}
#[must_use]
pub fn title(&self) -> &str {
self.get(TITLE).unwrap_or_default()
}
#[must_use]
pub fn language(&self) -> &str {
self.get(LANGUAGE).unwrap_or_default()
}
pub fn languages(&self) -> impl Iterator<Item = &str> {
self.get_all(LANGUAGE)
}
#[must_use]
pub fn id(&self) -> &str {
self.get(ID).unwrap_or_default()
}
pub fn authors(&self) -> impl Iterator<Item = &str> {
self.get_all("author")
}
pub fn translators(&self) -> impl Iterator<Item = &str> {
self.get_all("translator")
}
#[must_use]
pub fn series(&self) -> Option<Series<'_>> {
self.get("series").map(|name| Series {
name,
index: self.get("series-index"),
})
}
#[must_use]
pub fn publisher(&self) -> Option<&str> {
self.get("publisher")
}
#[must_use]
pub fn date(&self) -> Option<&str> {
self.get("date")
}
pub fn isbns(&self) -> impl Iterator<Item = &str> {
self.get_all("isbn")
}
pub fn genres(&self) -> impl Iterator<Item = &str> {
self.get_all("genre")
}
#[must_use]
pub fn annotation(&self) -> Option<&str> {
self.get("annotation")
}
#[must_use]
pub fn cover(&self) -> Option<&str> {
self.get("cover")
}
#[must_use]
pub fn get(&self, key: &str) -> Option<&str> {
self.get_all(key).next()
}
pub fn get_all(&self, key: &str) -> impl Iterator<Item = &str> {
self.rows
.iter()
.filter(move |(row_key, _)| row_key == key)
.map(|(_, value)| value.as_str())
}
pub fn iter(&self) -> impl Iterator<Item = (&str, &str)> {
self.rows
.iter()
.map(|(key, value)| (key.as_str(), value.as_str()))
}
pub fn set_title(&mut self, title: impl Into<String>) -> Result<()> {
self.set(TITLE, title)
}
pub fn set_language(&mut self, tag: impl Into<String>) -> Result<()> {
self.set(LANGUAGE, tag)
}
pub fn set_id(&mut self, id: impl Into<String>) -> Result<()> {
self.set(ID, id)
}
pub fn add_author(&mut self, name: impl Into<String>) -> Result<()> {
self.add("author", name)
}
pub fn add_translator(&mut self, name: impl Into<String>) -> Result<()> {
self.add("translator", name)
}
pub fn set_series(&mut self, name: impl Into<String>, index: Option<&str>) -> Result<()> {
self.set("series", name)?;
match index {
Some(index) => self.set("series-index", index),
None => self.remove("series-index"),
}
}
pub fn set_publisher(&mut self, name: impl Into<String>) -> Result<()> {
self.set("publisher", name)
}
pub fn set_date(&mut self, date: impl Into<String>) -> Result<()> {
self.set("date", date)
}
pub fn add_isbn(&mut self, isbn: impl Into<String>) -> Result<()> {
self.add("isbn", isbn)
}
pub fn add_genre(&mut self, genre: impl Into<String>) -> Result<()> {
self.add("genre", genre)
}
pub fn set_annotation(&mut self, text: impl Into<String>) -> Result<()> {
self.set("annotation", text)
}
pub fn set_cover(&mut self, path: impl Into<String>) -> Result<()> {
self.set("cover", path)
}
pub fn set(&mut self, key: &str, value: impl Into<String>) -> Result<()> {
let value = checked(key, value.into())?;
match self.rows.iter().position(|(row_key, _)| row_key == key) {
Some(first) => {
self.rows[first].1 = value;
let mut seen = 0;
self.rows.retain(|(row_key, _)| {
seen += usize::from(row_key == key);
row_key != key || seen == 1
});
}
None => self.rows.push((key.to_owned(), value)),
}
Ok(())
}
pub fn add(&mut self, key: &str, value: impl Into<String>) -> Result<()> {
let value = checked(key, value.into())?;
if Count::of(key).is_single() && self.get(key).is_some() {
return Err(MetaError::SingleValued(key.to_owned()).into());
}
self.rows.push((key.to_owned(), value));
Ok(())
}
pub fn remove(&mut self, key: &str) -> Result<()> {
if Count::of(key).is_required() {
return Err(MetaError::Required(key.to_owned()).into());
}
self.rows.retain(|(row_key, _)| row_key != key);
Ok(())
}
}
fn checked(key: &str, value: String) -> Result<String, MetaError> {
check_key(key)?;
if key == TITLE && value.is_empty() {
return Err(MetaError::EmptyTitle);
}
Ok(value)
}
pub(crate) fn is_key(text: &str) -> bool {
text.split('-').all(|part| {
!part.is_empty()
&& part
.bytes()
.all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit())
})
}
fn check_key(key: &str) -> Result<(), MetaError> {
if is_key(key) {
Ok(())
} else {
Err(MetaError::InvalidKey(key.to_owned()))
}
}
#[cfg(test)]
mod tests {
use super::Meta;
use crate::error::{Error, MetaError};
fn meta() -> Meta {
Meta::new("The Example".into(), "en".into(), "urn:uuid:0".into()).unwrap()
}
fn rows(rows: &[(&str, &str)]) -> Vec<(String, String)> {
rows.iter()
.map(|(key, value)| ((*key).to_owned(), (*value).to_owned()))
.collect()
}
fn meta_error(result: crate::Result<()>) -> MetaError {
match result {
Err(Error::InvalidMeta(error)) => error,
other => panic!("expected a metadata error, got {other:?}"),
}
}
#[test]
fn new_meta_has_the_three_required_keys() {
let meta = meta();
assert_eq!(
(meta.title(), meta.language(), meta.id()),
("The Example", "en", "urn:uuid:0")
);
}
#[test]
fn new_rejects_an_empty_title() {
let result = Meta::new(String::new(), "en".into(), "id".into());
assert_eq!(result, Err(MetaError::EmptyTitle));
}
#[test]
fn add_keeps_the_order_of_authors() {
let mut meta = meta();
meta.add_author("A. Writer").unwrap();
meta.add_author("B. Editor").unwrap();
assert_eq!(
meta.authors().collect::<Vec<_>>(),
["A. Writer", "B. Editor"]
);
}
#[test]
fn set_replaces_every_row_of_the_key_with_one() {
let mut meta = meta();
meta.add("lang", "fr").unwrap();
meta.set_language("de").unwrap();
assert_eq!(meta.languages().collect::<Vec<_>>(), ["de"]);
}
#[test]
fn set_keeps_the_position_of_the_first_row() {
let mut meta = meta();
meta.set_title("Renamed").unwrap();
assert_eq!(meta.iter().next(), Some(("title", "Renamed")));
}
#[test]
fn set_rejects_an_empty_title() {
assert_eq!(meta_error(meta().set_title("")), MetaError::EmptyTitle);
}
#[test]
fn add_rejects_a_second_value_for_a_single_key() {
let mut meta = meta();
meta.set_publisher("First").unwrap();
let error = meta_error(meta.add("publisher", "Second"));
assert_eq!(error, MetaError::SingleValued("publisher".into()));
}
#[test]
fn remove_rejects_a_required_key() {
let error = meta_error(meta().remove("id"));
assert_eq!(error, MetaError::Required("id".into()));
}
#[test]
fn remove_succeeds_for_an_absent_key() {
assert!(meta().remove("publisher").is_ok());
}
#[test]
fn set_series_without_an_index_removes_the_stored_index() {
let mut meta = meta();
meta.set_series("Saga", Some("2")).unwrap();
meta.set_series("Saga", None).unwrap();
assert_eq!(meta.series().and_then(|series| series.index), None);
}
#[test]
fn unknown_keys_may_repeat() {
let mut meta = meta();
meta.add("x-shelf", "a").unwrap();
meta.add("x-shelf", "b").unwrap();
assert_eq!(meta.get_all("x-shelf").count(), 2);
}
#[test]
fn set_rejects_a_key_with_an_uppercase_letter() {
let error = meta_error(meta().set("Title", "x"));
assert_eq!(error, MetaError::InvalidKey("Title".into()));
}
#[test]
fn set_rejects_a_key_with_an_empty_part() {
let error = meta_error(meta().set("x--shelf", "x"));
assert_eq!(error, MetaError::InvalidKey("x--shelf".into()));
}
#[test]
fn from_rows_accepts_a_complete_table() {
let table = rows(&[("title", "T"), ("lang", "en"), ("id", "i"), ("author", "A")]);
assert!(Meta::from_rows(table).is_ok());
}
#[test]
fn from_rows_rejects_a_missing_identifier() {
let table = rows(&[("title", "T"), ("lang", "en")]);
assert_eq!(
Meta::from_rows(table),
Err(MetaError::Required("id".into()))
);
}
#[test]
fn from_rows_rejects_two_titles() {
let table = rows(&[("title", "T"), ("title", "U"), ("lang", "en"), ("id", "i")]);
assert_eq!(
Meta::from_rows(table),
Err(MetaError::SingleValued("title".into()))
);
}
#[test]
fn from_rows_rejects_an_empty_title() {
let table = rows(&[("title", ""), ("lang", "en"), ("id", "i")]);
assert_eq!(Meta::from_rows(table), Err(MetaError::EmptyTitle));
}
}