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
// Take a look at the license at the top of the repository in the LICENSE file.
use std::fmt;

use crate::{prelude::*, DiscovererStreamInfo};

#[derive(Debug)]
pub struct Iter {
    stream_info: Option<DiscovererStreamInfo>,
    direction_forward: bool,
}

impl Iterator for Iter {
    type Item = DiscovererStreamInfo;

    fn next(&mut self) -> Option<DiscovererStreamInfo> {
        let current = self.stream_info.take();
        self.stream_info = match current {
            Some(ref c) => {
                // Decide on the direction
                if self.direction_forward {
                    c.next()
                } else {
                    c.previous()
                }
            }
            None => None,
        };
        current
    }
}

impl std::iter::FusedIterator for Iter {}

mod sealed {
    pub trait Sealed {}
    impl<T: super::IsA<super::DiscovererStreamInfo>> Sealed for T {}
}

pub trait DiscovererStreamInfoExtManual:
    sealed::Sealed + IsA<DiscovererStreamInfo> + 'static
{
    fn next_iter(&self) -> Iter {
        Iter {
            stream_info: self.next(),
            direction_forward: true,
        }
    }

    fn previous_iter(&self) -> Iter {
        Iter {
            stream_info: self.previous(),
            direction_forward: false,
        }
    }
}

impl<O: IsA<DiscovererStreamInfo>> DiscovererStreamInfoExtManual for O {}

pub struct Debug<'a>(&'a DiscovererStreamInfo);

impl<'a> fmt::Debug for Debug<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut d = f.debug_struct("DiscovererStreamInfo");
        d.field("caps", &self.0.caps())
            .field("stream-id", &self.0.stream_id())
            .field("misc", &self.0.misc())
            .field("stream-type-nick", &self.0.stream_type_nick())
            .field("tags", &self.0.tags())
            .field("toc", &self.0.toc());

        #[cfg(feature = "v1_20")]
        d.field("stream-number", &self.0.stream_number());

        d.finish()
    }
}

impl DiscovererStreamInfo {
    pub fn debug(&self) -> Debug {
        Debug(self)
    }
}