to_json

Function to_json 

Source
pub fn to_json(chapters: &[Chapter]) -> Result<String, String>
Expand description

Writes chapters to a JSON chapters file.

ยงExample:

let chapters = vec![
   Chapter {
       start: Duration::zero(),
       title: Some("Chapter 1".to_string()),
       ..Default::default()
   },
   Chapter {
       start: Duration::seconds(45) + Duration::milliseconds(900),
       title: Some("Chapter 2".to_string()),
       link: Some(Link {
           url: "https://example.com".parse().unwrap(),
           title: Some("Example".to_string()),
       }),
       ..Default::default()
   },
   Chapter {
       start: Duration::minutes(1)+Duration::seconds(5),
       title: Some("Hidden chapter".to_string()),
       hidden: true,
       ..Default::default()
   },
   Chapter {
       start: Duration::minutes(2)+Duration::seconds(10)+Duration::milliseconds(500),
       title: Some("Chapter 3".to_string()),
       image: Some(Image::Url("https://example.com/image.png".parse().unwrap())),
       ..Default::default()
   },
];

let json_chapters = chapters::to_json(&chapters).expect("Failed to serialize chapters");

assert_eq!(json_chapters, r#"{
  "version": "1.2.0",
  "chapters": [
    {
      "startTime": 0,
      "title": "Chapter 1"
    },
    {
      "startTime": 45.9,
      "title": "Chapter 2",
      "url": "https://example.com/"
    },
    {
      "startTime": 65,
      "title": "Hidden chapter",
      "toc": false
    },
    {
      "startTime": 130.5,
      "title": "Chapter 3",
      "img": "https://example.com/image.png"
    }
  ]
}"#);