RobertDober / earmark_parser

The Markdown to AST part of Earmark.
Apache License 2.0
68 stars 26 forks source link

Make as_ast accept any struct as options param #2

Closed RobertDober closed 4 years ago

RobertDober commented 4 years ago

This needs to be implemented so that Earmark.as_ast can be replaced by EarmarkParser.as_ast transparently by client code.

Here is what happens on a call like EarmarkParser.as_ast(..., %Earmark.Options{...})

iex(2)> EarmarkParser.as_ast("", %EarmarkOptions{})
** (CompileError) iex:2: EarmarkOptions.__struct__/1 is undefined, cannot expand struct EarmarkOptions. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code
    (stdlib 3.13) lists.erl:1354: :lists.mapfoldl/3
    (stdlib 3.13) lists.erl:1355: :lists.mapfoldl/3
iex(2)> EarmarkParser.as_ast("", %Earmark.Options{})
** (FunctionClauseError) no function clause matching in EarmarkParser.Parser.parse_markdown/2    

    The following arguments were given to EarmarkParser.Parser.parse_markdown/2:

        # 1
        [""]

        # 2
        %Earmark.Options{
          breaks: false,
          code_class_prefix: nil,
          do_smartypants: nil,
          file: "<no file>",
          footnote_offset: 1,
          footnotes: false,
          gfm: true,
          gfm_tables: false,
          line: 1,
          mapper: &Earmark.pmap/2,
          mapper_with_timeout: &Earmark.pmap/3,
          messages: [],
          pedantic: false,
          plugins: %{},
          pure_links: true,
          renderer: Earmark.HtmlRenderer,
          smartypants: true,
          timeout: nil
        }

    Attempted function clauses (showing 2 out of 2):

        def parse_markdown(lines, options = %EarmarkParser.Options{}) when is_list(lines)
        def parse_markdown(lines, options) when is_binary(lines)

    lib/earmark_parser/parser.ex:25: EarmarkParser.Parser.parse_markdown/2
    lib/earmark_parser.ex:339: EarmarkParser._as_ast/2
    lib/earmark_parser.ex:324: EarmarkParser.as_ast/2
RobertDober commented 4 years ago

Therefor as_ast fails to convert %Earmark.Options{} into %EarmarkParser.Options{}