Skip to main content

Crate serde_markdown

Crate serde_markdown 

Source
Expand description

Serde format crate for Markdown documents with a structured fields fence and ordered body sections.

A root struct’s non-body fields map to an optional first YAML, JSON, or TOML block (fenced or bare). Fields listed in Markdown::BODY_FIELDS map to body sections split on top-level ---. Serialize defaults to a labeled yaml fence; JSON and TOML only when the caller passes Format::Json / Format::Toml. Default crate features are yaml, json, toml, and derive. Feature buffa is off by default. A fence language not in the compiled features is ErrorKind::FormatDisabled. Documents are UTF-8: from_slice fails with ErrorKind::Syntax on invalid bytes.

Feature tokio (off by default) adds the module serde_markdown::tokio: the async twin of every function here, under the same name. It runs the same algorithm in steps and yields to the scheduler between them when the task’s cooperative budget is spent; no thread is spawned.

Google well-known types in the fence or body use that field type’s proto3 JSON serde. This crate does not parse RFC 3339 itself. Callers who pack protobuf Any must install a type registry (buffa_types::register_wkt_types) the same way they do for serde_json.

§Example

use serde::{Deserialize, Serialize};
use serde_markdown::{from_str, to_string, Markdown};

#[derive(Debug, PartialEq, Serialize, Deserialize, Markdown)]
struct Page {
    title: String,
    #[markdown(body)]
    body: String,
}

let page = Page {
    title: "Hi".into(),
    body: "Hello".into(),
};
let md = to_string(&page)?;
let back: Page = from_str(&md)?;
assert_eq!(page, back);

Modules§

buffa
Buffa codegen attributes for (markdown.body) = true.
tokio
The async form of the crate, behind the feature tokio.

Structs§

Error
Failure while reading or writing a Markdown document.

Enums§

ErrorKind
Classification for Error. Match this; do not parse Display. New kinds may be added, so a match needs a _ arm.
FieldsLayout
Whether the serialized fields block is a fenced code block or a bare mapping.
Format
Fence language used when serializing the fields block.

Constants§

PROTO_INCLUDE
Directory containing markdown/options.proto for include paths.

Traits§

Markdown
Marks which Serde field names of a root struct are Markdown body sections.

Functions§

from_reader
Deserialize a Markdown document by reading reader to the end.
from_slice
Deserialize a Markdown document from UTF-8 bytes.
from_str
Deserialize a Markdown document from s.
to_string
Serialize value as a fenced YAML Markdown document.
to_string_with
Serialize value with an explicit fence language and fields layout.
to_string_with_format
Serialize value as a fenced Markdown document in format.
to_vec
Serialize value as UTF-8 bytes of a fenced YAML Markdown document.
to_writer
Write value as a fenced YAML Markdown document to writer.

Derive Macros§

Markdown
Implements serde_markdown::Markdown for a named struct.