local task(obj) =
  local tags = std.get(obj, 'tags');
  {
    name: 'packages | Install ' + obj.name,
    'community.general.cargo': {
      name: obj.name,
      state: 'latest',
    },
  }
  + if std.isArray(tags) then { tags: [obj.name] + tags } else { tags: obj.name }
;

local taskRaw(obj) =
  local tags = std.get(obj, 'tags');
  local url = std.get(obj, 'url');
  local flags = std.get(obj, 'flags');
  {
    name: 'packages | Install ' + obj.name,
    command: {
      cmd:
        '/usr/bin/cargo install --locked --force '
        + (
          if std.isString(url) then
            '--git ' + url
          else obj.name
        )
        + (
          if std.isString(flags) then
            ' ' + flags
          else ''
        ),
      removes: '/usr/bin/cargo',
    },
  }
  + if std.isArray(tags) then { tags: [obj.name] + tags } else { tags: obj.name }
;

local packages =
  [
    { name: 'selene', tags: ['linters'] },
    { name: 'typos-cli', tags: ['linters'] },
    { name: 'pylyzer', tags: ['lspservers'] },
    { name: 'jinja-lsp', tags: ['lspservers'] },
    { name: 'cargo-spellcheck', tags: ['linters'] },
  ]
  + [
    { name: x }
    for x in [
      'kubernix',
      'thokr',
      'tickrs',
      'krabby',
      'fselect',
      'git-stack',
      'git-branch-stash',
      'mdbook-mermaid',
      'boringtun-cli',
      'genact',
      'cocogitto',
      'alass-cli',  // Used for mpv script https://github.com/joaquintorres/autosubsync-mpv
      'vtracer',
    ]
  ];

local packagesRaw =
  [
    { name: 'atsi', url: 'https://github.com/queer/atsi.git' },
    { name: 'languagetool-rust', flags: '--features full' },
    { name: 'qsv', flags: '--features all_full' },
    { name: 'faerber', url: 'https://github.com/nekowinston/faerber.git', flags: 'faerber' },
    { name: 'nerdfix', url: 'https://github.com/loichyan/nerdfix.git' },
    { name: 'nil', url: 'https://github.com/oxalica/nil.git', flags: 'nil', tags: ['lspservers'] },
    { name: 'nixpkgs-fmt', url: 'https://github.com/nix-community/nixpkgs-fmt.git', flags: 'nixpkgs-fmt', tags: ['linters'] },

    // taplo package from Alpine's repo doesn't enable "lsp" feature
    // Ref: https://gitlab.alpinelinux.org/alpine/aports/-/merge_requests/42225
    { name: 'taplo-cli', flags: '--features native-tls,lsp', tags: ['lspservers'] },
  ]
;

std.manifestYamlDoc(
  [taskRaw(x) for x in packagesRaw]
  + [task(x) for x in packages],
  quote_keys=false
)
