re_types/archetypes/
text_log.rs

1// DO NOT EDIT! This file was auto-generated by crates/build/re_types_builder/src/codegen/rust/api.rs
2// Based on "crates/store/re_types/definitions/rerun/archetypes/text_log.fbs".
3
4#![allow(unused_braces)]
5#![allow(unused_imports)]
6#![allow(unused_parens)]
7#![allow(clippy::clone_on_copy)]
8#![allow(clippy::cloned_instead_of_copied)]
9#![allow(clippy::map_flatten)]
10#![allow(clippy::needless_question_mark)]
11#![allow(clippy::new_without_default)]
12#![allow(clippy::redundant_closure)]
13#![allow(clippy::too_many_arguments)]
14#![allow(clippy::too_many_lines)]
15
16use ::re_types_core::try_serialize_field;
17use ::re_types_core::SerializationResult;
18use ::re_types_core::{ComponentBatch as _, SerializedComponentBatch};
19use ::re_types_core::{ComponentDescriptor, ComponentType};
20use ::re_types_core::{DeserializationError, DeserializationResult};
21
22/// **Archetype**: A log entry in a text log, comprised of a text body and its log level.
23///
24/// ## Example
25///
26/// ### `text_log_integration`:
27/// ```ignore
28/// use rerun::external::log;
29///
30/// fn main() -> Result<(), Box<dyn std::error::Error>> {
31///     let rec = rerun::RecordingStreamBuilder::new("rerun_example_text_log_integration").spawn()?;
32///
33///     // Log a text entry directly:
34///     rec.log(
35///         "logs",
36///         &rerun::TextLog::new("this entry has loglevel TRACE")
37///             .with_level(rerun::TextLogLevel::TRACE),
38///     )?;
39///
40///     // Or log via a logging handler:
41///     rerun::Logger::new(rec.clone()) // recording streams are ref-counted
42///         .with_path_prefix("logs/handler")
43///         // You can also use the standard `RUST_LOG` environment variable!
44///         .with_filter(rerun::default_log_filter())
45///         .init()?;
46///     log::info!("This INFO log got added through the standard logging interface");
47///
48///     log::logger().flush();
49///
50///     Ok(())
51/// }
52/// ```
53/// <center>
54/// <picture>
55///   <source media="(max-width: 480px)" srcset="https://static.rerun.io/text_log_integration/9737d0c986325802a9885499d6fcc773b1736488/480w.png">
56///   <source media="(max-width: 768px)" srcset="https://static.rerun.io/text_log_integration/9737d0c986325802a9885499d6fcc773b1736488/768w.png">
57///   <source media="(max-width: 1024px)" srcset="https://static.rerun.io/text_log_integration/9737d0c986325802a9885499d6fcc773b1736488/1024w.png">
58///   <source media="(max-width: 1200px)" srcset="https://static.rerun.io/text_log_integration/9737d0c986325802a9885499d6fcc773b1736488/1200w.png">
59///   <img src="https://static.rerun.io/text_log_integration/9737d0c986325802a9885499d6fcc773b1736488/full.png" width="640">
60/// </picture>
61/// </center>
62#[derive(Clone, Debug, PartialEq, Default)]
63pub struct TextLog {
64    /// The body of the message.
65    pub text: Option<SerializedComponentBatch>,
66
67    /// The verbosity level of the message.
68    ///
69    /// This can be used to filter the log messages in the Rerun Viewer.
70    pub level: Option<SerializedComponentBatch>,
71
72    /// Optional color to use for the log line in the Rerun Viewer.
73    pub color: Option<SerializedComponentBatch>,
74}
75
76impl TextLog {
77    /// Returns the [`ComponentDescriptor`] for [`Self::text`].
78    ///
79    /// The corresponding component is [`crate::components::Text`].
80    #[inline]
81    pub fn descriptor_text() -> ComponentDescriptor {
82        ComponentDescriptor {
83            archetype: Some("rerun.archetypes.TextLog".into()),
84            component: "TextLog:text".into(),
85            component_type: Some("rerun.components.Text".into()),
86        }
87    }
88
89    /// Returns the [`ComponentDescriptor`] for [`Self::level`].
90    ///
91    /// The corresponding component is [`crate::components::TextLogLevel`].
92    #[inline]
93    pub fn descriptor_level() -> ComponentDescriptor {
94        ComponentDescriptor {
95            archetype: Some("rerun.archetypes.TextLog".into()),
96            component: "TextLog:level".into(),
97            component_type: Some("rerun.components.TextLogLevel".into()),
98        }
99    }
100
101    /// Returns the [`ComponentDescriptor`] for [`Self::color`].
102    ///
103    /// The corresponding component is [`crate::components::Color`].
104    #[inline]
105    pub fn descriptor_color() -> ComponentDescriptor {
106        ComponentDescriptor {
107            archetype: Some("rerun.archetypes.TextLog".into()),
108            component: "TextLog:color".into(),
109            component_type: Some("rerun.components.Color".into()),
110        }
111    }
112}
113
114static REQUIRED_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; 1usize]> =
115    once_cell::sync::Lazy::new(|| [TextLog::descriptor_text()]);
116
117static RECOMMENDED_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; 1usize]> =
118    once_cell::sync::Lazy::new(|| [TextLog::descriptor_level()]);
119
120static OPTIONAL_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; 1usize]> =
121    once_cell::sync::Lazy::new(|| [TextLog::descriptor_color()]);
122
123static ALL_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; 3usize]> =
124    once_cell::sync::Lazy::new(|| {
125        [
126            TextLog::descriptor_text(),
127            TextLog::descriptor_level(),
128            TextLog::descriptor_color(),
129        ]
130    });
131
132impl TextLog {
133    /// The total number of components in the archetype: 1 required, 1 recommended, 1 optional
134    pub const NUM_COMPONENTS: usize = 3usize;
135}
136
137impl ::re_types_core::Archetype for TextLog {
138    #[inline]
139    fn name() -> ::re_types_core::ArchetypeName {
140        "rerun.archetypes.TextLog".into()
141    }
142
143    #[inline]
144    fn display_name() -> &'static str {
145        "Text log"
146    }
147
148    #[inline]
149    fn required_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
150        REQUIRED_COMPONENTS.as_slice().into()
151    }
152
153    #[inline]
154    fn recommended_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
155        RECOMMENDED_COMPONENTS.as_slice().into()
156    }
157
158    #[inline]
159    fn optional_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
160        OPTIONAL_COMPONENTS.as_slice().into()
161    }
162
163    #[inline]
164    fn all_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
165        ALL_COMPONENTS.as_slice().into()
166    }
167
168    #[inline]
169    fn from_arrow_components(
170        arrow_data: impl IntoIterator<Item = (ComponentDescriptor, arrow::array::ArrayRef)>,
171    ) -> DeserializationResult<Self> {
172        re_tracing::profile_function!();
173        use ::re_types_core::{Loggable as _, ResultExt as _};
174        let arrays_by_descr: ::nohash_hasher::IntMap<_, _> = arrow_data.into_iter().collect();
175        let text = arrays_by_descr
176            .get(&Self::descriptor_text())
177            .map(|array| SerializedComponentBatch::new(array.clone(), Self::descriptor_text()));
178        let level = arrays_by_descr
179            .get(&Self::descriptor_level())
180            .map(|array| SerializedComponentBatch::new(array.clone(), Self::descriptor_level()));
181        let color = arrays_by_descr
182            .get(&Self::descriptor_color())
183            .map(|array| SerializedComponentBatch::new(array.clone(), Self::descriptor_color()));
184        Ok(Self { text, level, color })
185    }
186}
187
188impl ::re_types_core::AsComponents for TextLog {
189    #[inline]
190    fn as_serialized_batches(&self) -> Vec<SerializedComponentBatch> {
191        use ::re_types_core::Archetype as _;
192        [self.text.clone(), self.level.clone(), self.color.clone()]
193            .into_iter()
194            .flatten()
195            .collect()
196    }
197}
198
199impl ::re_types_core::ArchetypeReflectionMarker for TextLog {}
200
201impl TextLog {
202    /// Create a new `TextLog`.
203    #[inline]
204    pub fn new(text: impl Into<crate::components::Text>) -> Self {
205        Self {
206            text: try_serialize_field(Self::descriptor_text(), [text]),
207            level: None,
208            color: None,
209        }
210    }
211
212    /// Update only some specific fields of a `TextLog`.
213    #[inline]
214    pub fn update_fields() -> Self {
215        Self::default()
216    }
217
218    /// Clear all the fields of a `TextLog`.
219    #[inline]
220    pub fn clear_fields() -> Self {
221        use ::re_types_core::Loggable as _;
222        Self {
223            text: Some(SerializedComponentBatch::new(
224                crate::components::Text::arrow_empty(),
225                Self::descriptor_text(),
226            )),
227            level: Some(SerializedComponentBatch::new(
228                crate::components::TextLogLevel::arrow_empty(),
229                Self::descriptor_level(),
230            )),
231            color: Some(SerializedComponentBatch::new(
232                crate::components::Color::arrow_empty(),
233                Self::descriptor_color(),
234            )),
235        }
236    }
237
238    /// Partitions the component data into multiple sub-batches.
239    ///
240    /// Specifically, this transforms the existing [`SerializedComponentBatch`]es data into [`SerializedComponentColumn`]s
241    /// instead, via [`SerializedComponentBatch::partitioned`].
242    ///
243    /// This makes it possible to use `RecordingStream::send_columns` to send columnar data directly into Rerun.
244    ///
245    /// The specified `lengths` must sum to the total length of the component batch.
246    ///
247    /// [`SerializedComponentColumn`]: [::re_types_core::SerializedComponentColumn]
248    #[inline]
249    pub fn columns<I>(
250        self,
251        _lengths: I,
252    ) -> SerializationResult<impl Iterator<Item = ::re_types_core::SerializedComponentColumn>>
253    where
254        I: IntoIterator<Item = usize> + Clone,
255    {
256        let columns = [
257            self.text
258                .map(|text| text.partitioned(_lengths.clone()))
259                .transpose()?,
260            self.level
261                .map(|level| level.partitioned(_lengths.clone()))
262                .transpose()?,
263            self.color
264                .map(|color| color.partitioned(_lengths.clone()))
265                .transpose()?,
266        ];
267        Ok(columns.into_iter().flatten())
268    }
269
270    /// Helper to partition the component data into unit-length sub-batches.
271    ///
272    /// This is semantically similar to calling [`Self::columns`] with `std::iter::take(1).repeat(n)`,
273    /// where `n` is automatically guessed.
274    #[inline]
275    pub fn columns_of_unit_batches(
276        self,
277    ) -> SerializationResult<impl Iterator<Item = ::re_types_core::SerializedComponentColumn>> {
278        let len_text = self.text.as_ref().map(|b| b.array.len());
279        let len_level = self.level.as_ref().map(|b| b.array.len());
280        let len_color = self.color.as_ref().map(|b| b.array.len());
281        let len = None.or(len_text).or(len_level).or(len_color).unwrap_or(0);
282        self.columns(std::iter::repeat(1).take(len))
283    }
284
285    /// The body of the message.
286    #[inline]
287    pub fn with_text(mut self, text: impl Into<crate::components::Text>) -> Self {
288        self.text = try_serialize_field(Self::descriptor_text(), [text]);
289        self
290    }
291
292    /// This method makes it possible to pack multiple [`crate::components::Text`] in a single component batch.
293    ///
294    /// This only makes sense when used in conjunction with [`Self::columns`]. [`Self::with_text`] should
295    /// be used when logging a single row's worth of data.
296    #[inline]
297    pub fn with_many_text(
298        mut self,
299        text: impl IntoIterator<Item = impl Into<crate::components::Text>>,
300    ) -> Self {
301        self.text = try_serialize_field(Self::descriptor_text(), text);
302        self
303    }
304
305    /// The verbosity level of the message.
306    ///
307    /// This can be used to filter the log messages in the Rerun Viewer.
308    #[inline]
309    pub fn with_level(mut self, level: impl Into<crate::components::TextLogLevel>) -> Self {
310        self.level = try_serialize_field(Self::descriptor_level(), [level]);
311        self
312    }
313
314    /// This method makes it possible to pack multiple [`crate::components::TextLogLevel`] in a single component batch.
315    ///
316    /// This only makes sense when used in conjunction with [`Self::columns`]. [`Self::with_level`] should
317    /// be used when logging a single row's worth of data.
318    #[inline]
319    pub fn with_many_level(
320        mut self,
321        level: impl IntoIterator<Item = impl Into<crate::components::TextLogLevel>>,
322    ) -> Self {
323        self.level = try_serialize_field(Self::descriptor_level(), level);
324        self
325    }
326
327    /// Optional color to use for the log line in the Rerun Viewer.
328    #[inline]
329    pub fn with_color(mut self, color: impl Into<crate::components::Color>) -> Self {
330        self.color = try_serialize_field(Self::descriptor_color(), [color]);
331        self
332    }
333
334    /// This method makes it possible to pack multiple [`crate::components::Color`] in a single component batch.
335    ///
336    /// This only makes sense when used in conjunction with [`Self::columns`]. [`Self::with_color`] should
337    /// be used when logging a single row's worth of data.
338    #[inline]
339    pub fn with_many_color(
340        mut self,
341        color: impl IntoIterator<Item = impl Into<crate::components::Color>>,
342    ) -> Self {
343        self.color = try_serialize_field(Self::descriptor_color(), color);
344        self
345    }
346}
347
348impl ::re_byte_size::SizeBytes for TextLog {
349    #[inline]
350    fn heap_size_bytes(&self) -> u64 {
351        self.text.heap_size_bytes() + self.level.heap_size_bytes() + self.color.heap_size_bytes()
352    }
353}