shared/domain/
asset.rs

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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
//! Types for Assets, Jig and LearningPath.

use std::{
    collections::HashMap,
    fmt::{self, Debug},
    str::FromStr,
};

use anyhow::anyhow;
use chrono::{DateTime, Utc};
// use dyn_clone::DynClone;
use serde::{Deserialize, Serialize};
use strum_macros::Display;
use uuid::Uuid;

use crate::{
    api::endpoints::PathPart,
    domain::{
        category::CategoryId,
        meta::{AffiliationId, AgeRangeId},
        module::LiteModule,
    },
};

use super::{
    additional_resource::AdditionalResource,
    course::{CourseId, CourseResponse},
    jig::{JigId, JigResponse},
    module::body::ThemeId,
    playlist::{PlaylistId, PlaylistResponse},
    resource::{ResourceId, ResourceResponse},
    user::UserId,
};

/// AssetType
#[derive(Copy, Clone, Eq, PartialEq, Serialize, Deserialize, Debug, Display)]
#[serde(rename_all = "kebab-case")]
#[strum(serialize_all = "kebab-case")]
pub enum AssetType {
    /// JIG
    Jig,

    /// Resource
    Resource,

    /// Playlist
    Playlist,

    /// Course
    Course,
}

impl AssetType {
    /// check if jig
    pub fn is_jig(&self) -> bool {
        matches!(self, Self::Jig)
    }

    /// check if resource
    pub fn is_resource(&self) -> bool {
        matches!(self, Self::Resource)
    }

    /// check if playlist
    pub fn is_playlist(&self) -> bool {
        matches!(self, Self::Playlist)
    }

    /// check if course
    pub fn is_course(&self) -> bool {
        matches!(self, Self::Course)
    }

    /// Represents the asset type as a `str`
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Jig => "jig",
            Self::Resource => "resource",
            Self::Playlist => "playlist",
            Self::Course => "course",
        }
    }

    /// asset type display name
    pub fn display_name(&self) -> &'static str {
        match self {
            Self::Jig => "JIG",
            Self::Resource => "resource",
            Self::Playlist => "playlist",
            Self::Course => "course",
        }
    }

    /// asset type display name capitalized
    pub fn display_name_capitalized(&self) -> &'static str {
        match self {
            Self::Jig => "JIG",
            Self::Resource => "Resource",
            Self::Playlist => "Playlist",
            Self::Course => "course",
        }
    }

    /// return to gallery button on sidebar
    pub fn sidebar_header(&self) -> &'static str {
        match self {
            Self::Jig => "JIG",
            Self::Resource => "resource",
            Self::Playlist => "playlist",
            Self::Course => "course",
        }
    }

    /// Create asset id from self and uuid
    pub fn to_asset_id(&self, uuid: Uuid) -> AssetId {
        match self {
            AssetType::Jig => JigId(uuid).into(),
            AssetType::Playlist => PlaylistId(uuid).into(),
            AssetType::Resource => ResourceId(uuid).into(),
            AssetType::Course => CourseId(uuid).into(),
        }
    }
}

impl From<&AssetId> for AssetType {
    fn from(asset_id: &AssetId) -> Self {
        match asset_id {
            AssetId::JigId(_) => AssetType::Jig,
            AssetId::PlaylistId(_) => AssetType::Playlist,
            AssetId::ResourceId(_) => AssetType::Resource,
            AssetId::CourseId(_) => AssetType::Course,
        }
    }
}

impl TryFrom<&str> for AssetType {
    type Error = ();

    fn try_from(s: &str) -> Result<Self, Self::Error> {
        match s {
            "jig" => Ok(Self::Jig),
            "playlist" => Ok(Self::Playlist),
            "resource" => Ok(Self::Resource),
            "course" => Ok(Self::Course),
            _ => Err(()),
        }
    }
}

impl PathPart for AssetType {
    fn get_path_string(&self) -> String {
        self.as_str().to_string()
    }
}

/// AssetId
#[derive(Copy, Clone, Eq, PartialEq, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub enum AssetId {
    /// JIG ID
    JigId(JigId),

    /// Playlist ID
    PlaylistId(PlaylistId),

    /// Resource ID
    ResourceId(ResourceId),

    /// Course ID
    CourseId(CourseId),
}

impl From<JigId> for AssetId {
    fn from(jig_id: JigId) -> Self {
        Self::JigId(jig_id)
    }
}

impl From<PlaylistId> for AssetId {
    fn from(playlist_id: PlaylistId) -> Self {
        Self::PlaylistId(playlist_id)
    }
}

impl From<ResourceId> for AssetId {
    fn from(resource_id: ResourceId) -> Self {
        Self::ResourceId(resource_id)
    }
}

impl From<CourseId> for AssetId {
    fn from(course_id: CourseId) -> Self {
        Self::CourseId(course_id)
    }
}

impl AssetId {
    /// get asset type
    pub fn asset_type(&self) -> AssetType {
        self.into()
    }

    /// get jig id value as ref
    pub fn unwrap_jig(&self) -> &JigId {
        match self {
            Self::JigId(jig_id) => jig_id,
            _ => panic!(),
        }
    }

    /// get playlist id value as ref
    pub fn unwrap_playlist(&self) -> &PlaylistId {
        match self {
            Self::PlaylistId(playlist_id) => playlist_id,
            _ => panic!(),
        }
    }

    /// get resource id value as ref
    pub fn unwrap_resource(&self) -> &ResourceId {
        match self {
            Self::ResourceId(resource_id) => resource_id,
            _ => panic!(),
        }
    }

    /// get course id value as ref
    pub fn unwrap_course(&self) -> &CourseId {
        match self {
            Self::CourseId(course_id) => course_id,
            _ => panic!(),
        }
    }

    /// get the id uuid
    pub fn uuid(&self) -> &Uuid {
        match self {
            Self::JigId(jig_id) => &jig_id.0,
            Self::PlaylistId(playlist_id) => &playlist_id.0,
            Self::ResourceId(resource_id) => &resource_id.0,
            Self::CourseId(course_id) => &course_id.0,
        }
    }

    /// check if jig
    pub fn is_jig_id(&self) -> bool {
        matches!(self, Self::JigId(_))
    }

    /// check if playlist
    pub fn is_playlist_id(&self) -> bool {
        matches!(self, Self::PlaylistId(_))
    }

    /// check if resource
    pub fn is_resource_id(&self) -> bool {
        matches!(self, Self::ResourceId(_))
    }

    /// check if course
    pub fn is_course_id(&self) -> bool {
        matches!(self, Self::CourseId(_))
    }
}

/// Asset
#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub enum Asset {
    /// JIG ID associated with the module.
    Jig(JigResponse),

    /// Playlist ID associated with the module.
    Playlist(PlaylistResponse),

    /// Resource ID associated with the module.
    Resource(ResourceResponse),

    /// Course ID associated with the module.
    Course(CourseResponse),
}

impl From<JigResponse> for Asset {
    fn from(jig: JigResponse) -> Self {
        Self::Jig(jig)
    }
}

impl From<PlaylistResponse> for Asset {
    fn from(playlist: PlaylistResponse) -> Self {
        Self::Playlist(playlist)
    }
}

impl From<ResourceResponse> for Asset {
    fn from(resource: ResourceResponse) -> Self {
        Self::Resource(resource)
    }
}

impl From<CourseResponse> for Asset {
    fn from(course: CourseResponse) -> Self {
        Self::Course(course)
    }
}

impl Asset {
    /// get asset type
    pub fn asset_type(&self) -> AssetType {
        (&self.id()).into()
    }

    /// get jig value as ref
    pub fn unwrap_jig(&self) -> &JigResponse {
        match self {
            Self::Jig(jig) => jig,
            _ => panic!(),
        }
    }

    /// get resource value as ref
    pub fn unwrap_resource(&self) -> &ResourceResponse {
        match self {
            Self::Resource(resource) => resource,
            _ => panic!(),
        }
    }

    /// get playlist value as ref
    pub fn unwrap_playlist(&self) -> &PlaylistResponse {
        match self {
            Self::Playlist(playlist) => playlist,
            _ => panic!(),
        }
    }

    /// get course value as ref
    pub fn unwrap_course(&self) -> &CourseResponse {
        match self {
            Self::Course(course) => course,
            _ => panic!(),
        }
    }

    /// check if is jig
    pub fn is_jig(&self) -> bool {
        matches!(self, Self::Jig(_))
    }

    /// check if is playlist
    pub fn is_playlist(&self) -> bool {
        matches!(self, Self::Playlist(_))
    }

    /// check if is resource
    pub fn is_resource(&self) -> bool {
        matches!(self, Self::Resource(_))
    }

    /// check if is course
    pub fn is_course(&self) -> bool {
        matches!(self, Self::Course(_))
    }

    /// get id
    pub fn id(&self) -> AssetId {
        match self {
            Self::Jig(jig) => jig.id.into(),
            Self::Playlist(playlist) => playlist.id.into(),
            Self::Resource(resource) => resource.id.into(),
            Self::Course(course) => course.id.into(),
        }
    }

    /// get id
    pub fn published_at(&self) -> Option<DateTime<Utc>> {
        match self {
            Self::Jig(jig) => jig.published_at,
            Self::Playlist(playlist) => playlist.published_at,
            Self::Resource(resource) => resource.published_at,
            Self::Course(course) => course.published_at,
        }
    }

    /// get display_name
    pub fn display_name(&self) -> &String {
        match self {
            Self::Jig(jig) => &jig.jig_data.display_name,
            Self::Playlist(playlist) => &playlist.playlist_data.display_name,
            Self::Resource(resource) => &resource.resource_data.display_name,
            Self::Course(course) => &course.course_data.display_name,
        }
    }

    /// get language
    pub fn language(&self) -> &String {
        match self {
            Self::Jig(jig) => &jig.jig_data.language,
            Self::Playlist(playlist) => &playlist.playlist_data.language,
            Self::Resource(resource) => &resource.resource_data.language,
            Self::Course(course) => &course.course_data.language,
        }
    }

    /// get description
    pub fn description(&self) -> &String {
        match self {
            Self::Jig(jig) => &jig.jig_data.description,
            Self::Playlist(playlist) => &playlist.playlist_data.description,
            Self::Resource(resource) => &resource.resource_data.description,
            Self::Course(course) => &course.course_data.description,
        }
    }

    /// get cover
    pub fn cover(&self) -> Option<&LiteModule> {
        match self {
            Self::Jig(jig) => jig.jig_data.modules.first(),
            Self::Playlist(playlist) => playlist.playlist_data.cover.as_ref(),
            Self::Resource(resource) => resource.resource_data.cover.as_ref(),
            Self::Course(course) => course.course_data.cover.as_ref(),
        }
    }

    /// get privacy_level
    pub fn privacy_level(&self) -> &PrivacyLevel {
        match self {
            Self::Jig(jig) => &jig.jig_data.privacy_level,
            Self::Playlist(playlist) => &playlist.playlist_data.privacy_level,
            Self::Resource(resource) => &resource.resource_data.privacy_level,
            Self::Course(course) => &course.course_data.privacy_level,
        }
    }

    /// get other_keywords
    pub fn other_keywords(&self) -> &String {
        match self {
            Self::Jig(jig) => &jig.jig_data.other_keywords,
            Self::Playlist(playlist) => &playlist.playlist_data.other_keywords,
            Self::Resource(resource) => &resource.resource_data.other_keywords,
            Self::Course(course) => &course.course_data.other_keywords,
        }
    }

    /// get translated_keywords
    pub fn translated_keywords(&self) -> &String {
        match self {
            Self::Jig(jig) => &jig.jig_data.translated_keywords,
            Self::Playlist(playlist) => &playlist.playlist_data.translated_keywords,
            Self::Resource(resource) => &resource.resource_data.translated_keywords,
            Self::Course(course) => &course.course_data.translated_keywords,
        }
    }

    /// get age_ranges
    pub fn age_ranges(&self) -> &Vec<AgeRangeId> {
        match self {
            Self::Jig(jig) => &jig.jig_data.age_ranges,
            Self::Playlist(playlist) => &playlist.playlist_data.age_ranges,
            Self::Resource(resource) => &resource.resource_data.age_ranges,
            Self::Course(_) => unimplemented!(),
        }
    }

    /// get affiliations
    pub fn affiliations(&self) -> &Vec<AffiliationId> {
        match self {
            Self::Jig(jig) => &jig.jig_data.affiliations,
            Self::Playlist(playlist) => &playlist.playlist_data.affiliations,
            Self::Resource(resource) => &resource.resource_data.affiliations,
            Self::Course(_) => panic!(),
        }
    }

    /// get categories
    pub fn categories(&self) -> &Vec<CategoryId> {
        match self {
            Self::Jig(jig) => &jig.jig_data.categories,
            Self::Playlist(playlist) => &playlist.playlist_data.categories,
            Self::Resource(resource) => &resource.resource_data.categories,
            Self::Course(course) => &course.course_data.categories,
        }
    }

    /// get likes
    pub fn likes(&self) -> i64 {
        match self {
            Self::Jig(jig) => jig.likes,
            Self::Playlist(playlist) => playlist.likes,
            Self::Resource(resource) => resource.likes,
            Self::Course(course) => course.likes,
        }
    }

    /// is likes by current user
    pub fn is_liked(&self) -> bool {
        match self {
            Self::Jig(jig) => jig.is_liked,
            Self::Playlist(playlist) => playlist.is_liked,
            Self::Resource(resource) => resource.is_liked,
            Self::Course(_course) => todo!(),
        }
    }

    /// get plays
    pub fn plays(&self) -> i64 {
        match self {
            Self::Jig(jig) => jig.plays,
            Self::Playlist(playlist) => playlist.plays,
            Self::Resource(resource) => resource.views,
            Self::Course(course) => course.plays,
        }
    }

    /// get author_id
    pub fn author_id(&self) -> &Option<UserId> {
        match self {
            Self::Jig(jig) => &jig.author_id,
            Self::Playlist(playlist) => &playlist.author_id,
            Self::Resource(resource) => &resource.author_id,
            Self::Course(course) => &course.author_id,
        }
    }

    /// get author_name
    pub fn author_name(&self) -> &Option<String> {
        match self {
            Self::Jig(jig) => &jig.author_name,
            Self::Playlist(playlist) => &playlist.author_name,
            Self::Resource(resource) => &resource.author_name,
            Self::Course(course) => &course.author_name,
        }
    }

    /// get additional_resources
    pub fn additional_resources(&self) -> &Vec<AdditionalResource> {
        match self {
            Self::Jig(jig) => &jig.jig_data.additional_resources,
            Self::Playlist(playlist) => &playlist.playlist_data.additional_resources,
            Self::Resource(resource) => &resource.resource_data.additional_resources,
            Self::Course(course) => &course.course_data.additional_resources,
        }
    }

    /// get translated_description
    pub fn translated_description(&self) -> &HashMap<String, String> {
        match self {
            Self::Jig(jig) => &jig.jig_data.translated_description,
            Self::Playlist(playlist) => &playlist.playlist_data.translated_description,
            Self::Resource(resource) => &resource.resource_data.translated_description,
            Self::Course(course) => &course.course_data.translated_description,
        }
    }

    /// get theme
    pub fn theme(&self) -> ThemeId {
        match self {
            Self::Jig(jig) => jig.jig_data.theme,
            Self::Playlist(_) => ThemeId::default(),
            Self::Resource(_) => ThemeId::default(),
            Self::Course(_) => ThemeId::default(),
        }
    }

    /// get live_up_to_date
    pub fn live_up_to_date(&self) -> bool {
        match self {
            Self::Jig(jig) => jig.live_up_to_date,
            Self::Playlist(playlist) => playlist.live_up_to_date,
            Self::Resource(resource) => resource.live_up_to_date,
            Self::Course(course) => course.live_up_to_date,
        }
    }

    /// whether the current asset is a premium asset
    pub fn premium(&self) -> bool {
        match self {
            Self::Jig(asset) => asset.admin_data.premium,
            Self::Playlist(asset) => asset.admin_data.premium,
            Self::Resource(asset) => asset.admin_data.premium,
            Self::Course(asset) => asset.admin_data.premium,
        }
    }
}

// dyn_clone::clone_trait_object!(Asset);

/// Special parameter for allowing implicit `me` as a user.
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum UserOrMe {
    /// We should use the user found in the session auth.
    Me,

    /// we should use the provided user.
    User(UserId),
}

impl From<UserId> for UserOrMe {
    fn from(user_id: UserId) -> Self {
        UserOrMe::User(user_id)
    }
}

impl serde::Serialize for UserOrMe {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match self {
            UserOrMe::Me => serializer.serialize_str("me"),
            UserOrMe::User(id) => serializer.collect_str(&id),
        }
    }
}

impl<'de> serde::Deserialize<'de> for UserOrMe {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct Visitor;

        impl<'de> serde::de::Visitor<'de> for Visitor {
            type Value = UserOrMe;

            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str("`me` or `<uuid>`")
            }

            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                if value == "me" {
                    Ok(UserOrMe::Me)
                } else {
                    Uuid::from_str(value)
                        .map(|id| UserOrMe::User(UserId(id)))
                        .map_err(|e| E::custom(format!("failed to parse id: {}", e)))
                }
            }
        }

        deserializer.deserialize_str(Visitor)
    }
}

/// Sort browse results by timestamp
#[derive(Serialize, Deserialize, Copy, Clone, Eq, PartialEq, Debug, Display)]
#[cfg_attr(feature = "backend", derive(sqlx::Type))]
#[serde(rename_all = "camelCase")]
#[repr(i16)]
pub enum OrderBy {
    /// Order Asset results by timestamp created_at
    #[strum(serialize = "Created")]
    CreatedAt = 0,

    /// Order Asset results by timestamp published_at
    #[strum(serialize = "Published")]
    PublishedAt = 1,
}

/// Access level for the jig.
#[derive(Serialize, Deserialize, Copy, Clone, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "backend", derive(sqlx::Type))]
#[serde(rename_all = "camelCase")]
#[repr(i16)]
pub enum PrivacyLevel {
    /// Publicly available and indexed. Can be shared with others.
    Public = 0,

    /// Not indexed, but can be accessed by non-owners if the id is known. "Private" in the front-end
    Unlisted = 1,

    /// NOT IMPLEMENTED. Only available to the author.
    Private = 2,
}

impl PrivacyLevel {
    /// Represents the privacy level as a `str`. Relevant for Algolia tag filtering.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Public => "public",
            Self::Unlisted => "unlisted",
            Self::Private => "private",
        }
    }
}

impl FromStr for PrivacyLevel {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "public" => Self::Public,
            "unlisted" => Self::Unlisted,
            "private" => Self::Private,
            _ => return Err(anyhow!("invalid")),
        })
    }
}

impl Default for PrivacyLevel {
    fn default() -> Self {
        Self::Public
    }
}

/// Whether the data is draft or live.
#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
#[cfg_attr(feature = "backend", derive(sqlx::Type))]
#[serde(rename_all = "camelCase")]
#[repr(i16)]
pub enum DraftOrLive {
    /// Represents a draft copy
    Draft = 0,
    /// Represents a live copy
    Live = 1,
}

impl Default for DraftOrLive {
    fn default() -> Self {
        Self::Live
    }
}

impl DraftOrLive {
    /// create draft variant
    pub fn draft() -> Self {
        Self::Draft
    }

    /// create live variant
    pub fn live() -> Self {
        Self::Live
    }

    /// Returns `true` for a [`Self::Live`] value.
    ///
    /// ```
    /// let x = DraftOrLive::Live;
    /// assert_eq!(x.is_live(), true);
    ///
    /// let x = DraftOrLive::Draft;
    /// assert_eq!(x.is_live(), false);
    /// ```
    pub fn is_live(&self) -> bool {
        matches!(*self, DraftOrLive::Live)
    }

    /// Returns `true` for a [`Draft`] value.
    ///
    /// ```
    /// let x = DraftOrLive::Live;
    /// assert_eq!(x.is_draft(), false);
    ///
    /// let x = DraftOrLive::Draft;
    /// assert_eq!(x.is_draft(), true);
    /// ```
    pub fn is_draft(&self) -> bool {
        !self.is_live()
    }

    /// get str `draft` of `live`
    pub fn as_str(&self) -> &'static str {
        match self {
            DraftOrLive::Draft => "draft",
            DraftOrLive::Live => "live",
        }
    }
}

impl FromStr for DraftOrLive {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "draft" => Ok(Self::Draft),
            "live" => Ok(Self::Live),
            s => Err(format!("Can't create DraftFroLive from {:?}", s)),
        }
    }
}

impl From<DraftOrLive> for bool {
    fn from(draft_or_live: DraftOrLive) -> Self {
        match draft_or_live {
            DraftOrLive::Draft => false,
            DraftOrLive::Live => true,
        }
    }
}

impl From<bool> for DraftOrLive {
    fn from(draft_or_live: bool) -> Self {
        match draft_or_live {
            false => DraftOrLive::Draft,
            true => DraftOrLive::Live,
        }
    }
}