wallabag_api/types/
patch_entry.rs

1// Copyright 2018 Samuel Walladge <samuel@swalladge.net>
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4use chrono::{DateTime, Utc};
5
6use serde::{Deserialize, Serialize};
7
8use super::Entry;
9use crate::utils::serde::bool_to_int;
10
11/// A struct representing an entry to be changed. Fields here are the only fields that can be
12/// modified directly via the api.
13///
14/// Setting a field to `None` causes the field to not be modified.
15#[derive(Deserialize, Serialize, Debug)]
16pub struct PatchEntry {
17    pub title: Option<String>,
18
19    /// List of tag labels as strings. Commas in tag labels are valid but discouraged.
20    pub tags: Option<Vec<String>>,
21
22    #[serde(serialize_with = "bool_to_int")]
23    pub archive: Option<bool>,
24    #[serde(serialize_with = "bool_to_int")]
25    pub starred: Option<bool>,
26    #[serde(serialize_with = "bool_to_int")]
27    pub public: Option<bool>,
28
29    pub content: Option<String>,
30    pub language: Option<String>,
31    pub preview_picture: Option<String>,
32    pub published_at: Option<DateTime<Utc>>,
33
34    /// Formatted as "name 1, name 2"
35    pub authors: Option<String>,
36
37    pub origin_url: Option<String>,
38}
39
40/// Use this as a convenience. This allows doing something like the following instead of needing to
41/// explicitly setting each ignored value to `None`:
42/// ```
43/// # use wallabag_api::types::PatchEntry;
44/// let archive_it = PatchEntry { archive: Some(true), .. Default::default() };
45/// ```
46impl Default for PatchEntry {
47    fn default() -> Self {
48        Self {
49            title: None,
50            tags: None,
51            archive: None,
52            starred: None,
53            public: None,
54            content: None,
55            language: None,
56            preview_picture: None,
57            published_at: None,
58            authors: None,
59            origin_url: None,
60        }
61    }
62}
63
64/// Convert an Entry to a set of changes ready for sending to the api.
65impl From<&Entry> for PatchEntry {
66    fn from(entry: &Entry) -> Self {
67        let tags: Vec<String> = entry.tags.iter().map(|t| t.label.clone()).collect();
68        Self {
69            title: entry.title.clone(),
70            tags: Some(tags),
71            archive: Some(entry.is_archived),
72            starred: Some(entry.is_starred),
73            public: Some(entry.is_public),
74            content: entry.content.clone(),
75            language: entry.language.clone(),
76            preview_picture: entry.preview_picture.clone(),
77            published_at: entry.published_at,
78            authors: None,
79            origin_url: entry.origin_url.clone(),
80        }
81    }
82}