Skip to main content

grib/encoder/message/
single.rs

1use std::iter::{Once, once};
2
3use super::{
4    GpvEncoder, WriteGrib2GridDef, WriteGrib2Ident, WriteGrib2LocalUse, WriteGrib2Message,
5    WriteGrib2MessageIterL1, WriteGrib2MessageIterL2, WriteGrib2ProductDef,
6};
7
8/// A writer for GRIB2 messages that contain only one submessage.
9pub struct SingleGrib2Message<'d, I, L, G, P> {
10    pub(crate) discipline: u8,
11    pub(crate) ident: I,
12    pub(crate) local_use: Option<L>,
13    pub(crate) grid: G,
14    pub(crate) product: P,
15    pub(crate) values: GpvEncoder<'d>,
16}
17
18impl<'d, I, L, G, P> SingleGrib2Message<'d, I, L, G, P> {
19    pub fn new(
20        discipline: u8,
21        ident: I,
22        local_use: Option<L>,
23        grid: G,
24        product: P,
25        values: GpvEncoder<'d>,
26    ) -> Self {
27        Self {
28            discipline,
29            ident,
30            local_use,
31            grid,
32            product,
33            values,
34        }
35    }
36}
37
38impl<'d, I, L, G, P> WriteGrib2Message for SingleGrib2Message<'d, I, L, G, P>
39where
40    I: WriteGrib2Ident,
41    L: WriteGrib2LocalUse,
42    G: WriteGrib2GridDef,
43    P: WriteGrib2ProductDef,
44{
45    type S1<'a>
46        = I
47    where
48        Self: 'a;
49
50    type Item<'a>
51        = (&'a Option<L>, &'a G, &'a P, &'a GpvEncoder<'d>)
52    where
53        Self: 'a;
54
55    type Iter<'a>
56        = Once<(&'a Option<L>, &'a G, &'a P, &'a GpvEncoder<'d>)>
57    where
58        Self: 'a;
59
60    fn discipline(&self) -> u8 {
61        self.discipline
62    }
63
64    fn section1(&self) -> &Self::S1<'_> {
65        &self.ident
66    }
67
68    fn iter(&self) -> Self::Iter<'_> {
69        once((&self.local_use, &self.grid, &self.product, &self.values))
70    }
71}
72
73impl<'a, 'd, L, G, P> WriteGrib2MessageIterL1 for (&'a Option<L>, &'a G, &'a P, &'a GpvEncoder<'d>)
74where
75    L: WriteGrib2LocalUse,
76    G: WriteGrib2GridDef,
77    P: WriteGrib2ProductDef,
78{
79    type S2<'s>
80        = L
81    where
82        Self: 's;
83
84    type Item<'s>
85        = (&'a G, &'a P, &'a GpvEncoder<'d>)
86    where
87        Self: 's;
88
89    type Iter<'s>
90        = Once<(&'a G, &'a P, &'a GpvEncoder<'d>)>
91    where
92        Self: 's;
93
94    fn section2(&self) -> Option<&Self::S2<'_>> {
95        self.0.as_ref()
96    }
97
98    fn iter(&self) -> Self::Iter<'_> {
99        once((self.1, self.2, self.3))
100    }
101}
102
103impl<'a, 'd, G, P> WriteGrib2MessageIterL2 for (&'a G, &'a P, &'a GpvEncoder<'d>)
104where
105    G: WriteGrib2GridDef,
106    P: WriteGrib2ProductDef,
107{
108    type S3<'s>
109        = G
110    where
111        Self: 's;
112
113    type Item<'s>
114        = (&'a P, &'a GpvEncoder<'d>)
115    where
116        Self: 's;
117
118    type Iter<'s>
119        = Once<(&'a P, &'a GpvEncoder<'d>)>
120    where
121        Self: 's;
122
123    fn section3(&self) -> &Self::S3<'_> {
124        self.0
125    }
126
127    fn iter(&self) -> Self::Iter<'_> {
128        once((self.1, self.2))
129    }
130}