Skip to main content

to_osd

Function to_osd 

Source
pub fn to_osd(
    schema: &Schema,
    indent: Option<usize>,
) -> Result<String, WriteError>
Expand description

Serialize a Schema back to OSD text (canonical output, spec section 5.9). Ported from Python’s osd.to_osd/_record/_field/_card/_type.

indent: None renders a single-line, machine-oriented form (record definitions and the trailing root statement joined by spaces, fields joined by , , no trailing comma) instead of the default pretty-printed, indented form – mirroring write_oml/write_json’s own indent: None convention. A Some(n) sets the pretty-mode indent width in spaces. Both forms round-trip through parse_schema.

Labels are always quoted and escaped per OSD-15: a backslash is written \\ and a double quote \", and nothing else is escaped.

§Errors

OSD-14: a field label containing a C0 control character (U+0000 to U+001F, tab and newline included) has no OSD spelling – section 5.3.1 bans the raw character in a string body and OSD’s unescaping is weak, so no escape can stand in for it – and the write fails with a WriteError carrying code write.unsupported-value (spec section 8.3.9, E-26), unconditionally. The diagnostic’s path is the Schema path of the record holding the field (R, never R.<label>, since section 8.4 has no way to quote a label in a path). The first offending record in declaration order is reported. Such a schema still travels as OSD-OML, whose \u00XX escape is real.

OSD-16 / S-24: a field with max = 0 ([0,0], representable in the model but rejected by every text reader) likewise fails with write.unsupported-value at the record path R. [crate::ops::prune] removes such fields from every record it rebuilds (it keeps an unsatisfiable root intact), so prune before writing. When both a C0 label and a max = 0 field are present, which is reported is unspecified.