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

Cloudinary gallery refactoring #34

Merged
merged 5 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/nodes/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::Serialize;

#[derive(Debug, PartialEq, Serialize, Default, Clone)]
pub struct Metadata {
pub header: Option<String>,
pub title: Option<String>,
pub timestamp: Option<DateTime<FixedOffset>>,
pub image: Option<String>,
pub preview: Option<String>,
Expand All @@ -15,14 +15,14 @@ pub struct Metadata {

impl Metadata {
pub fn new<S: Into<String>>(
header: Option<S>,
title: Option<S>,
timestamp: Option<DateTime<FixedOffset>>,
image: Option<S>,
preview: Option<S>,
tags: Option<Vec<String>>,
) -> Self {
Self {
header: header.map(|h| h.into()),
title: title.map(|h| h.into()),
timestamp,
image: image.map(|i| i.into()),
preview: preview.map(|p| p.into()),
Expand All @@ -39,9 +39,9 @@ impl Display for Metadata {
write!(
f,
"{}{}{}{}{}^^^\n\n",
self.header
self.title
.as_ref()
.map_or("".to_string(), |h| format!("header: {h}\n")),
.map_or("".to_string(), |h| format!("title: {h}\n")),
self.timestamp
.as_ref()
.map_or("".to_string(), |t| format!("timestamp: {t}\n")),
Expand All @@ -62,7 +62,7 @@ impl Display for Metadata {

impl Node for Metadata {
fn len(&self) -> usize {
let len = self.header.as_ref().map_or(0, |h| h.len() + 9)
let len = self.title.as_ref().map_or(0, |h| h.len() + 8)
+ self
.timestamp
.as_ref()
Expand Down Expand Up @@ -94,8 +94,8 @@ impl Deserializer for Metadata {
if let Some(metadata) = matcher.get_match("", "^^^\n\n", false) {
let mut meta = Self::new::<&str>(None, None, None, None, None);
metadata.body.split('\n').for_each(|line| {
if line.starts_with("header: ") {
meta.header = Some(line.replace("header: ", ""));
if line.starts_with("title: ") {
meta.title = Some(line.replace("title: ", ""));
} else if line.starts_with("timestamp: ") {
// remove this when %:z works as expected
if line.trim().chars().rev().nth(5) == Some('+') {
Expand Down Expand Up @@ -143,14 +143,14 @@ mod tests {
);
assert_eq!(
metadata.to_string(),
"header: header\ntimestamp: 2022-01-01 00:00:00 +02:00\nimage: image\npreview: preview\ntags: tag1, tag2\n^^^\n\n"
"title: header\ntimestamp: 2022-01-01 00:00:00 +02:00\nimage: image\npreview: preview\ntags: tag1, tag2\n^^^\n\n"
);
}

#[test]
fn test_len() {
let metadata = Metadata::new(
Some("header"),
Some("title"),
Some(
DateTime::parse_from_str("2022-01-01 00:00:00 +02:00", "%Y-%m-%d %H:%M:%S %z")
.unwrap(),
Expand All @@ -165,7 +165,7 @@ mod tests {
#[test]
fn len_with_one_tag() {
let metadata = Metadata::new(
Some("header"),
Some("title"),
Some(
DateTime::parse_from_str("2022-01-01 00:00:00 +02:00", "%Y-%m-%d %H:%M:%S %z")
.unwrap(),
Expand All @@ -180,7 +180,7 @@ mod tests {
#[test]
fn test_deserialize() {
let metadata = Metadata::new(
Some("header"),
Some("title"),
Some(
DateTime::parse_from_str("2022-01-01 00:00:00 +02:00", "%Y-%m-%d %H:%M:%S %z")
.unwrap(),
Expand Down Expand Up @@ -209,9 +209,9 @@ mod tests {
}

#[test]
fn deserialize_only_with_header() {
fn deserialize_only_with_title() {
assert_eq!(
Metadata::deserialize("header: header\n^^^\n\n"),
Metadata::deserialize("title: header\n^^^\n\n"),
Some(Metadata::new(Some("header"), None, None, None, None))
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/yamd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ mod tests {
};
use chrono::DateTime;
use pretty_assertions::assert_eq;
const TEST_CASE: &str = r#"header: test
const TEST_CASE: &str = r#"title: test
timestamp: 2022-01-01 00:00:00 +02:00
image: image
preview: preview
Expand Down
Loading