gen-bsky 0.1.23

A Library to generate and post a bluesky post
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
use std::{
    cmp::max,
    fs::File,
    io::{BufRead, BufReader},
    path::PathBuf,
};

use serde::{Deserialize, Serialize};
use thiserror::Error;
use toml::value::Datetime;
use unicode_segmentation::UnicodeSegmentation;

mod bluesky;
mod extra;
mod tags;
mod taxonomies;

use bluesky::Bluesky;
use extra::Extra;
pub(crate) use taxonomies::Taxonomies;

// +++
// title = "Blue Sky Test Blog"
// description = "A blog post to test the processing of blog posts for posting
// to Bluesky."
// date = 2025-01-17
// updated = 2025-01-16
// draft = false
//
// [taxonomies]
// topic = ["Technology"]
// description = "A blog post to test the processing of blog posts for posting
// to Bluesky."
// tags = ["bluesky", "testing", "test only", "ci"]
//
// [extra]
// bluesky.description = "This is a test blog post for Bluesky."
// bluesky.tags = ["bluesky", "testing", "test only", "ci"]
// +++
//

/// Error enum for FrontMatter type
#[non_exhaustive]
#[derive(Error, Debug)]
pub(super) enum FrontMatterError {
    /// Draft posts not allowed
    #[error("processing of draft posts is not allowed")]
    DraftNotAllowed,

    /// Post too old
    #[error("Post is older than allowed by minimum date setting {0}")]
    PostTooOld(Datetime),

    /// Error reported by IO library
    #[error("io error says: {0:?}")]
    Io(#[from] std::io::Error),

    /// Error reported by the Toml library.
    #[error("toml deserialization error says: {0:?}")]
    Toml(#[from] toml::de::Error),
}

/// Type representing the expected and optional keys in the
/// frontmatter of a markdown blog post file.
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub(crate) struct FrontMatter {
    /// The title for the blog post.
    title: String,
    /// A description of the blog post.
    pub(super) description: String,
    /// The creation date of the blog post.
    pub(super) date: Option<Datetime>,
    /// The updated data of the blog post.
    pub(super) updated: Option<Datetime>,
    /// Flag to indicate if the post is draft.
    #[serde(default)]
    pub(super) draft: bool,
    /// The taxonomies section in the front matter. Expected
    /// to contain tags.
    pub(super) taxonomies: Option<Taxonomies>,
    /// The extras section in the front matter. Containing
    /// custom keys and may contain the bluesky custom keys.
    pub(super) extra: Option<Extra>,
    /// The bluesky section in the front matter. May contain
    /// the bluesky custom keys.
    pub(super) bluesky: Option<Bluesky>,
}

/// Report values in private fields
impl FrontMatter {
    pub(super) fn title(&self) -> &str {
        self.title.as_str()
    }
}

/// Create new file and set advanced values
#[cfg(test)]
impl FrontMatter {
    pub(crate) fn new(title: &str, description: &str) -> Self {
        FrontMatter {
            title: title.to_string(),
            description: description.to_string(),
            date: None,
            updated: None,
            draft: false,
            taxonomies: None,
            extra: None,
            bluesky: None,
        }
    }
}

impl FrontMatter {
    pub(super) fn read(
        blog_file: &PathBuf,
        min_date: Datetime,
        allow_draft: bool,
    ) -> Result<FrontMatter, FrontMatterError> {
        log::debug!("Reading front matter from `{}` ", blog_file.display());
        let file = File::open(blog_file)?;
        let reader = BufReader::new(file);

        let mut front_str = String::new();
        let mut quit = false;

        for line in reader.lines().map_while(Result::ok) {
            if line.starts_with("+++") & quit {
                break;
            } else if line.starts_with("+++") {
                quit = true;
                continue;
            } else {
                front_str.push_str(&line);
                front_str.push('\n');
            }
        }

        #[cfg(test)]
        log::trace!("Front matter string:\n {front_str}");

        let front_matter = toml::from_str::<FrontMatter>(&front_str)?;

        if !allow_draft && front_matter.draft {
            #[cfg(test)]
            log::warn!("blog marked as draft and not allowed");
            return Err(FrontMatterError::DraftNotAllowed);
        }
        if front_matter.most_recent_date() < min_date {
            #[cfg(test)]
            log::warn!("blog post too old to process");
            return Err(FrontMatterError::PostTooOld(min_date));
        }

        #[cfg(test)]
        log::trace!("Front matter: {front_matter:#?}");

        Ok(front_matter)
    }

    pub(super) fn bluesky_created(&self) -> Option<toml::value::Datetime> {
        self.bluesky.as_ref().and_then(|b| b.created())
    }

    #[allow(dead_code)]
    pub(super) fn bluesky_published(&self) -> Option<toml::value::Datetime> {
        self.bluesky.as_ref().and_then(|b| b.published())
    }

    pub(super) fn bluesky_description(&self) -> &str {
        if let Some(bs) = self.bluesky.as_ref() {
            return bs.description();
        }

        if let Some(e) = self.extra.as_ref() {
            if let Some(bs) = e.bluesky() {
                return bs.description();
            }
        }

        &self.description
    }

    pub(super) fn bluesky_tags(&self) -> Vec<String> {
        if let Some(bs) = self.bluesky.as_ref() {
            return bs.hashtags();
        }

        if let Some(e) = self.extra.as_ref() {
            if let Some(bs) = e.bluesky() {
                return bs.hashtags();
            }
        }

        if let Some(t) = self.taxonomies.as_ref() {
            return t.hashtags();
        }

        Vec::new()
    }

    /// Return a toml formatted datetime representing the most
    /// recent date reported in the frontmatter.
    pub(super) fn most_recent_date(&self) -> Datetime {
        match (self.date.is_some(), self.updated.is_some()) {
            (false, false) => super::super::today(),
            (true, false) => self.date.unwrap(),
            (false, true) => self.updated.unwrap(),
            (true, true) => max(self.date.unwrap(), self.updated.unwrap()),
        }
    }

    pub(super) fn log_post_details(&self) {
        log::debug!(
            "Length of title: {} characters and {} graphemes",
            self.title.len(),
            self.title.graphemes(true).count()
        );
        log::debug!(
            "Length of description: {} characters and {} graphemes",
            self.description.len(),
            self.description.graphemes(true).count()
        );
        log::debug!(
            "Length of bluesky description: {} characters and {} graphemes",
            self.bluesky_description().len(),
            self.bluesky_description().graphemes(true).count()
        );
        log::debug!(
            "Length of tag contents: {} characters and {} graphemes",
            self.taxonomies
                .as_ref()
                .map_or(0, |e| e.tags().join("#").len() + 1),
            self.taxonomies
                .as_ref()
                .map_or(0, |e| e.tags().join("#").graphemes(true).count() + 1)
        );
        log::debug!(
            "Length of bluesky tag contents: {} characters and {} graphemes",
            {
                let tags = self.bluesky_tags();
                if tags.is_empty() {
                    0
                } else {
                    tags.join("#").len() + 1
                }
            },
            {
                let tags = self.bluesky_tags();
                if tags.is_empty() {
                    0
                } else {
                    tags.join("#").graphemes(true).count() + 1
                }
            }
        );
    }
}

#[cfg(test)]
mod tests {
    use chrono::{Datelike, Utc};
    use log::LevelFilter;

    use super::*;

    fn get_test_logger() {
        let mut builder = env_logger::Builder::new();
        builder.filter(None, LevelFilter::Debug);
        builder.format_timestamp_secs().format_module_path(false);
        let _ = builder.try_init();
    }

    #[test]
    fn test_from_toml_basic() {
        let toml = r#"
            title = "Test Title"
            description = "Test Description"

            [taxonomies]
            tags = ["rust", "testing"]
        "#;
        let fm = toml::from_str::<FrontMatter>(toml).unwrap();
        assert_eq!(fm.title(), "Test Title");
        assert_eq!(fm.description, "Test Description");
        assert_eq!(fm.taxonomies.unwrap().tags(), vec!["rust", "testing"]);
        assert!(fm.extra.is_none());
    }

    #[test]
    fn test_from_toml_with_extra() {
        get_test_logger();

        let toml = r#"
            title = "Extra Test"
            description = "Has extra field"

            [taxonomies]
            tags = ["extra"]

            [extra]
            bluesky.description = "extra_value"
        "#;
        let fm = toml::from_str::<FrontMatter>(toml).unwrap();
        assert_eq!(fm.title, "Extra Test");
        assert_eq!(fm.taxonomies.unwrap().tags(), vec!["extra"]);
        assert!(fm.extra.is_some());
        assert_eq!(
            fm.extra.unwrap().bluesky().unwrap().description(),
            "extra_value"
        );
    }

    #[test]
    fn test_from_toml_with_extra_bluesky() {
        get_test_logger();

        let toml = r#"
            title = "Extra Test"
            description = "Has extra field"

            [taxonomies]
            tags = ["extra"]

            [extra]

            [extra.bluesky]
            description = "extra_value"
            tags = ["extra_tag"]
        "#;
        let fm = toml::from_str::<FrontMatter>(toml).unwrap();
        assert_eq!(fm.title, "Extra Test");
        assert_eq!(fm.taxonomies.unwrap().tags(), vec!["extra"]);
        assert!(fm.extra.is_some());
        assert_eq!(
            fm.extra.as_ref().unwrap().bluesky().unwrap().description(),
            "extra_value"
        );
        assert_eq!(
            fm.extra.as_ref().unwrap().bluesky().unwrap().tags(),
            vec!["extra_tag".to_string()]
        );
    }

    #[test]
    fn test_from_toml_with_bluesky() {
        get_test_logger();

        let toml = r#"
            title = "Extra Test"
            description = "Has extra field"

            [taxonomies]
            tags = ["extra"]

            [bluesky]
            description = "extra_value"
            tags = ["extra_tag"]
        "#;
        let fm = toml::from_str::<FrontMatter>(toml).unwrap();
        assert_eq!(fm.title, "Extra Test");
        assert_eq!(fm.taxonomies.unwrap().tags(), vec!["extra"]);
        assert!(fm.bluesky.is_some());
        assert_eq!(fm.bluesky.as_ref().unwrap().description(), "extra_value");
        assert_eq!(
            fm.bluesky.as_ref().unwrap().tags(),
            vec!["extra_tag".to_string()]
        );
    }

    #[test]
    fn test_from_toml_missing_tags() {
        let toml = r#"
            title = "Missing Fields"
            description = "No taxonomies"
        "#;
        let fm = toml::from_str::<FrontMatter>(toml).unwrap();
        assert_eq!(fm.title, "Missing Fields");
        assert_eq!(fm.description, "No taxonomies");
        assert!(fm.taxonomies.is_none());
    }

    #[test]
    fn test_from_toml_bluesky_set_incorrectly() {
        get_test_logger();
        let toml = r#"
            title = "Overview of Our Workflow"
            description = "We will kick things off with a detailed overview"
            date = 2025-01-17
            updated = 2025-01-16
            draft = false
    
            [taxonomies]
            topic = ["Technology"]
            tags = ["devsecops", "software", "circleci", "security", "practices"]
    
            [extra]
            bluesky = "Covering the key steps involved."
            "#;

        let expected =
            r#"invalid type: string "Covering the key steps involved.", expected struct Bluesky"#;

        let fm_res = toml::from_str::<FrontMatter>(toml);
        assert!(fm_res.is_err());
        assert_eq!(fm_res.err().unwrap().message(), expected);
    }

    #[test]
    fn test_from_toml_invalid() {
        let toml = r#"
            title = 123
            description = "Invalid type"
        "#;
        let result = toml::from_str::<FrontMatter>(toml);
        assert!(result.is_err());
    }

    #[test]
    fn test_hashtags_formatting() {
        let taxonomies = Taxonomies::new(vec![
            "rust".to_string(),
            "blue sky".to_string(),
            "#AlreadyHashtag".to_string(),
            "multi word tag".to_string(),
            "".to_string(),
        ]);
        let hashtags = taxonomies.hashtags();
        assert_eq!(
            hashtags,
            vec!["#Rust", "#BlueSky", "#AlreadyHashtag", "#MultiWordTag", "#"]
        );
    }

    #[test]
    fn test_front_matter_empty_toml() {
        let toml = r#"
            title = "Extra Test"
            description = "Has extra field"
        "#;
        let fm = toml::from_str::<FrontMatter>(toml).unwrap();
        assert_eq!(fm.title, "Extra Test");
        assert_eq!(fm.description, "Has extra field");
        assert!(fm.taxonomies.is_none());
        assert!(fm.extra.is_none());
    }

    #[test]
    fn test_taxonomies_empty_tags() {
        let taxonomies = Taxonomies::new(vec![]);
        let hashtags = taxonomies.hashtags();
        assert_eq!(hashtags, Vec::<String>::new());
    }

    #[test]
    fn test_most_recent_date_no_dates() {
        let fm = FrontMatter {
            title: "Test".to_string(),
            description: "Test".to_string(),
            date: None,
            updated: None,
            ..Default::default()
        };
        let result = fm.most_recent_date();
        let now = Utc::now();

        let expected_date = Some(toml::value::Date {
            year: now.year() as u16,
            month: now.month() as u8,
            day: now.day() as u8,
        });
        assert_eq!(expected_date, result.date);
        assert!(result.time.is_none());
        assert!(result.offset.is_none());
    }

    #[test]
    fn test_most_recent_date_only_date() {
        let date = Datetime {
            date: Some(toml::value::Date {
                year: 2025,
                month: 1,
                day: 1,
            }),
            time: None,
            offset: None,
        };
        let fm = FrontMatter {
            title: "Test".to_string(),
            description: "Test".to_string(),
            date: Some(date),
            updated: None,
            ..Default::default()
        };
        let result = fm.most_recent_date();
        assert_eq!(result, date);
    }

    #[test]
    fn test_most_recent_date_only_updated() {
        let updated = Datetime {
            date: Some(toml::value::Date {
                year: 2025,
                month: 1,
                day: 2,
            }),
            time: None,
            offset: None,
        };
        let fm = FrontMatter {
            title: "Test".to_string(),
            description: "Test".to_string(),
            date: None,
            updated: Some(updated),
            ..Default::default()
        };
        let result = fm.most_recent_date();
        assert_eq!(result, updated);
    }

    #[test]
    fn test_most_recent_date_both_dates_updated_newer() {
        let date = Datetime {
            date: Some(toml::value::Date {
                year: 2025,
                month: 1,
                day: 1,
            }),
            time: None,
            offset: None,
        };
        let updated = Datetime {
            date: Some(toml::value::Date {
                year: 2025,
                month: 1,
                day: 2,
            }),
            time: None,
            offset: None,
        };
        let fm = FrontMatter {
            title: "Test".to_string(),
            description: "Test".to_string(),
            date: Some(date),
            updated: Some(updated),
            ..Default::default()
        };
        let result = fm.most_recent_date();
        assert_eq!(result, updated);
    }

    #[test]
    fn test_most_recent_date_both_dates_date_newer() {
        let date = Datetime {
            date: Some(toml::value::Date {
                year: 2025,
                month: 1,
                day: 2,
            }),
            time: None,
            offset: None,
        };
        let updated = Datetime {
            date: Some(toml::value::Date {
                year: 2025,
                month: 1,
                day: 1,
            }),
            time: None,
            offset: None,
        };
        let fm = FrontMatter {
            title: "Test".to_string(),
            description: "Test".to_string(),
            date: Some(date),
            updated: Some(updated),
            ..Default::default()
        };
        let result = fm.most_recent_date();
        assert_eq!(result, date);
    }

    #[test]
    fn test_date_from_toml_basic() {
        let toml = r#"
            title = "Date Test"
            description = "Basic date test"
            date = 2025-01-17
        "#;
        let fm = toml::from_str::<FrontMatter>(toml).unwrap();
        assert!(fm.date.is_some());
        let date = fm.date.unwrap();
        assert_eq!(date.date.unwrap().year, 2025);
        assert_eq!(date.date.unwrap().month, 1);
        assert_eq!(date.date.unwrap().day, 17);
        assert!(date.time.is_none());
        assert!(date.offset.is_none());
    }

    #[test]
    fn test_date_from_toml_with_time() {
        let toml = r#"
            title = "DateTime Test"
            description = "Date with time test"
            date = 2025-01-17T15:30:00Z
        "#;
        let fm = toml::from_str::<FrontMatter>(toml).unwrap();
        assert!(fm.date.is_some());
        let date = fm.date.unwrap();
        assert_eq!(date.date.unwrap().year, 2025);
        assert_eq!(date.date.unwrap().month, 1);
        assert_eq!(date.date.unwrap().day, 17);
        assert!(date.time.is_some());
        assert_eq!(date.time.unwrap().hour, 15);
        assert_eq!(date.time.unwrap().minute, 30);
        assert_eq!(date.time.unwrap().second, Some(0));
        assert!(date.offset.is_some());
    }

    #[test]
    fn test_date_from_toml_with_timezone() {
        let toml = r#"
            title = "Timezone Test"
            description = "Date with timezone test"
            date = 2025-01-17T15:30:00+02:00
        "#;
        let fm = toml::from_str::<FrontMatter>(toml).unwrap();
        assert!(fm.date.is_some());
        let date = fm.date.unwrap();
        assert_eq!(date.date.unwrap().year, 2025);
        assert_eq!(date.date.unwrap().month, 1);
        assert_eq!(date.date.unwrap().day, 17);
        assert!(date.time.is_some());
        assert_eq!(date.time.unwrap().hour, 15);
        assert_eq!(date.time.unwrap().minute, 30);
        assert!(date.offset.is_some());
        assert_eq!(
            date.offset.unwrap(),
            toml::value::Offset::Custom { minutes: 120 }
        );
    }

    #[test]
    fn test_invalid_date_format() {
        let toml = r#"
            title = "Invalid Date"
            description = "Invalid date format test"
            date = "not-a-date"
        "#;
        let result = toml::from_str::<FrontMatter>(toml);
        assert!(result.is_err());
    }

    #[test]
    fn test_date_comparison() {
        get_test_logger();

        let toml = r#"
            title = "Date Comparison"
            description = "Testing date comparison"
            date = 2025-01-17T15:30:00Z
            updated = 2025-01-18T15:30:00Z
        "#;
        let fm = toml::from_str::<FrontMatter>(toml).unwrap();
        assert!(fm.date.is_some());
        assert!(fm.updated.is_some());
        let most_recent = fm.most_recent_date();
        assert_eq!(most_recent.date.unwrap().day, 18);
    }

    #[test]
    fn test_date_with_microseconds() {
        let toml = r#"
            title = "Microseconds Test"
            description = "Date with microseconds test"
            date = 2025-01-17T15:30:00.123456Z
        "#;
        let fm = toml::from_str::<FrontMatter>(toml).unwrap();
        assert!(fm.date.is_some());
        let date = fm.date.unwrap();
        assert_eq!(date.date.unwrap().year, 2025);
        assert_eq!(date.time.unwrap().nanosecond, Some(123456000));
    }
}