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

# Autogenerated by pycargoebuild 0.14.0

EAPI=8

RUST_MIN_VER="1.85.0"

CRATES=" "

TYPST_HASH="946ea31fb554bcf62e3215f64ddda87d70b026af"

declare -A GIT_CRATES=(
	[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 BSD-2 BSD CC0-1.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|;"
		)
	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

	dobashcomp tinymist
	dofishcomp tinymist.fish
	dozshcomp _tinymist
}