Repository: typst/typst
File: crates/typst-library/src/model/bibliography.rs (Rust)

Code before:
    #[typst_macros::time(name = "bibliography", span = self.span())]
    fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
        const COLUMN_GUTTER: Em = Em::new(0.65);
        const INDENT: Em = Em::new(1.5);

        let span = self.span();

        let mut seq = vec![];
        if let Some(title) = self.title(styles).unwrap_or_else(|| {
            Some(TextElem::packed(Self::local_name_in(styles)).spanned(span))
        }) {
            seq.push(
                HeadingElem::new(title)
                    .with_depth(NonZeroUsize::ONE)
                    .pack()
                    .spanned(span),
            );
        }

        let works = Works::generate(engine).at(span)?;
        let references = works
            .references
            .as_ref()
            .ok_or_else(|| {
                eco_format!(
                    "{:?} CSL style is not suitable for bibliographies",
                    self.style(styles).into_value()
                )
            })
            .at(span)?;

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

            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),

Review comment (bug):
Using `{:?}` debug formatting here is problematic as it fill use Rust-style formatting (e.g. `Bytes(10)`). I think we should instead only print a name if we know it (i.e. for named styles via `style.names()`). I would also reorder this, so that the final message is 
```rust
eco_format!("CSL style `{}` is not suitable for bibliographies", name.repr())
```
If there is no name, we can keep the old message.

Could you also add tests for both cases?

Code after:
    #[typst_macros::time(name = "bibliography", span = self.span())]
    fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
        const COLUMN_GUTTER: Em = Em::new(0.65);
        const INDENT: Em = Em::new(1.5);

        let span = self.span();

        let mut seq = vec![];
        if let Some(title) = self.title(styles).unwrap_or_else(|| {
            Some(TextElem::packed(Self::local_name_in(styles)).spanned(span))
        }) {
            seq.push(
                HeadingElem::new(title)
                    .with_depth(NonZeroUsize::ONE)
                    .pack()
                    .spanned(span),
            );
        }

        let works = Works::generate(engine).at(span)?;
        let references = works
            .references
            .as_ref()
            .ok_or_else(|| match self.style(styles).source {
                CslSource::Named(style) => eco_format!(
                    "CSL style \"{}\" is not suitable for bibliographies",
                    style.display_name()
                ),
                CslSource::Normal(..) => {
                    "CSL style is not suitable for bibliographies".into()
                }
            })
            .at(span)?;

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

            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()]))