Coverage Report

Created: 2024-05-16 12:16

/__w/smoldot/smoldot/repo/light-base/src/platform/with_prefix.rs
Line
Count
Source (jump to first uncovered line)
1
// Smoldot
2
// Copyright (C) 2024  Pierre Krieger
3
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
4
5
// This program is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
10
// This program is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
15
// You should have received a copy of the GNU General Public License
16
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
use core::{iter, pin::Pin};
19
20
use super::{Address, ConnectionType, LogLevel, MultiStreamAddress, PlatformRef};
21
use alloc::{borrow::Cow, format, string::String};
22
23
/// Implementation of a [`PlatformRef`] that wraps around another platform and adds a prefix
24
/// before every log line and task name.
25
#[derive(Debug, Clone)]
26
pub struct WithPrefix<T> {
27
    inner: T,
28
    prefix: String,
29
}
30
31
impl<T> WithPrefix<T> {
32
    /// Builds a new [`WithPrefix`].
33
0
    pub const fn new(prefix: String, inner: T) -> Self {
34
0
        WithPrefix { inner, prefix }
35
0
    }
Unexecuted instantiation: _RNvMNtNtCsiGub1lfKphe_13smoldot_light8platform11with_prefixINtB2_10WithPrefixpE3newB6_
Unexecuted instantiation: _RNvMNtNtCsih6EgvAwZF2_13smoldot_light8platform11with_prefixINtB2_10WithPrefixpE3newB6_
36
}
37
38
impl<T: PlatformRef> PlatformRef for WithPrefix<T> {
39
    type Delay = T::Delay;
40
    type Instant = T::Instant;
41
    type MultiStream = T::MultiStream;
42
    type Stream = T::Stream;
43
    type ReadWriteAccess<'a> = T::ReadWriteAccess<'a>;
44
    type StreamErrorRef<'a> = T::StreamErrorRef<'a>;
45
    type StreamConnectFuture = T::StreamConnectFuture;
46
    type MultiStreamConnectFuture = T::MultiStreamConnectFuture;
47
    type StreamUpdateFuture<'a> = T::StreamUpdateFuture<'a>;
48
    type NextSubstreamFuture<'a> = T::NextSubstreamFuture<'a>;
49
50
0
    fn now_from_unix_epoch(&self) -> core::time::Duration {
51
0
        self.inner.now_from_unix_epoch()
52
0
    }
Unexecuted instantiation: _RNvXININtNtCsiGub1lfKphe_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef19now_from_unix_epochB9_
Unexecuted instantiation: _RNvXININtNtCsih6EgvAwZF2_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef19now_from_unix_epochB9_
53
54
0
    fn now(&self) -> Self::Instant {
55
0
        self.inner.now()
56
0
    }
Unexecuted instantiation: _RNvXININtNtCsiGub1lfKphe_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef3nowB9_
Unexecuted instantiation: _RNvXININtNtCsih6EgvAwZF2_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef3nowB9_
57
58
0
    fn fill_random_bytes(&self, buffer: &mut [u8]) {
59
0
        self.inner.fill_random_bytes(buffer)
60
0
    }
Unexecuted instantiation: _RNvXININtNtCsiGub1lfKphe_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef17fill_random_bytesB9_
Unexecuted instantiation: _RNvXININtNtCsih6EgvAwZF2_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef17fill_random_bytesB9_
61
62
0
    fn sleep(&self, duration: core::time::Duration) -> Self::Delay {
63
0
        self.inner.sleep(duration)
64
0
    }
Unexecuted instantiation: _RNvXININtNtCsiGub1lfKphe_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef5sleepB9_
Unexecuted instantiation: _RNvXININtNtCsih6EgvAwZF2_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef5sleepB9_
65
66
0
    fn sleep_until(&self, when: Self::Instant) -> Self::Delay {
67
0
        self.inner.sleep_until(when)
68
0
    }
Unexecuted instantiation: _RNvXININtNtCsiGub1lfKphe_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef11sleep_untilB9_
Unexecuted instantiation: _RNvXININtNtCsih6EgvAwZF2_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef11sleep_untilB9_
69
70
0
    fn spawn_task(
71
0
        &self,
72
0
        task_name: Cow<str>,
73
0
        task: impl futures_util::future::Future<Output = ()> + Send + 'static,
74
0
    ) {
75
0
        self.inner
76
0
            .spawn_task(Cow::Owned(format!("{}-{}", self.prefix, task_name)), task)
77
0
    }
Unexecuted instantiation: _RINvXININtNtCsiGub1lfKphe_13smoldot_light8platform11with_prefixs_0pEINtB6_10WithPrefixpENtB8_11PlatformRef10spawn_taskpEBa_
Unexecuted instantiation: _RINvXININtNtCsih6EgvAwZF2_13smoldot_light8platform11with_prefixs_0pEINtB6_10WithPrefixpENtB8_11PlatformRef10spawn_taskpEBa_
78
79
0
    fn log<'a>(
80
0
        &self,
81
0
        log_level: LogLevel,
82
0
        log_target: &'a str,
83
0
        message: &'a str,
84
0
        mut key_values: impl Iterator<Item = (&'a str, &'a dyn core::fmt::Display)>,
85
0
    ) {
86
0
        self.inner.log(
87
0
            log_level,
88
0
            &format!("{}-{}", self.prefix, log_target),
89
0
            message,
90
0
            // We have to use `iter::from_fn` due to a lifetime mismatch between the items
91
0
            // produced by the iterator and the newly-constructed log target.
92
0
            iter::from_fn(move || key_values.next()),
Unexecuted instantiation: _RNCINvXININtNtCsiGub1lfKphe_13smoldot_light8platform11with_prefixs_0pEINtB8_10WithPrefixpENtBa_11PlatformRef3logpE0Bc_
Unexecuted instantiation: _RNCINvXININtNtCsih6EgvAwZF2_13smoldot_light8platform11with_prefixs_0pEINtB8_10WithPrefixpENtBa_11PlatformRef3logpE0Bc_
93
0
        )
94
0
    }
Unexecuted instantiation: _RINvXININtNtCsiGub1lfKphe_13smoldot_light8platform11with_prefixs_0pEINtB6_10WithPrefixpENtB8_11PlatformRef3logpEBa_
Unexecuted instantiation: _RINvXININtNtCsih6EgvAwZF2_13smoldot_light8platform11with_prefixs_0pEINtB6_10WithPrefixpENtB8_11PlatformRef3logpEBa_
95
96
0
    fn client_name(&self) -> Cow<str> {
97
0
        self.inner.client_name()
98
0
    }
Unexecuted instantiation: _RNvXININtNtCsiGub1lfKphe_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef11client_nameB9_
Unexecuted instantiation: _RNvXININtNtCsih6EgvAwZF2_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef11client_nameB9_
99
100
0
    fn client_version(&self) -> Cow<str> {
101
0
        self.inner.client_version()
102
0
    }
Unexecuted instantiation: _RNvXININtNtCsiGub1lfKphe_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef14client_versionB9_
Unexecuted instantiation: _RNvXININtNtCsih6EgvAwZF2_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef14client_versionB9_
103
104
0
    fn supports_connection_type(&self, connection_type: ConnectionType) -> bool {
105
0
        self.inner.supports_connection_type(connection_type)
106
0
    }
Unexecuted instantiation: _RNvXININtNtCsiGub1lfKphe_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef24supports_connection_typeB9_
Unexecuted instantiation: _RNvXININtNtCsih6EgvAwZF2_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef24supports_connection_typeB9_
107
108
0
    fn connect_stream(&self, address: Address) -> Self::StreamConnectFuture {
109
0
        self.inner.connect_stream(address)
110
0
    }
Unexecuted instantiation: _RNvXININtNtCsiGub1lfKphe_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef14connect_streamB9_
Unexecuted instantiation: _RNvXININtNtCsih6EgvAwZF2_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef14connect_streamB9_
111
112
0
    fn connect_multistream(&self, address: MultiStreamAddress) -> Self::MultiStreamConnectFuture {
113
0
        self.inner.connect_multistream(address)
114
0
    }
Unexecuted instantiation: _RNvXININtNtCsiGub1lfKphe_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef19connect_multistreamB9_
Unexecuted instantiation: _RNvXININtNtCsih6EgvAwZF2_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef19connect_multistreamB9_
115
116
0
    fn open_out_substream(&self, connection: &mut Self::MultiStream) {
117
0
        self.inner.open_out_substream(connection)
118
0
    }
Unexecuted instantiation: _RNvXININtNtCsiGub1lfKphe_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef18open_out_substreamB9_
Unexecuted instantiation: _RNvXININtNtCsih6EgvAwZF2_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef18open_out_substreamB9_
119
120
0
    fn next_substream<'a>(
121
0
        &self,
122
0
        connection: &'a mut Self::MultiStream,
123
0
    ) -> Self::NextSubstreamFuture<'a> {
124
0
        self.inner.next_substream(connection)
125
0
    }
Unexecuted instantiation: _RNvXININtNtCsiGub1lfKphe_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef14next_substreamB9_
Unexecuted instantiation: _RNvXININtNtCsih6EgvAwZF2_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef14next_substreamB9_
126
127
0
    fn read_write_access<'a>(
128
0
        &self,
129
0
        stream: Pin<&'a mut Self::Stream>,
130
0
    ) -> Result<Self::ReadWriteAccess<'a>, Self::StreamErrorRef<'a>> {
131
0
        self.inner.read_write_access(stream)
132
0
    }
Unexecuted instantiation: _RNvXININtNtCsiGub1lfKphe_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef17read_write_accessB9_
Unexecuted instantiation: _RNvXININtNtCsih6EgvAwZF2_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef17read_write_accessB9_
133
134
0
    fn wait_read_write_again<'a>(
135
0
        &self,
136
0
        stream: Pin<&'a mut Self::Stream>,
137
0
    ) -> Self::StreamUpdateFuture<'a> {
138
0
        self.inner.wait_read_write_again(stream)
139
0
    }
Unexecuted instantiation: _RNvXININtNtCsiGub1lfKphe_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef21wait_read_write_againB9_
Unexecuted instantiation: _RNvXININtNtCsih6EgvAwZF2_13smoldot_light8platform11with_prefixs_0pEINtB5_10WithPrefixpENtB7_11PlatformRef21wait_read_write_againB9_
140
}