from_json

Function from_json 

Source
pub fn from_json<R: Read>(reader: R) -> Result<Vec<Chapter>, String>
Expand description

Reads chapters from a JSON chapters file.

ยงExample:

let json = r#"{
  "version": "1.2.0",
  "chapters": [
    {
      "startTime": 0,
      "endTime": 30.5,
      "title": "Chapter 1",
      "img": "https://example.com/chapter-1.jpg",
      "url": "https://example.com/chapter-1"
    },
    {
      "startTime": 30.5,
      "title": "Chapter 2"
    },
    {
      "startTime": 55,
      "title": "Hidden chapter",
      "toc": false
    },
    {
      "startTime": 60,
      "endTime": 90,
      "title": "Chapter 3",
      "img": "https://example.com/chapter-3.jpg"
    }
  ]
}"#;

let chapters = chapters::from_json(json.as_bytes()).unwrap();

assert_eq!(
    chapters,
    vec![
        Chapter {
            start: Duration::seconds(0),
            end: Some(Duration::seconds(30) + Duration::milliseconds(500)),
            title: Some("Chapter 1".to_string()),
            image: Some(Image::Url(
                url::Url::parse("https://example.com/chapter-1.jpg").unwrap()
            )),
            link: Some(Link {
                url: url::Url::parse("https://example.com/chapter-1").unwrap(),
                title: None,
            }),
            ..Default::default()
        },
        Chapter {
            start: Duration::seconds(30) + Duration::milliseconds(500),
            end: None,
            title: Some("Chapter 2".to_string()),
            ..Default::default()
        },
        Chapter {
            start: Duration::seconds(55),
            end: None,
            title: Some("Hidden chapter".to_string()),
            hidden: true,
            ..Default::default()
        },
        Chapter {
            start: Duration::seconds(60),
            end: Some(Duration::seconds(90)),
            title: Some("Chapter 3".to_string()),
            image: Some(Image::Url(
                url::Url::parse("https://example.com/chapter-3.jpg").unwrap()
            )),
            ..Default::default()
        },
    ]
);