Repository: typst/typst
File: crates/typst-layout/src/rules.rs (Rust)

Code before:
    };

    let loc = elem.element_location().at(span)?;
    Ok(block.linked(Destination::Location(loc)))
};

const REF_RULE: ShowFn<RefElem> = |elem, engine, styles| elem.realize(engine, styles);

const CITE_GROUP_RULE: ShowFn<CiteGroup> = |elem, engine, _| {
    let (_, content) = elem.realize(engine)?;
    Ok(content)
};

const BIBLIOGRAPHY_RULE: ShowFn<BibliographyElem> = |elem, engine, styles| {
    const COLUMN_GUTTER: Em = Em::new(0.65);
    const INDENT: Em = Em::new(1.5);

    let span = elem.span();

    let mut seq = vec![];
    if let Some(title) = elem.realize_title(styles) {
        seq.push(title);
    }

    let works = elem.realize_works(engine, styles)?;
    let references = works.references.as_ref().unwrap();

    if references.iter().any(|(prefix, _)| prefix.is_some()) {
        let row_gutter = styles.get(ParElem::spacing);

        let mut cells = vec![];
        for (prefix, reference) in references {
            cells.push(GridChild::Item(GridItem::Cell(
                Packed::new(GridCell::new(prefix.clone().unwrap_or_default()))
                    .spanned(span),
            )));
            cells.push(GridChild::Item(GridItem::Cell(
                Packed::new(GridCell::new(reference.clone())).spanned(span),
            )));
        }
        seq.push(
            GridElem::new(cells)
                .with_columns(TrackSizings(smallvec![Sizing::Auto; 2]))
                .with_column_gutter(TrackSizings(smallvec![COLUMN_GUTTER.into()]))
                .with_row_gutter(TrackSizings(smallvec![row_gutter.into()]))
                .pack()
                .spanned(span),
        );
    } else {
        for (_, reference) in references {
            let realized = reference.clone();

Review comment (suggestion):
This is a bit of an anti-pattern as it doesn't seem, at first, evident that `realize_works` would guarantee that `references` is `Some`.

The following would be more idiomatic (with the appropriate changes to `realize_works`).

```suggestion
    let (works, references) = elem.realize_works(engine, styles)?;
```

Code after:
        let body = prefix.unwrap_or_default() + inner;
        BlockElem::new()
            .with_body(Some(BlockBody::Content(body)))
            .pack()
            .spanned(span)
    } else {
        elem.indented(engine, context, span, prefix, inner, Em::new(0.5).into())?
    };

    let loc = elem.element_location().at(span)?;
    Ok(block.linked(Destination::Location(loc)))
};

const REF_RULE: ShowFn<RefElem> = |elem, engine, styles| elem.realize(engine, styles);

const CITE_GROUP_RULE: ShowFn<CiteGroup> = |elem, engine, _| elem.realize(engine);

const BIBLIOGRAPHY_RULE: ShowFn<BibliographyElem> = |elem, engine, styles| {
    const COLUMN_GUTTER: Em = Em::new(0.65);
    const INDENT: Em = Em::new(1.5);

    let span = elem.span();

    let mut seq = vec![];
    seq.extend(elem.realize_title(styles));

    let works = Works::generate(engine).at(span)?;
    let references = works.references(elem, styles)?;

    if references.iter().any(|(prefix, ..)| prefix.is_some()) {
        let row_gutter = styles.get(ParElem::spacing);

        let mut cells = vec![];
        for (prefix, reference, loc) in references {
            cells.push(GridChild::Item(GridItem::Cell(
                Packed::new(GridCell::new(
                    prefix.clone().unwrap_or_default().located(*loc),
                ))
                .spanned(span),
            )));
            cells.push(GridChild::Item(GridItem::Cell(
                Packed::new(GridCell::new(reference.clone())).spanned(span),
            )));
        }
        seq.push(
            GridElem::new(cells)
                .with_columns(TrackSizings(smallvec![Sizing::Auto; 2]))
                .with_column_gutter(TrackSizings(smallvec![COLUMN_GUTTER.into()]))
                .with_row_gutter(TrackSizings(smallvec![row_gutter.into()]))
                .pack()
                .spanned(span),