aboutsummaryrefslogtreecommitdiffstats
path: root/src/stream.rs
blob: c717a171f0bf1e21c68193299d251c7c49869e4d (plain) (blame)
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
/**
 * High level stream API for ZotApi.
 *
 * The stream API will parse the raw json response from a stream (network
 * or channel stream) into proper types, that should be easier to use from
 * a client application.
 *
 * SPDX-FileCopyrightText: 2023 Eilertsens Kodeknekkeri
 * SPDX-FileCopyrightText: 2023 Harald Eilertsen <haraldei@anduin.net>
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

mod actor;
mod datetime;
mod streamitem;
mod tag;
mod verb;

pub use actor::Actor;
pub use datetime::DateTime;
pub use streamitem::{
    StreamItem,
    StreamItemEncoding,
    StreamItemType,
};
pub use tag::{
    Tag,
    TagType,
};
pub use verb::Verb;

use std::{
    error::Error,
    result::Result
};

#[derive(Debug, PartialEq)]
pub struct Stream {
    pub items: Vec<StreamItem>,
}

impl Stream {
    pub fn from_json(json: &str) -> Result<Self, Box<dyn Error + 'static>> {
        let items: Vec<StreamItem> = serde_json::from_str(&json)?;
        Ok(Self { items })
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn construct_stream_from_empty_string_should_fail() {
        let s = Stream::from_json("");
        assert!(s.is_err());
    }

    #[test]
    fn construct_stream_from_empty_json() {
        let s = Stream::from_json("[]");
        assert!(s.is_ok());
    }

}