1
Fork 0
framed-rs/bin/decode_typed

63 lines
1.9 KiB
Bash
Executable file

#!/usr/bin/env bash
set -e
REPO_DIR="$(cd $(dirname ${BASH_SOURCE[0]} )/..; pwd )"
DECODE_TYPE_CRATE_DIR="${DECODE_TYPE_CRATE_DIR?You must set this environment variable with the path to the crate containing your serialized type.
E.g. \'framed-rs/test_type\'}"
DECODE_TYPE_NAME="${DECODE_TYPE_NAME?You must set this environment variable with the full name of your serialized type including its module path.
E.g. \'framed_test_type::Test\'}"
TC=${TC:stable}
echo "DECODE_TYPE_CRATE_DIR = '${DECODE_TYPE_CRATE_DIR}'"
echo "DECODE_TYPE_NAME = '${DECODE_TYPE_NAME}'"
TYPE_CRATE_NAME="$(cargo read-manifest --manifest-path "${DECODE_TYPE_CRATE_DIR}/Cargo.toml" | jq '.name' -r)"
TMP_DIR="${REPO_DIR}/target/decode_typed/tmp"
mkdir -p "${TMP_DIR}";
rm -f "${TMP_DIR}/"*;
export FRAMED_DECODE_DYNAMIC_RS="${TMP_DIR}/dynamic_${DECODE_TYPE_NAME}.rs";
cat > "${FRAMED_DECODE_DYNAMIC_RS}" <<EOF
// Generated by framed-rs/bin/decode_typed, your changes will be overwritten.
extern crate ${TYPE_CRATE_NAME};
use ${DECODE_TYPE_NAME} as UserType;
#[allow(dead_code)]
static USER_TYPE_NAME: &'static str = "${DECODE_TYPE_NAME}";
EOF
# Build user type crate.
cargo +${TC} rustc \
--manifest-path "${DECODE_TYPE_CRATE_DIR}/Cargo.toml" \
--lib \
${DECODE_TYPE_CARGO_FLAGS} \
-- \
-o "${TMP_DIR}/${TYPE_CRATE_NAME}.rlib" \
${DECODE_TYPE_RUSTC_FLAGS} \
< /dev/null;
TYPE_CRATE_RLIB="$(ls "${TMP_DIR}"/lib${TYPE_CRATE_NAME}*.rlib)";
# Build decode bin.
cargo +${TC} rustc \
--manifest-path "${REPO_DIR}/decode/Cargo.toml" \
--bin framed_decode \
${DECODE_BIN_CARGO_FLAGS} \
-- \
-o "${TMP_DIR}/decode" \
--extern ${TYPE_CRATE_NAME}="${TYPE_CRATE_RLIB}" \
--emit obj \
${DECODE_BIN_RUSTC_FLAGS} \
< /dev/null;
DECODE_PATH="$(find "${TMP_DIR}/decode"* -executable -type f)";
echo "";
echo "";
# Run decode bin.
${DECODE_PATH} "$@";