| 1 | use serde_json::Value; |
| 2 | |
| 3 | use crate::adaptive_card::{ |
| 4 | CardAction, CardElement, CardItem, TextColor, parse_items, parse_select_action, |
| 5 | parse_text_color, string_field, |
| 6 | }; |
| 7 | use crate::card_layout::ContainerLayout; |
| 8 | |
| 9 | const ICON_NAME_SEPARATOR: char = ','; |
| 10 | const MAX_PROGRESS: f32 = 100.; |
| 11 | |
| 12 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 13 | pub struct CardCodeBlock { |
| 14 | pub code: String, |
| 15 | pub language: Option<String>, |
| 16 | pub start_line: usize, |
| 17 | } |
| 18 | |
| 19 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 20 | pub enum BadgeStyle { |
| 21 | Default, |
| 22 | Accent, |
| 23 | Good, |
| 24 | Attention, |
| 25 | Warning, |
| 26 | Subtle, |
| 27 | Informative, |
| 28 | } |
| 29 | |
| 30 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 31 | pub enum BadgeAppearance { |
| 32 | Filled, |
| 33 | Tint, |
| 34 | } |
| 35 | |
| 36 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 37 | pub enum BadgeShape { |
| 38 | Square, |
| 39 | Rounded, |
| 40 | Circular, |
| 41 | } |
| 42 | |
| 43 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 44 | pub enum BadgeSize { |
| 45 | Medium, |
| 46 | Large, |
| 47 | ExtraLarge, |
| 48 | } |
| 49 | |
| 50 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 51 | pub enum IconPosition { |
| 52 | Before, |
| 53 | After, |
| 54 | } |
| 55 | |
| 56 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 57 | pub struct CardBadge { |
| 58 | pub text: String, |
| 59 | pub icon: Option<String>, |
| 60 | pub style: BadgeStyle, |
| 61 | pub appearance: BadgeAppearance, |
| 62 | pub shape: BadgeShape, |
| 63 | pub size: BadgeSize, |
| 64 | pub icon_position: IconPosition, |
| 65 | pub tooltip: Option<String>, |
| 66 | } |
| 67 | |
| 68 | #[derive(Debug, Clone, PartialEq)] |
| 69 | pub struct CardProgressBar { |
| 70 | pub value: Option<f32>, |
| 71 | pub color: TextColor, |
| 72 | } |
| 73 | |
| 74 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 75 | pub enum RingSize { |
| 76 | Tiny, |
| 77 | Small, |
| 78 | Medium, |
| 79 | Large, |
| 80 | } |
| 81 | |
| 82 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 83 | pub enum LabelPosition { |
| 84 | Before, |
| 85 | After, |
| 86 | Above, |
| 87 | Below, |
| 88 | } |
| 89 | |
| 90 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 91 | pub struct CardProgressRing { |
| 92 | pub label: Option<String>, |
| 93 | pub label_position: LabelPosition, |
| 94 | pub size: RingSize, |
| 95 | } |
| 96 | |
| 97 | #[derive(Debug, Clone, PartialEq)] |
| 98 | pub struct CardCompoundButton { |
| 99 | pub icon: Option<String>, |
| 100 | pub title: String, |
| 101 | pub description: Option<String>, |
| 102 | pub badge: Option<String>, |
| 103 | pub select_action: Option<CardAction>, |
| 104 | } |
| 105 | |
| 106 | #[derive(Debug, Clone, PartialEq)] |
| 107 | pub struct CardCarousel { |
| 108 | pub pages: Vec<CardCarouselPage>, |
| 109 | pub initial_page: usize, |
| 110 | } |
| 111 | |
| 112 | #[derive(Debug, Clone, PartialEq)] |
| 113 | pub struct CardCarouselPage { |
| 114 | pub items: Vec<CardItem>, |
| 115 | pub layout: ContainerLayout, |
| 116 | pub select_action: Option<CardAction>, |
| 117 | } |
| 118 | |
| 119 | fn non_blank(value: &Value, key: &str) -> Option<String> { |
| 120 | string_field(value, key) |
| 121 | .map(str::trim) |
| 122 | .filter(|text| !text.is_empty()) |
| 123 | .map(str::to_owned) |
| 124 | } |
| 125 | |
| 126 | fn icon_name(value: &Value, key: &str) -> Option<String> { |
| 127 | let name = string_field(value, key)? |
| 128 | .split(ICON_NAME_SEPARATOR) |
| 129 | .next()?; |
| 130 | let name = name.trim(); |
| 131 | (!name.is_empty()).then(|| name.to_owned()) |
| 132 | } |
| 133 | |
| 134 | pub(crate) fn parse_code_block(value: &Value) -> Option<CardElement> { |
| 135 | let code = string_field(value, "codeSnippet").filter(|code| !code.trim().is_empty())?; |
| 136 | Some(CardElement::CodeBlock(CardCodeBlock { |
| 137 | code: code.trim_end_matches('\n').to_owned(), |
| 138 | language: non_blank(value, "language"), |
| 139 | start_line: value |
| 140 | .get("startLineNumber") |
| 141 | .and_then(Value::as_u64) |
| 142 | .map_or(1, |line| line as usize), |
| 143 | })) |
| 144 | } |
| 145 | |
| 146 | pub(crate) fn parse_badge(value: &Value) -> Option<CardElement> { |
| 147 | let text = non_blank(value, "text"); |
| 148 | let icon = icon_name(value, "icon"); |
| 149 | if text.is_none() && icon.is_none() { |
| 150 | return None; |
| 151 | } |
| 152 | let lowered = |key: &str| string_field(value, key).map(str::to_ascii_lowercase); |
| 153 | Some(CardElement::Badge(CardBadge { |
| 154 | text: text.unwrap_or_default(), |
| 155 | icon, |
| 156 | style: match lowered("style").as_deref() { |
| 157 | Some("accent") => BadgeStyle::Accent, |
| 158 | Some("good") => BadgeStyle::Good, |
| 159 | Some("attention") => BadgeStyle::Attention, |
| 160 | Some("warning") => BadgeStyle::Warning, |
| 161 | Some("subtle") => BadgeStyle::Subtle, |
| 162 | Some("informative") => BadgeStyle::Informative, |
| 163 | _ => BadgeStyle::Default, |
| 164 | }, |
| 165 | appearance: match lowered("appearance").as_deref() { |
| 166 | Some("tint") => BadgeAppearance::Tint, |
| 167 | _ => BadgeAppearance::Filled, |
| 168 | }, |
| 169 | shape: match lowered("shape").as_deref() { |
| 170 | Some("square") => BadgeShape::Square, |
| 171 | Some("rounded") => BadgeShape::Rounded, |
| 172 | _ => BadgeShape::Circular, |
| 173 | }, |
| 174 | size: match lowered("size").as_deref() { |
| 175 | Some("large") => BadgeSize::Large, |
| 176 | Some("extralarge") => BadgeSize::ExtraLarge, |
| 177 | _ => BadgeSize::Medium, |
| 178 | }, |
| 179 | icon_position: match lowered("iconPosition").as_deref() { |
| 180 | Some("after") => IconPosition::After, |
| 181 | _ => IconPosition::Before, |
| 182 | }, |
| 183 | tooltip: non_blank(value, "tooltip"), |
| 184 | })) |
| 185 | } |
| 186 | |
| 187 | pub(crate) fn parse_progress_bar(value: &Value) -> Option<CardElement> { |
| 188 | Some(CardElement::ProgressBar(CardProgressBar { |
| 189 | value: value |
| 190 | .get("value") |
| 191 | .and_then(Value::as_f64) |
| 192 | .map(|progress| (progress as f32).clamp(0., MAX_PROGRESS)), |
| 193 | color: parse_text_color(string_field(value, "color")), |
| 194 | })) |
| 195 | } |
| 196 | |
| 197 | pub(crate) fn parse_progress_ring(value: &Value) -> Option<CardElement> { |
| 198 | let lowered = |key: &str| string_field(value, key).map(str::to_ascii_lowercase); |
| 199 | Some(CardElement::ProgressRing(CardProgressRing { |
| 200 | label: non_blank(value, "label"), |
| 201 | label_position: match lowered("labelPosition").as_deref() { |
| 202 | Some("before") => LabelPosition::Before, |
| 203 | Some("above") => LabelPosition::Above, |
| 204 | Some("below") => LabelPosition::Below, |
| 205 | _ => LabelPosition::After, |
| 206 | }, |
| 207 | size: match lowered("size").as_deref() { |
| 208 | Some("tiny") => RingSize::Tiny, |
| 209 | Some("small") => RingSize::Small, |
| 210 | Some("large") => RingSize::Large, |
| 211 | _ => RingSize::Medium, |
| 212 | }, |
| 213 | })) |
| 214 | } |
| 215 | |
| 216 | pub(crate) fn parse_compound_button(value: &Value) -> Option<CardElement> { |
| 217 | Some(CardElement::CompoundButton(CardCompoundButton { |
| 218 | icon: icon_name(value, "icon"), |
| 219 | title: non_blank(value, "title")?, |
| 220 | description: non_blank(value, "description"), |
| 221 | badge: non_blank(value, "badge"), |
| 222 | select_action: parse_select_action(value), |
| 223 | })) |
| 224 | } |
| 225 | |
| 226 | pub(crate) fn parse_carousel(value: &Value) -> Option<CardElement> { |
| 227 | let pages: Vec<CardCarouselPage> = value |
| 228 | .get("pages")? |
| 229 | .as_array()? |
| 230 | .iter() |
| 231 | .filter_map(|page| { |
| 232 | let items = parse_items(page.get("items")); |
| 233 | (!items.is_empty()).then(|| CardCarouselPage { |
| 234 | items, |
| 235 | layout: ContainerLayout::parse(page), |
| 236 | select_action: parse_select_action(page), |
| 237 | }) |
| 238 | }) |
| 239 | .collect(); |
| 240 | let initial_page = value |
| 241 | .get("initialPageIndex") |
| 242 | .and_then(Value::as_u64) |
| 243 | .map_or(0, |index| index as usize) |
| 244 | .min(pages.len().saturating_sub(1)); |
| 245 | (!pages.is_empty()).then_some(CardElement::Carousel(CardCarousel { |
| 246 | pages, |
| 247 | initial_page, |
| 248 | })) |
| 249 | } |