Return `dyn Iterator` instead of `Vec` for `SupportedContent`

finchie
Nov 12, 2023, 12:28 PM
GKBSBSDY5XLAT7SDKAVFQI3PHFUHKHTJGGXZH5XWG2E45U7BSU5QC

Dependencies

  • [2] ZRGSUUIC Add MVP list support
  • [3] BA5Y6VSE Output Rust code using `syn`
  • [4] HEUMSBES Ouput use statements in generated file
  • [5] GYTRFADR Support Typst subdirectories
  • [6] BSJYWOYS Implement MVP Typst embedding
  • [7] HEIF2O2E Migrate from `pandoc` to `typst` for AST processing
  • [8] I5IZPMTH Handle empty expressions
  • [9] 4MMVEN5Y Move generated docs inside of parent `docs` module
  • [10] I3NG5A4W Add support for content sequences
  • [11] MPTQGIIJ Improve `typst_rust_gen` function names
  • [12] Q3IYM4WF Add MVP table support
  • [13] CQEA2ZDI Parse evaluated Typst code instead of AST
  • [14] YKL5NCLH Import items from `syn`
  • [15] 2N3KOCP7 Create MVP Pandoc->Rust compiler

Change contents

  • replacement in docs/test.typ at line 1
    [3.242][2.0:66]()
    List:
    - First
    - Second
    - Third
    Ordered:
    + First
    + Second
    + Third
    [3.242]
    [2.66]
    #show emph: it => {
    text(blue, it.body)
    }
  • replacement in docs/test.typ at line 5
    [2.67][2.67:160]()
    #enum(
    enum.item(1)[First step],
    enum.item(5)[Fifth step],
    enum.item(10)[Tenth step]
    )
    [2.67]
    This is _emphasized_ differently.
  • replacement in crates/typser/src/lib.rs at line 10
    [3.11][3.45:145]()
    token, AngleBracketedGenericArguments, ExprCall, ExprLit, ExprPath, ExprTuple, GenericArgument,
    [3.11]
    [3.145]
    token, AngleBracketedGenericArguments, ExprLit, ExprTuple, GenericArgument,
  • edit in crates/typser/src/lib.rs at line 241
    [3.10600][3.10600:10607](),[3.3254][3.3110:3113](),[3.10607][3.3110:3113](),[3.3110][3.3110:3113](),[3.3113][3.3953:4127](),[3.4127][3.3307:3316](),[3.3307][3.3307:3316](),[3.3316][3.1172:1212](),[3.1212][3.4163:4246](),[3.4163][3.4163:4246](),[3.3449][3.1947:1948](),[3.4246][3.1947:1948](),[3.1947][3.1947:1948](),[3.1948][3.3255:3366](),[3.3366][3.3543:3583](),[3.3543][3.3543:3583](),[3.3583][3.4247:4278](),[3.4278][3.3619:3767](),[3.3619][3.3619:3767](),[3.3767][3.1213:1244](),[3.1244][3.394:397](),[3.4305][3.394:397](),[3.397][3.1245:1354](),[3.1354][3.485:599](),[3.485][3.485:599](),[3.599][3.1355:1417]()
    })
    }
    fn path_segment(segment: &str) -> PathSegment {
    PathSegment {
    ident: Ident::new(segment, proc_macro2::Span::call_site()),
    arguments: PathArguments::None,
    }
    }
    fn expr_path(name: &str) -> syn::Expr {
    let mut segments: Punctuated<PathSegment, token::PathSep> = Punctuated::new();
    // The path should be elements::name
    // e.g. elements::h2
    segments.push(path_segment("elements"));
    segments.push(path_segment(name));
    let expr_path = ExprPath {
    attrs: Vec::new(),
    qself: None,
    path: syn::Path {
    leading_colon: None,
    segments,
    },
    };
    syn::Expr::Path(expr_path)
    }
    fn xilem_html_element(name: &str, expressions: Vec<syn::Expr>) -> syn::Expr {
    syn::Expr::Call(ExprCall {
    attrs: Vec::new(),
    func: Box::new(expr_path(name)),
    paren_token: token::Paren::default(),
    args: Punctuated::from_iter(expressions.into_iter()),
  • edit in crates/typser/src/content.rs at line 1
    [3.2939]
    [3.2940]
    use syn::punctuated::Punctuated;
    use syn::{token, ExprPath, PathSegment};
  • replacement in crates/typser/src/content.rs at line 8
    [3.3054][3.316:365]()
    use crate::{literal_string, xilem_html_element};
    [3.3054]
    [3.3103]
    use crate::literal_string;
  • replacement in crates/typser/src/content.rs at line 55
    [3.3696][3.792:839]()
    pub fn to_xilem(&self) -> Vec<syn::Expr> {
    [3.3696]
    [3.839]
    pub fn to_xilem(&self) -> Box<dyn Iterator<Item = syn::Expr> + '_> {
  • replacement in crates/typser/src/content.rs at line 57
    [3.860][3.860:1059]()
    SupportedContent::Sequence(sequence) => sequence
    .iter()
    .map(SupportedContent::to_xilem)
    .flatten()
    .collect::<Vec<_>>(),
    [3.860]
    [3.1059]
    SupportedContent::Sequence(sequence) => {
    Box::new(sequence.iter().map(SupportedContent::to_xilem).flatten())
    }
  • replacement in crates/typser/src/content.rs at line 62
    [3.1175][3.1175:1244]()
    vec![xilem_html_element("h1", body_text.to_xilem())]
    [3.1175]
    [3.1244]
    Box::new(Some(xilem_html_element("h1", body_text.to_xilem())).into_iter())
  • replacement in crates/typser/src/content.rs at line 65
    [3.1304][3.1304:1379]()
    vec![syn::Expr::Lit(literal_string(text.text().as_str()))]
    [3.1304]
    [3.1379]
    Box::new(Some(syn::Expr::Lit(literal_string(text.text().as_str()))).into_iter())
  • replacement in crates/typser/src/content.rs at line 67
    [3.1393][3.1393:1634](),[3.1634][3.881:983](),[3.881][3.881:983](),[3.983][3.1635:2885](),[3.2885][2.951:1047]()
    SupportedContent::Space(_space) => vec![syn::Expr::Lit(literal_string(" "))],
    SupportedContent::Table(table) => vec![xilem_html_element(
    "table",
    vec![syn::Expr::Tuple(syn::ExprTuple {
    attrs: Vec::new(),
    paren_token: syn::token::Paren::default(),
    elems: syn::punctuated::Punctuated::from_iter(
    table
    .children()
    .into_iter()
    .map(|content| SupportedContent::downcast(content))
    .map(|supported| supported.to_xilem())
    .flatten()
    .collect::<Vec<_>>()
    .chunks(2) // TODO: a slightly more sophisticated layout algorithm
    .map(|chunk| {
    xilem_html_element(
    "tr",
    vec![syn::Expr::Tuple(syn::ExprTuple {
    attrs: Vec::new(),
    paren_token: syn::token::Paren::default(),
    elems: syn::punctuated::Punctuated::from_iter(
    chunk.into_iter().map(|item| item.to_owned()),
    ),
    })],
    )
    }),
    ),
    })],
    )],
    SupportedContent::UnorderedList(list) => {
    vec![xilem_html_element(
    [3.1393]
    [2.1047]
    SupportedContent::Space(_space) => {
    Box::new(Some(syn::Expr::Lit(literal_string(" "))).into_iter())
    }
    SupportedContent::Table(table) => Box::new(
    Some(xilem_html_element(
    "table",
    Some(syn::Expr::Tuple(syn::ExprTuple {
    attrs: Vec::new(),
    paren_token: token::Paren::default(),
    elems: Punctuated::from_iter(
    table
    .children()
    .into_iter()
    .map(|content| SupportedContent::downcast(content))
    .map(|supported| supported.to_xilem().collect::<Vec<_>>())
    .flatten()
    .collect::<Vec<_>>()
    .chunks(2) // TODO: a slightly more sophisticated layout algorithm
    .map(|chunk| {
    xilem_html_element(
    "tr",
    vec![syn::Expr::Tuple(syn::ExprTuple {
    attrs: Vec::new(),
    paren_token: token::Paren::default(),
    elems: Punctuated::from_iter(
    chunk.into_iter().map(|item| item.to_owned()),
    ),
    })],
    )
    }),
    ),
    })),
    ))
    .into_iter(),
    ),
    SupportedContent::UnorderedList(list) => Box::new(
    Some(xilem_html_element(
  • replacement in crates/typser/src/content.rs at line 107
    [2.1175][2.1175:1313]()
    paren_token: syn::token::Paren::default(),
    elems: syn::punctuated::Punctuated::from_iter(
    [2.1175]
    [2.1313]
    paren_token: token::Paren::default(),
    elems: Punctuated::from_iter(
  • replacement in crates/typser/src/content.rs at line 112
    [2.1493][2.1493:1564]()
    .map(|supported| supported.to_xilem())
    [2.1493]
    [2.1564]
    .map(|supported| supported.to_xilem().collect::<Vec<_>>())
  • replacement in crates/typser/src/content.rs at line 116
    [2.1660][2.1660:1787]()
    )]
    }
    SupportedContent::OrderedList(list) => {
    vec![xilem_html_element(
    [2.1660]
    [2.1787]
    ))
    .into_iter(),
    ),
    SupportedContent::OrderedList(list) => Box::new(
    Some(xilem_html_element(
  • replacement in crates/typser/src/content.rs at line 124
    [2.1915][2.1915:2053]()
    paren_token: syn::token::Paren::default(),
    elems: syn::punctuated::Punctuated::from_iter(
    [2.1915]
    [2.2053]
    paren_token: token::Paren::default(),
    elems: Punctuated::from_iter(
  • replacement in crates/typser/src/content.rs at line 129
    [2.2233][2.2233:2304]()
    .map(|supported| supported.to_xilem())
    [2.2233]
    [2.2304]
    .map(|supported| supported.to_xilem().collect::<Vec<_>>())
  • replacement in crates/typser/src/content.rs at line 133
    [2.2400][2.2400:2533]()
    )]
    }
    SupportedContent::UnorderedListItem(item) => {
    vec![xilem_html_element(
    [2.2400]
    [2.2533]
    ))
    .into_iter(),
    ),
    SupportedContent::UnorderedListItem(item) => Box::new(
    Some(xilem_html_element(
  • replacement in crates/typser/src/content.rs at line 143
    [2.2744][2.2744:2890]()
    paren_token: syn::token::Paren::default(),
    elems: syn::punctuated::Punctuated::from_iter(
    [2.2744]
    [2.2890]
    paren_token: token::Paren::default(),
    elems: Punctuated::from_iter(
  • replacement in crates/typser/src/content.rs at line 149
    [2.3058][2.3058:3189]()
    )]
    }
    SupportedContent::OrderedListItem(item) => {
    vec![xilem_html_element(
    [2.3058]
    [2.3189]
    ))
    .into_iter(),
    ),
    SupportedContent::OrderedListItem(item) => Box::new(
    Some(xilem_html_element(
  • replacement in crates/typser/src/content.rs at line 159
    [2.3400][2.3400:3546]()
    paren_token: syn::token::Paren::default(),
    elems: syn::punctuated::Punctuated::from_iter(
    [2.3400]
    [2.3546]
    paren_token: token::Paren::default(),
    elems: Punctuated::from_iter(
  • replacement in crates/typser/src/content.rs at line 165
    [2.3714][2.3714:3856]()
    )]
    }
    SupportedContent::ParagraphBreak(_parahraph_break) => {
    vec![xilem_html_element(
    [2.3714]
    [2.3856]
    ))
    .into_iter(),
    ),
    SupportedContent::ParagraphBreak(_parahraph_break) => Box::new(
    Some(xilem_html_element(
  • replacement in crates/typser/src/content.rs at line 173
    [2.3984][2.3984:4118]()
    paren_token: syn::token::Paren::default(),
    elems: syn::punctuated::Punctuated::new(),
    [2.3984]
    [2.4118]
    paren_token: token::Paren::default(),
    elems: Punctuated::new(),
  • replacement in crates/typser/src/content.rs at line 176
    [2.4143][2.4143:4176]()
    )]
    }
    [2.4143]
    [3.2885]
    ))
    .into_iter(),
    ),
  • edit in crates/typser/src/content.rs at line 180
    [3.2895]
    [3.4166]
    }
    }
    fn path_segment(segment: &str) -> syn::PathSegment {
    syn::PathSegment {
    ident: syn::Ident::new(segment, proc_macro2::Span::call_site()),
    arguments: syn::PathArguments::None,
  • edit in crates/typser/src/content.rs at line 189
    [3.4174]
    fn expr_path(name: &str) -> syn::Expr {
    let mut segments: Punctuated<PathSegment, token::PathSep> = Punctuated::new();
    // The path should be elements::name
    // e.g. elements::h2
    segments.push(path_segment("elements"));
    segments.push(path_segment(name));
    let expr_path = ExprPath {
    attrs: Vec::new(),
    qself: None,
    path: syn::Path {
    leading_colon: None,
    segments,
    },
    };
    syn::Expr::Path(expr_path)
    }
    fn xilem_html_element(name: &str, expressions: impl IntoIterator<Item = syn::Expr>) -> syn::Expr {
    syn::Expr::Call(syn::ExprCall {
    attrs: Vec::new(),
    func: Box::new(expr_path(name)),
    paren_token: token::Paren::default(),
    args: Punctuated::from_iter(expressions.into_iter()),
    })
    }