line_ui/element/
into.rs

1/*
2 * Copyright (c) 2025 Jasmine Tai. All rights reserved.
3 */
4
5use crate::element::{Element, FixedWidth, Styled, Text};
6use crate::style::Style;
7
8/// A type that can be converted into an element.
9pub trait IntoElement: Sized {
10    /// The element type to be converted into.
11    type ElementType: Element;
12
13    /// Converts this type into an [`Element`].
14    fn into_element(self) -> Self::ElementType;
15
16    /// Convenience function to wrap this element in a [`FixedWidth`].
17    fn fixed_width(self, width: usize) -> FixedWidth<Self::ElementType> {
18        FixedWidth::new(width, self.into_element())
19    }
20
21    /// Convenience function to wrap this element in a [`Styled`].
22    fn with_style(self, style: Style) -> Styled<Self::ElementType> {
23        Styled::new(style, self.into_element())
24    }
25}
26
27impl<E: Element> IntoElement for E {
28    type ElementType = Self;
29
30    fn into_element(self) -> Self::ElementType {
31        self
32    }
33}
34
35impl<'s> IntoElement for &'s str {
36    type ElementType = Text<'s>;
37
38    fn into_element(self) -> Self::ElementType {
39        Text::from(self)
40    }
41}