Lines
100 %
Functions
90 %
Branches
//! What the edit-form loaders share: the refusals for stored tags a form
//! cannot hold, and the `tags` rows.
use std::collections::HashSet;
use finance::tag::Tag;
use forms::Row;
use server::logical::ensure_single_line_names;
use super::Failure;
pub(super) fn refused(subject: &str, reason: &str) -> Failure {
Failure::Refused(format!(
"the {subject} cannot be edited as a form: {reason}"
))
}
/// A line break in a name or value, or two values of one name.
pub(super) fn ensure_representable(subject: &str, tags: &[Tag]) -> Result<(), Failure> {
ensure_single_line_names(tags).map_err(|reason| refused(subject, &reason))?;
let mut seen = HashSet::new();
match tags.iter().find(|tag| !seen.insert(tag.normalized().0)) {
Some(tag) => Err(refused(
subject,
&format!(
"the {subject} carries two values of the tag '{}'",
tag.normalized().0
),
)),
None => Ok(()),
pub(super) fn tag_row(tag: &Tag) -> Row {
let mut row = Row::from([
("name".to_string(), tag.normalized().0.to_string()),
("value".to_string(), tag.normalized().1.to_string()),
]);
if let Some(description) = &tag.description {
row.insert("description".to_string(), description.clone());
row