| 1 | use serde_json::Value; |
| 2 | |
| 3 | use crate::adaptive_card::{ |
| 4 | ContainerStyle, VerticalAlignment, bool_field, is_supported_url, parse_container_style, |
| 5 | parse_vertical_alignment, pixel_field, string_field, |
| 6 | }; |
| 7 | |
| 8 | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] |
| 9 | pub enum HorizontalAlignment { |
| 10 | #[default] |
| 11 | Left, |
| 12 | Center, |
| 13 | Right, |
| 14 | } |
| 15 | |
| 16 | #[derive(Debug, Clone, Default, PartialEq)] |
| 17 | pub struct ContainerLayout { |
| 18 | pub style: ContainerStyle, |
| 19 | pub min_height: Option<f32>, |
| 20 | pub vertical_alignment: VerticalAlignment, |
| 21 | pub background_image: Option<String>, |
| 22 | pub bleed: bool, |
| 23 | pub rtl: bool, |
| 24 | } |
| 25 | |
| 26 | impl ContainerLayout { |
| 27 | pub fn parse(value: &Value) -> ContainerLayout { |
| 28 | ContainerLayout { |
| 29 | style: parse_container_style(string_field(value, "style")), |
| 30 | min_height: pixel_field(value, "minHeight"), |
| 31 | vertical_alignment: parse_vertical_alignment(string_field( |
| 32 | value, |
| 33 | "verticalContentAlignment", |
| 34 | )), |
| 35 | background_image: parse_background_image(value), |
| 36 | bleed: bool_field(value, "bleed"), |
| 37 | rtl: bool_field(value, "rtl"), |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | pub(crate) fn parse_horizontal_alignment(value: Option<&str>) -> Option<HorizontalAlignment> { |
| 43 | match value.map(str::to_ascii_lowercase).as_deref() { |
| 44 | Some("left") => Some(HorizontalAlignment::Left), |
| 45 | Some("center") => Some(HorizontalAlignment::Center), |
| 46 | Some("right") => Some(HorizontalAlignment::Right), |
| 47 | _ => None, |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | fn parse_background_image(value: &Value) -> Option<String> { |
| 52 | let image = value.get("backgroundImage")?; |
| 53 | let url = image.as_str().or_else(|| string_field(image, "url"))?; |
| 54 | is_supported_url(url).then(|| url.to_owned()) |
| 55 | } |
| 56 | |
| 57 | /// `#RRGGBB` or `#AARRGGBB` as `0xRRGGBBAA`. |
| 58 | pub(crate) fn parse_color_hex(value: &str) -> Option<u32> { |
| 59 | let digits = value.trim().strip_prefix('#')?; |
| 60 | let parsed = u32::from_str_radix(digits, 16).ok()?; |
| 61 | match digits.len() { |
| 62 | 6 => Some(parsed << 8 | 0xff), |
| 63 | 8 => Some(parsed.rotate_left(8)), |
| 64 | _ => None, |
| 65 | } |
| 66 | } |