fix(Patches): Handle more types when serializing patches
@@ -521,28 +521,32 @@ impl Operation {
BlockContent
// Child types of the above
- CodeError
ListItem
TableCaption
TableRow
TableCell
FigureCaption
-
- // Primitive and generics `Node` enum
+ CodeChunkCaption
Node
+ CodeError
+
+ // Primitives
String
Number
Integer
- u8
- i32
- f32
Boolean
Array
Object
Null
+
+ // Types used on some properties e.g. `Heading.depth`, `TableCell.rowspan`
+ u8
+ u32
+ i32
+ f32
);
- tracing::error!("Unhandled `value` type when serializing an `Operation`");
+ tracing::error!("Unhandled `value` type when serializing patch `Operation`");
"<unserialized type>".serialize(serializer)
}
@@ -592,15 +596,15 @@ impl Operation {
BlockContent
// Child types of the above
- CodeError
ListItem
TableCaption
TableRow
TableCell
FigureCaption
-
- // Primitive and generics `Node` enum
Node
+ CodeError
+
+ // Primitives
String
Number
Integer
@@ -610,6 +614,26 @@ impl Operation {
Null
);
+ // Convert an atomic (used in some struct properties e.g. `Heading.depth`). These
+ // don't usually need to be a HTML (they are handled differently) but for consistency
+ // we generate it anyway
+ macro_rules! to_html_atomic {
+ ($type:ty) => {
+ if let Some(node) = value.downcast_ref::<$type>() {
+ return node.to_string()
+ }
+ };
+ ($($type:ty)*) => {
+ $(to_html_atomic!($type);)*
+ }
+ }
+ to_html_atomic!(
+ u8
+ u32
+ i32
+ f32
+ );
+
// The value may be a JSON value (if this patch was sent from a client)
// In that case we want to deserialize it to one of the above types and
// then encode as HTML
@@ -629,13 +653,18 @@ impl Operation {
} else if let Ok(nodes) = serde_json::from_value::<Vec<ListItem>>(value.clone()) {
return nodes.to_html("", &context);
} else {
- tracing::error!("Unhandled JSON value type when generating HTML for `Operation`");
- return ["<span class=\"todo\">", &value.to_string(), "</span>"].concat();
+ tracing::error!(
+ "Unhandled JSON value type when generating HTML for patch `Operation`: {}",
+ value.to_string()
+ );
}
+ } else {
+ tracing::error!("Unhandled `value` type when generating HTML for patch `Operation`");
}
- tracing::error!("Unhandled value type when generating HTML for `Operation`");
- "<span class=\"todo\">TODO</span>".to_string()
+ // Send HTML that indicates error to developers (in addition to above tracing error)
+ // but is invisible to users.
+ "<meta name=\"error\" content=\"Unhandled patch value type\">".to_string()
}
/// Set the `html` field from the `value` field
