isbnlib/enums/
toc_enum.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
use serde::{Deserialize, Serialize};
use serde_json;

use crate::jsonb::edition_toc::EditionToC;

#[derive(
Serialize,
Deserialize,
Debug,
Clone,
PartialEq,
)]
#[serde(untagged)]
pub enum ToCEnum {
    String(String),
    ToC(EditionToC)
}

impl ToCEnum {
    pub fn to_json(self) -> serde_json::Value {
        match self {
            ToCEnum::String(string) => {
                serde_json::json!({
                    "String": string
                })
            }
            ToCEnum::ToC(content) => {
                let level = content.level.unwrap_or(0);
                let title = content.title.unwrap_or(String::new());
                let class = content.class.unwrap_or(String::new());

                serde_json::json!({
                    "level": level,
                    "title": title,
                    "class": class
                })
            }
        }
    }
}