1use crate::ast::Sheet;
2use crate::manager::StyleManager;
3#[cfg(feature = "yew")]
4use crate::Style;
5
6#[derive(Debug, Clone, PartialEq)]
24pub struct StyleSource {
25 inner: Sheet,
26
27 manager: Option<StyleManager>,
28 #[cfg(all(debug_assertions, feature = "debug_style_locations"))]
29 pub(crate) location: String,
30}
31
32impl StyleSource {
33 #[cfg(all(debug_assertions, feature = "debug_style_locations"))]
34 #[track_caller]
35 pub(crate) fn get_caller_location() -> String {
36 let caller_loc = std::panic::Location::caller();
37 format!(
39 "{}:{}:{}",
40 caller_loc.file(),
41 caller_loc.line(),
42 caller_loc.column()
43 )
44 }
45
46 pub(crate) fn into_sheet(self) -> Sheet {
47 self.inner
48 }
49
50 #[cfg(feature = "yew")]
51 pub(crate) fn into_style(mut self) -> Style {
52 use stylist_core::ResultDisplay;
53 let manager = self.manager.take().unwrap_or_default();
54 Style::new_with_manager(self, manager).expect_display("Failed to create style")
55 }
56
57 #[doc(hidden)]
58 pub fn with_manager(mut self, manager: StyleManager) -> Self {
59 self.manager = Some(manager);
60
61 self
62 }
63}
64
65impl From<Sheet> for StyleSource {
66 #[cfg_attr(all(debug_assertions, feature = "debug_style_locations"), track_caller)]
67 fn from(sheet: Sheet) -> StyleSource {
68 StyleSource {
69 inner: sheet,
70 manager: None,
71 #[cfg(all(debug_assertions, feature = "debug_style_locations"))]
72 location: Self::get_caller_location(),
73 }
74 }
75}
76
77#[cfg(feature = "parser")]
78mod feat_parser {
79 use super::*;
80 use std::borrow::Cow;
81 use std::str::FromStr;
82
83 impl TryFrom<String> for StyleSource {
84 type Error = crate::Error;
85 #[cfg_attr(all(debug_assertions, feature = "debug_style_locations"), track_caller)]
86 fn try_from(other: String) -> crate::Result<StyleSource> {
87 let sheet = other.parse()?;
88 Ok(StyleSource {
89 inner: sheet,
90 manager: None,
91 #[cfg(all(debug_assertions, feature = "debug_style_locations"))]
92 location: Self::get_caller_location(),
93 })
94 }
95 }
96
97 impl<'a> TryFrom<&'a str> for StyleSource {
98 type Error = crate::Error;
99 #[cfg_attr(all(debug_assertions, feature = "debug_style_locations"), track_caller)]
100 fn try_from(other: &'a str) -> crate::Result<StyleSource> {
101 let sheet = other.parse()?;
102 Ok(StyleSource {
103 inner: sheet,
104 manager: None,
105 #[cfg(all(debug_assertions, feature = "debug_style_locations"))]
106 location: Self::get_caller_location(),
107 })
108 }
109 }
110
111 impl<'a> TryFrom<Cow<'a, str>> for StyleSource {
112 type Error = crate::Error;
113 #[cfg_attr(all(debug_assertions, feature = "debug_style_locations"), track_caller)]
114 fn try_from(other: Cow<'a, str>) -> crate::Result<StyleSource> {
115 let sheet = other.parse()?;
116 Ok(StyleSource {
117 inner: sheet,
118 manager: None,
119 #[cfg(all(debug_assertions, feature = "debug_style_locations"))]
120 location: Self::get_caller_location(),
121 })
122 }
123 }
124
125 impl FromStr for StyleSource {
126 type Err = crate::Error;
127 #[cfg_attr(all(debug_assertions, feature = "debug_style_locations"), track_caller)]
128 fn from_str(s: &str) -> Result<Self, Self::Err> {
129 s.try_into()
130 }
131 }
132}