Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alias]
xtask = "run --package xtask --"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/target
Cargo.lock
.env

/typesense-data
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
members = [
"typesense",
"typesense_derive",
"typesense_codegen"
"typesense_codegen",
"xtask",
]

resolver = "3"
Expand Down
9 changes: 9 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
typesense:
image: typesense/typesense:29.0
restart: on-failure
ports:
- '8108:8108'
volumes:
- ./typesense-data:/data
command: '--data-dir /data --api-key=xyz --enable-cors'
2 changes: 1 addition & 1 deletion typesense/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! # Examples
//!
//! ```
//! #[cfg(any(feature = "tokio_test", target_arch = "wasm32"))]
//! #[cfg(not(target_family = "wasm"))]
//! {
//! use serde::{Deserialize, Serialize};
//! use typesense::document::Document;
Expand Down
89 changes: 42 additions & 47 deletions typesense_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,17 @@ fn impl_typesense_collection(item: ItemStruct) -> syn::Result<TokenStream> {
} = extract_attrs(attrs)?;
let collection_name = collection_name.unwrap_or_else(|| ident.to_string().to_lowercase());

if let Some(ref sorting_field) = default_sorting_field {
if !fields.iter().any(|field|
if let Some(ref sorting_field) = default_sorting_field
&& !fields.iter().any(|field|
// At this point we are sure that this field is a named field.
field.ident.as_ref().unwrap() == sorting_field)
{
return Err(syn::Error::new_spanned(
item_ts,
format!(
"defined default_sorting_field = \"{sorting_field}\" does not match with any field."
),
));
}
{
return Err(syn::Error::new_spanned(
item_ts,
format!(
"defined default_sorting_field = \"{sorting_field}\" does not match with any field."
),
));
}

let typesense_fields = fields
Expand Down Expand Up @@ -98,17 +97,16 @@ fn impl_typesense_collection(item: ItemStruct) -> syn::Result<TokenStream> {

// Get the inner type for a given wrapper
fn ty_inner_type<'a>(ty: &'a syn::Type, wrapper: &'static str) -> Option<&'a syn::Type> {
if let syn::Type::Path(p) = ty {
if p.path.segments.len() == 1 && p.path.segments[0].ident == wrapper {
if let syn::PathArguments::AngleBracketed(ref inner_ty) = p.path.segments[0].arguments {
if inner_ty.args.len() == 1 {
// len is 1 so this should not fail
let inner_ty = inner_ty.args.first().unwrap();
if let syn::GenericArgument::Type(t) = inner_ty {
return Some(t);
}
}
}
if let syn::Type::Path(p) = ty
&& p.path.segments.len() == 1
&& p.path.segments[0].ident == wrapper
&& let syn::PathArguments::AngleBracketed(ref inner_ty) = p.path.segments[0].arguments
&& inner_ty.args.len() == 1
{
// len is 1 so this should not fail
let inner_ty = inner_ty.args.first().unwrap();
if let syn::GenericArgument::Type(t) = inner_ty {
return Some(t);
}
}
None
Expand Down Expand Up @@ -231,42 +229,39 @@ fn to_typesense_field_type(field: &Field) -> syn::Result<proc_macro2::TokenStrea
.attrs
.iter()
.filter_map(|attr| {
if attr.path.segments.len() == 1 && attr.path.segments[0].ident == "typesense" {
if let Some(proc_macro2::TokenTree::Group(g)) =
if attr.path.segments.len() == 1
&& attr.path.segments[0].ident == "typesense"
&& let Some(proc_macro2::TokenTree::Group(g)) =
attr.tokens.clone().into_iter().next()
{
let mut tokens = g.stream().into_iter();
match tokens.next() {
Some(proc_macro2::TokenTree::Ident(ref i)) => {
if i != "facet" {
return Some(Err(syn::Error::new_spanned(
i,
format!("Unexpected token {i}. Did you mean `facet`?"),
)));
}
}
Some(ref tt) => {
return Some(Err(syn::Error::new_spanned(
tt,
format!("Unexpected token {tt}. Did you mean `facet`?"),
)));
}
None => {
{
let mut tokens = g.stream().into_iter();
match tokens.next() {
Some(proc_macro2::TokenTree::Ident(ref i)) => {
if i != "facet" {
return Some(Err(syn::Error::new_spanned(
attr,
"expected `facet`",
i,
format!("Unexpected token {i}. Did you mean `facet`?"),
)));
}
}

if let Some(ref tt) = tokens.next() {
Some(ref tt) => {
return Some(Err(syn::Error::new_spanned(
tt,
"Unexpected token. Expected )",
format!("Unexpected token {tt}. Did you mean `facet`?"),
)));
}
return Some(Ok(()));
None => {
return Some(Err(syn::Error::new_spanned(attr, "expected `facet`")));
}
}

if let Some(ref tt) = tokens.next() {
return Some(Err(syn::Error::new_spanned(
tt,
"Unexpected token. Expected )",
)));
}
return Some(Ok(()));
}
None
})
Expand Down
12 changes: 12 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "xtask"
publish = false
version = "0.0.0"
edition.workspace = true

[dependencies]
reqwest = { version = "0.11", features = ["blocking"] } # "blocking" is simpler for scripts
anyhow = "1.0"
clap = { version = "4.0", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9"
139 changes: 139 additions & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
use anyhow::{Context, Result};
use clap::{Parser, ValueEnum};
use std::env;
use std::fs;

Check warning on line 4 in xtask/src/main.rs

View workflow job for this annotation

GitHub Actions / tests (ubuntu, wasm32-unknown-unknown)

unused import: `std::fs`
use std::process::Command;
mod preprocess_openapi;
use preprocess_openapi::preprocess_openapi_file;

const SPEC_URL: &str =

Check warning on line 9 in xtask/src/main.rs

View workflow job for this annotation

GitHub Actions / tests (ubuntu, wasm32-unknown-unknown)

constant `SPEC_URL` is never used
"https://raw.githubusercontent.com/typesense/typesense-api-spec/master/openapi.yml";

// Input spec file, expected in the project root.
const INPUT_SPEC_FILE: &str = "openapi.yml";

Check warning on line 13 in xtask/src/main.rs

View workflow job for this annotation

GitHub Actions / tests (ubuntu, wasm32-unknown-unknown)

constant `INPUT_SPEC_FILE` is never used
const OUTPUT_PREPROCESSED_FILE: &str = "./preprocessed_openapi.yml";

Check warning on line 14 in xtask/src/main.rs

View workflow job for this annotation

GitHub Actions / tests (ubuntu, wasm32-unknown-unknown)

constant `OUTPUT_PREPROCESSED_FILE` is never used

// Output directory for the generated code.
const OUTPUT_DIR: &str = "typesense_codegen";

Check warning on line 17 in xtask/src/main.rs

View workflow job for this annotation

GitHub Actions / tests (ubuntu, wasm32-unknown-unknown)

constant `OUTPUT_DIR` is never used

#[derive(Parser)]
#[command(
author,
version,
about = "A task runner for the typesense-rust project"
)]
struct Cli {
/// The list of tasks to run in sequence.
#[arg(required = true, value_enum)]
tasks: Vec<Task>,
}

#[derive(ValueEnum, Clone, Debug)]
#[clap(rename_all = "kebab-case")] // Allows us to type `code-gen` instead of `CodeGen`
enum Task {
/// Fetches the latest OpenAPI spec from [the Typesense repository](https://github.com/typesense/typesense-api-spec/blob/master/openapi.yml).
Fetch,
/// Generates client code from the spec file using the Docker container.
CodeGen,
}

#[cfg(target_family = "wasm")]
fn main() {}

#[cfg(not(target_family = "wasm"))]
fn main() -> Result<()> {
let cli = Cli::parse();

for task in cli.tasks {
println!("▶️ Running task: {:?}", task);
match task {
Task::Fetch => task_fetch_api_spec()?,
Task::CodeGen => task_codegen()?,
}
}
Ok(())
}

#[cfg(not(target_family = "wasm"))]
fn task_fetch_api_spec() -> Result<()> {
println!("▶️ Running codegen task...");

println!(" - Downloading spec from {}", SPEC_URL);
let response =
reqwest::blocking::get(SPEC_URL).context("Failed to download OpenAPI spec file")?;

if !response.status().is_success() {
anyhow::bail!("Failed to download spec: HTTP {}", response.status());
}

let spec_content = response.text()?;
fs::write(INPUT_SPEC_FILE, spec_content)
.context(format!("Failed to write spec to {}", INPUT_SPEC_FILE))?;
println!(" - Spec saved to {}", INPUT_SPEC_FILE);

println!("✅ Fetch API spec task finished successfully.");

Ok(())
}

/// Task to generate client code from the OpenAPI spec using a Docker container.
fn task_codegen() -> Result<()> {

Check warning on line 80 in xtask/src/main.rs

View workflow job for this annotation

GitHub Actions / tests (ubuntu, wasm32-unknown-unknown)

function `task_codegen` is never used
println!("▶️ Running codegen task via Docker...");

println!("Preprocessing the Open API spec file...");
preprocess_openapi_file(INPUT_SPEC_FILE, OUTPUT_PREPROCESSED_FILE)
.expect("Preprocess failed, aborting!");
// Get the absolute path to the project's root directory.
// std::env::current_dir() gives us the directory from which `cargo xtask` was run.
let project_root = env::current_dir().context("Failed to get current directory")?;

// Check if the input spec file exists before trying to run Docker.
let input_spec_path = project_root.join(INPUT_SPEC_FILE);
if !input_spec_path.exists() {
anyhow::bail!(
"Input spec '{}' not found in project root. Please add it before running.",
INPUT_SPEC_FILE
);
}

// Construct the volume mount string for Docker.
// Docker needs an absolute path for the volume mount source.
// to_string_lossy() is used to handle potential non-UTF8 paths gracefully.
let volume_mount = format!("{}:/local", project_root.to_string_lossy());
println!(" - Using volume mount: {}", volume_mount);

// Set up and run the Docker command.
println!(" - Starting Docker container...");
let status = Command::new("docker")
.arg("run")
.arg("--rm") // Remove the container after it exits
.arg("-v")
.arg(volume_mount) // Mount the project root to /local in the container
.arg("openapitools/openapi-generator-cli")
.arg("generate")
.arg("-i")
.arg(format!("/local/{}", OUTPUT_PREPROCESSED_FILE)) // Input path inside the container
.arg("-g")
.arg("rust")
.arg("-o")
.arg(format!("/local/{}", OUTPUT_DIR)) // Output path inside the container
.arg("--additional-properties")
.arg("library=reqwest")
.arg("--additional-properties")
.arg("supportMiddleware=true")
.arg("--additional-properties")
.arg("useSingleRequestParameter=true")
// .arg("--additional-properties")
// .arg("useBonBuilder=true")
.status()
.context("Failed to execute Docker command. Is Docker installed and running?")?;

// Check if the command was successful.
if !status.success() {
anyhow::bail!("Docker command failed with status: {}", status);
}

println!("✅ Codegen task finished successfully.");
println!(" Generated code is available in '{}'", OUTPUT_DIR);
Ok(())
}
Loading
Loading