| 1 | |
| 2 | |
| 3 | use std::collections::HashMap; |
| 4 | use std::hash::BuildHasher; |
| 5 | |
| 6 | use loro::LoroValue; |
| 7 | |
| 8 | use crate::tree::value_str; |
| 9 | |
| 10 | |
| 11 | #[derive(PartialEq, Clone, Default, Debug)] |
| 12 | pub struct Marks { |
| 13 | pub strong: bool, |
| 14 | pub em: bool, |
| 15 | pub code: bool, |
| 16 | pub link: Option<(String, Option<String>)>, |
| 17 | } |
| 18 | |
| 19 | impl Marks { |
| 20 | |
| 21 | |
| 22 | pub fn from_attrs<S: BuildHasher>(attrs: &HashMap<String, LoroValue, S>) -> Marks { |
| 23 | let on = |k: &str| attrs.get(k).is_some_and(|v| !matches!(v, LoroValue::Null)); |
| 24 | let link = attrs.get("link").map(|v| { |
| 25 | let href = v.get_by_key("href").and_then(value_str).unwrap_or_default(); |
| 26 | let title = v |
| 27 | .get_by_key("title") |
| 28 | .and_then(value_str) |
| 29 | .filter(|t| !t.is_empty()); |
| 30 | (href, title) |
| 31 | }); |
| 32 | Marks { |
| 33 | strong: on("strong"), |
| 34 | em: on("em"), |
| 35 | code: on("code"), |
| 36 | link, |
| 37 | } |
| 38 | } |
| 39 | } |