egui_charts/model/post.rs
1//! Posts and ideas that post and idea drawings link to.
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6/// The content of a post or idea, as the host resolves a link to it.
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
8#[non_exhaustive]
9pub struct PostContent {
10 /// The title of an idea; empty for a post, whose card then leads with the author.
11 #[serde(default)]
12 pub title: String,
13 /// Who published it.
14 pub author: String,
15 /// When it was published.
16 pub time: DateTime<Utc>,
17 /// The text.
18 pub text: String,
19 /// The address of its picture, such as an idea's chart image, loaded through the host's egui
20 /// image loaders as an image drawing's address is; `None` without one. The chart loads it
21 /// through those loaders only, so answer with addresses they should fetch.
22 #[serde(default)]
23 pub picture: Option<String>,
24}
25
26impl PostContent {
27 /// A post by `author`, published at `time`, with `text` and no title or picture.
28 pub fn new(author: impl Into<String>, time: DateTime<Utc>, text: impl Into<String>) -> Self {
29 Self {
30 title: String::new(),
31 author: author.into(),
32 time,
33 text: text.into(),
34 picture: None,
35 }
36 }
37}
38
39/// The post or idea a drawing links to, and its content once the host resolved it.
40///
41/// A drawing saves both, so a saved layout shows the content without asking the host again.
42#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
43#[non_exhaustive]
44pub struct PostLink {
45 /// The address of the post or idea, or the host's reference to it, such as a feed entry
46 /// identifier.
47 pub link: String,
48 /// What the host resolved the link to, or `None` while it has not.
49 #[serde(default)]
50 pub content: Option<PostContent>,
51 /// The host could not resolve the link, or the chart has no service to ask. Not saved, so
52 /// the link is asked for again when the drawing is loaded.
53 #[serde(skip)]
54 pub(crate) failed: bool,
55}
56
57impl PostLink {
58 /// `link`, waiting for its content.
59 pub fn new(link: impl Into<String>) -> Self {
60 Self {
61 link: link.into(),
62 content: None,
63 failed: false,
64 }
65 }
66
67 /// `link` with the content it resolves to, which the chart then does not ask for.
68 pub fn resolved(link: impl Into<String>, content: PostContent) -> Self {
69 Self {
70 link: link.into(),
71 content: Some(content),
72 failed: false,
73 }
74 }
75}