# Copyright 2025 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# Autogenerated by pycargoebuild 0.15.0

EAPI=8

RUST_MIN_VER="1.89.0"

CRATES=" "

TYPST_HASH="28af048d65cbb7a5fc78f3ca1a20c9e885384a62"
TYPST_TS_HASH="c078ddf869d9438b36e1cacb65100e4514780dc1"

declare -A GIT_CRATES=(
	[docx-rs]="https://github.com/Myriad-Dreamin/docx-rs;db49a729f68dbdb9e8e91857fbb1c3d414209871;docx-rs-%commit%/docx-core"
	[reflexo-typst2vec]="https://github.com/Myriad-Dreamin/typst.ts;${TYPST_TS_HASH};typst.ts-%commit%/crates/conversion/typst2vec"
	[reflexo-typst]="https://github.com/Myriad-Dreamin/typst.ts;${TYPST_TS_HASH};typst.ts-%commit%/crates/reflexo-typst"
	[reflexo-vec2svg]="https://github.com/Myriad-Dreamin/typst.ts;${TYPST_TS_HASH};typst.ts-%commit%/crates/conversion/vec2svg"
	[reflexo]="https://github.com/Myriad-Dreamin/typst.ts;${TYPST_TS_HASH};typst.ts-%commit%/crates/reflexo"
	[typst-eval]="https://github.com/Myriad-Dreamin/typst;${TYPST_HASH};typst-%commit%/crates/typst-eval"
	[typst-html]="https://github.com/Myriad-Dreamin/typst;${TYPST_HASH};typst-%commit%/crates/typst-html"
	[typst-layout]="https://github.com/Myriad-Dreamin/typst;${TYPST_HASH};typst-%commit%/crates/typst-layout"
	[typst-library]="https://github.com/Myriad-Dreamin/typst;${TYPST_HASH};typst-%commit%/crates/typst-library"
	[typst-macros]="https://github.com/Myriad-Dreamin/typst;${TYPST_HASH};typst-%commit%/crates/typst-macros"
	[typst-pdf]="https://github.com/Myriad-Dreamin/typst;${TYPST_HASH};typst-%commit%/crates/typst-pdf"
	[typst-realize]="https://github.com/Myriad-Dreamin/typst;${TYPST_HASH};typst-%commit%/crates/typst-realize"
	[typst-render]="https://github.com/Myriad-Dreamin/typst;${TYPST_HASH};typst-%commit%/crates/typst-render"
	[typst-svg]="https://github.com/Myriad-Dreamin/typst;${TYPST_HASH};typst-%commit%/crates/typst-svg"
	[typst-syntax]="https://github.com/Myriad-Dreamin/typst;${TYPST_HASH};typst-%commit%/crates/typst-syntax"
	[typst-timing]="https://github.com/Myriad-Dreamin/typst;${TYPST_HASH};typst-%commit%/crates/typst-timing"
	[typst-utils]="https://github.com/Myriad-Dreamin/typst;${TYPST_HASH};typst-%commit%/crates/typst-utils"
	[typst]="https://github.com/Myriad-Dreamin/typst;${TYPST_HASH};typst-%commit%/crates/typst"
	[typstfmt]="https://github.com/Myriad-Dreamin/typstfmt;cdfe44ed065a90d80040c3b29dee7ed431a710ee;typstfmt-%commit%"
)

inherit cargo shell-completion

DESCRIPTION="An integrated language service for Typst."
HOMEPAGE="https://github.com/Myriad-Dreamin/tinymist"
SRC_URI="
	https://github.com/Myriad-Dreamin/tinymist/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz
	https://github.com/TimovVeen/tinymist/releases/download/v${PV}/tinymist-${PV}-crates.tar.xz
	${CARGO_CRATE_URIS}
"

LICENSE="Apache-2.0"
# Dependent crate licenses
LICENSE+="
	Apache-2.0 Apache-2.0-with-LLVM-exceptions BSD-2 BSD CC0-1.0
	CDLA-Permissive-2.0 EUPL-1.2 ISC LGPL-3+ MIT MPL-2.0 Unicode-3.0
	ZLIB
"
SLOT="0"
KEYWORDS="~amd64"

# e2e tests hang
RESTRICT="test"

QA_FLAGS_IGNORED="usr/bin/tinymist"

src_prepare() {
	default

	# Taken from app-laptop/framework_tool::gentoo,
	# adapted to also remove the tag field
	# Upstream uses [patch] on some dependencies in Cargo.toml,
	# which are not patched by cargo.eclass's ${ECARGO_HOME}/config
	local crate commit crate_uri crate_dir
	local -a sed_scripts
	for crate in "${!GIT_CRATES[@]}"; do
		IFS=';' read -r \
			crate_uri commit crate_dir <<< "${GIT_CRATES[${crate}]}"
		sed_scripts+=(
			"s|^(${crate}[[:space:]]*=[[:space:]]*[{].*)([[:space:]]*git[[:space:]]*=[[:space:]]*['\"][[:graph:]]+['\"][[:space:]]*)(.*[}])|\1path = '${WORKDIR}/${crate_dir//%commit%/${commit}}'\3|;"
			"s|^(${crate}[[:space:]]*=[[:space:]]*[{].*)([,][[:space:]]*branch[[:space:]]*=[[:space:]]*['\"][[:graph:]]+['\"][[:space:]]*)(.*[}])|\1\3|;"
			"s|^(${crate}[[:space:]]*=[[:space:]]*[{].*)([,][[:space:]]*tag[[:space:]]*=[[:space:]]*['\"][[:graph:]]+['\"][[:space:]]*)(.*[}])|\1\3|;"
			"s|^(${crate}[[:space:]]*=[[:space:]]*[{].*)([,][[:space:]]*rev[[:space:]]*=[[:space:]]*['\"][[:graph:]]+['\"][[:space:]]*)(.*[}])|\1\3|;"
		)
	done
	sed -i -E -e "${sed_scripts[*]}" Cargo.toml ||
		die "Failed to override dependencies in Cargo.toml"
}

src_compile() {
	cargo_src_compile

	"$(cargo_target_dir)"/tinymist completion bash > tinymist || die
	"$(cargo_target_dir)"/tinymist completion fish > tinymist.fish || die
	"$(cargo_target_dir)"/tinymist completion zsh > _tinymist || die
}

src_install() {
	cargo_src_install --path ./crates/tinymist-cli

	dobashcomp tinymist
	dofishcomp tinymist.fish
	dozshcomp _tinymist
}
