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

EAPI=8

POSTGRES_COMPAT=( {16..18} ) # config section below
POSTGRES_USEDEP="server"

CRATES="
	aho-corasick@1.1.3
	allocator-api2@0.2.21
	annotate-snippets@0.11.5
	anstream@0.6.20
	anstyle-parse@0.2.7
	anstyle-query@1.1.4
	anstyle-wincon@3.0.10
	anstyle@1.0.11
	anyhow@1.0.100
	bindgen@0.71.1
	bitflags@2.9.4
	bitvec@1.0.1
	bumpalo@3.19.0
	cargo_toml@0.22.3
	cc@1.2.38
	cee-scape@0.2.0
	cexpr@0.6.0
	cfg-if@1.0.3
	clang-sys@1.8.1
	clap@4.5.48
	clap_builder@4.5.48
	clap_derive@4.5.47
	clap_lex@0.7.5
	codepage@0.1.2
	colorchoice@1.0.4
	convert_case@0.8.0
	crossbeam-deque@0.8.6
	crossbeam-epoch@0.9.18
	crossbeam-utils@0.8.21
	crunchy@0.2.4
	darling@0.20.11
	darling_core@0.20.11
	darling_macro@0.20.11
	dary_heap@0.3.8
	displaydoc@0.2.5
	either@1.15.0
	encoding_rs@0.8.35
	enum-map-derive@0.17.0
	enum-map@2.7.3
	env_home@0.1.0
	equivalent@1.0.2
	errno@0.3.14
	eyre@0.6.12
	fallible-iterator@0.3.0
	fallible-streaming-iterator@0.1.9
	find-msvc-tools@0.1.2
	fixedbitset@0.5.7
	fnv@1.0.7
	foldhash@0.1.5
	form_urlencoded@1.2.2
	funty@2.0.0
	getrandom@0.3.3
	glob@0.3.3
	half@1.8.3
	half@2.6.0
	hashbrown@0.15.5
	hashbrown@0.16.0
	hashlink@0.10.0
	heck@0.5.0
	icu_collections@2.0.0
	icu_locale_core@2.0.0
	icu_normalizer@2.0.0
	icu_normalizer_data@2.0.0
	icu_properties@2.0.1
	icu_properties_data@2.0.1
	icu_provider@2.0.0
	ident_case@1.0.1
	idna@1.1.0
	idna_adapter@1.2.1
	indenter@0.3.4
	indexmap@2.11.4
	is_ci@1.2.0
	is_terminal_polyfill@1.70.1
	itertools@0.13.0
	itoa@1.0.15
	js-sys@0.3.81
	libc@0.2.176
	libloading@0.8.9
	libmimalloc-sys@0.1.44
	libsqlite3-sys@0.35.0
	linux-raw-sys@0.11.0
	litemap@0.8.0
	log@0.4.28
	memchr@2.7.6
	mimalloc@0.1.48
	min-max-heap@1.3.0
	minimal-lexical@0.2.1
	nom@7.1.3
	object@0.37.3
	once_cell@1.21.3
	once_cell_polyfill@1.70.1
	owo-colors@4.2.2
	paste@1.0.15
	pathsearch@0.2.0
	percent-encoding@2.3.2
	petgraph@0.8.2
	pgrx-bindgen@0.16.1
	pgrx-catalog@0.3.1
	pgrx-macros@0.16.1
	pgrx-pg-config@0.16.1
	pgrx-pg-sys@0.16.1
	pgrx-sql-entity-graph@0.16.1
	pgrx@0.16.1
	pin-project-internal@1.1.10
	pin-project@1.1.10
	pkg-config@0.3.32
	potential_utf@0.1.3
	ppv-lite86@0.2.21
	proc-macro-error-attr2@2.0.0
	proc-macro-error2@2.0.1
	proc-macro2@1.0.101
	quote@1.0.40
	r-efi@5.3.0
	radium@0.7.0
	rand@0.9.2
	rand_chacha@0.9.0
	rand_core@0.9.3
	rayon-core@1.13.0
	rayon@1.11.0
	regex-automata@0.4.11
	regex-syntax@0.8.6
	regex@1.11.3
	rusqlite@0.37.0
	rustc-hash@2.1.1
	rustix@1.1.2
	rustversion@1.0.22
	ryu@1.0.20
	same-file@1.0.6
	seahash@4.1.0
	seq-macro@0.3.6
	serde@1.0.227
	serde_cbor@0.11.2
	serde_core@1.0.227
	serde_derive@1.0.227
	serde_json@1.0.145
	serde_spanned@1.0.2
	shlex@1.3.0
	smallvec@1.15.1
	stable_deref_trait@1.2.0
	strsim@0.11.1
	supports-color@3.0.2
	syn@2.0.106
	synstructure@0.13.2
	tap@1.0.1
	target-triple@0.1.4
	thiserror-impl@2.0.16
	thiserror@2.0.16
	tinystr@0.8.1
	toml@0.9.7
	toml_datetime@0.7.2
	toml_parser@1.0.3
	toml_writer@1.0.3
	unescape@0.1.0
	unicode-ident@1.0.19
	unicode-segmentation@1.12.0
	unicode-width@0.2.1
	url@2.5.7
	utf8_iter@1.0.4
	utf8parse@0.2.2
	uuid@1.18.1
	validator@0.20.0
	validator_derive@0.20.0
	vcpkg@0.2.15
	walkdir@2.5.0
	wasi@0.14.7+wasi-0.2.4
	wasip2@1.0.1+wasi-0.2.4
	wasm-bindgen-backend@0.2.104
	wasm-bindgen-macro-support@0.2.104
	wasm-bindgen-macro@0.2.104
	wasm-bindgen-shared@0.2.104
	wasm-bindgen@0.2.104
	wasmparser@0.236.1
	which@8.0.0
	winapi-i686-pc-windows-gnu@0.4.0
	winapi-util@0.1.11
	winapi-x86_64-pc-windows-gnu@0.4.0
	winapi@0.3.9
	windows-link@0.2.0
	windows-sys@0.60.2
	windows-sys@0.61.1
	windows-targets@0.53.4
	windows_aarch64_gnullvm@0.53.0
	windows_aarch64_msvc@0.53.0
	windows_i686_gnu@0.53.0
	windows_i686_gnullvm@0.53.0
	windows_i686_msvc@0.53.0
	windows_x86_64_gnu@0.53.0
	windows_x86_64_gnullvm@0.53.0
	windows_x86_64_msvc@0.53.0
	winnow@0.7.13
	winsafe@0.0.19
	wit-bindgen@0.46.0
	writeable@0.6.1
	wyz@0.5.1
	yoke-derive@0.8.0
	yoke@0.8.0
	zerocopy-derive@0.8.27
	zerocopy@0.8.27
	zerofrom-derive@0.1.6
	zerofrom@0.1.6
	zerotrie@0.2.2
	zerovec-derive@0.11.1
	zerovec@0.11.4
"

inherit postgres-multi autotools cargo

DESCRIPTION="Scalable, fast, and disk-friendly vector search in Postgres, the successor of pgvecto.rs"
HOMEPAGE="
	https://docs.vectorchord.ai/vectorchord/getting-started/overview.html
	https://github.com/tensorchord/VectorChord
"
SRC_URI="
	https://github.com/tensorchord/VectorChord/archive/refs/tags/${PV}.tar.gz -> ${P}.tar.gz
	${CARGO_CRATE_URIS}
"

LICENSE="AGPL-3"
# Dependent crate licenses
LICENSE+=" Apache-2.0 BSD ISC MIT Unicode-3.0 ZLIB"

RDEPEND="
	${POSTGRES_DEP}
	dev-db/pgvector
"

BDEPEND="
	dev-util/cargo-pgrx
"

DEPEND="
	${BDEPEND}
	${RDEPEND}
"

SLOT="0"
KEYWORDS="~amd64"

RUST_REQ_USE="rustfmt"
RUST_MIN_VER="1.85.0"

S=${WORKDIR}/VectorChord-${PV}
export PGRX_HOME=${WORKDIR}/pgrx_home

pkg_setup() {
	postgres-multi_pkg_setup
	rust_pkg_setup
	use postgres_targets_postgres16 && ( cargo pgrx init --pg16=/usr/bin/pg_config || die )
	use postgres_targets_postgres17 && ( cargo pgrx init --pg17=/usr/bin/pg_config || die )
	use postgres_targets_postgres18 && ( cargo pgrx init --pg18=/usr/bin/pg_config || die )
}

src_configure() {
        local myfeatures=(
                $(usev postgres_targets_postgres16 pg16)
                $(usev postgres_targets_postgres17 pg17)
                $(usev postgres_targets_postgres18 pg18)
        )
        cargo_src_configure
}

src_compile() {
	make build
}

src_install() {
	default
	DESTDIR=${T} make install
}