summaryrefslogtreecommitdiffstats
path: root/lua/conform/formatters/rustfmt.lua
blob: 62d2d78197f4d7ea9a20a511b6effd1ce096f54a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
---@param manifest string
---@return nil|string
local function parse_edition(manifest)
  for line in io.lines(manifest) do
    if line:match("^edition *=") then
      local edition = line:match("%d+")
      if edition then
        return edition
      end
    end
  end
end

---@type conform.FileFormatterConfig
return {
  meta = {
    url = "https://github.com/rust-lang/rustfmt",
    description = "A tool for formatting rust code according to style guidelines.",
  },
  command = "rustfmt",
  options = {
    -- The default edition of Rust to use when no Cargo.toml file is found
    default_edition = "2021",
  },
  args = function(self, ctx)
    local args = { "--emit=stdout" }
    local edition
    local manifest = vim.fs.find("Cargo.toml", { upward = true, path = ctx.dirname })[1]
    if manifest then
      edition = parse_edition(manifest)
    end
    if not edition then
      edition = self.options.default_edition
    end
    table.insert(args, "--edition=" .. edition)

    return args
  end,
}