Skip to content

Commit

Permalink
Upgrade Specta
Browse files Browse the repository at this point in the history
I'm sick of the specta macros version not being locked bug.
  • Loading branch information
oscartbeaumont committed Mar 17, 2024
1 parent 10b711e commit 22eff02
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 106 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -63,6 +63,6 @@ pprof = { version = "0.13.0", features = ["flamegraph", "criterion", "protobuf-c
members = ["./crates/*", "./examples", "./examples/axum", "./examples/vercel", "./examples/tauri/src-tauri"]

[workspace.dependencies]
specta = { version = "=2.0.0-rc.6", default-features = false, features = ["serde", "serde_json"] }
specta = { version = "=2.0.0-rc.7", default-features = false, features = ["serde", "serde_json"] }
serde = { version = "1", default-features = false, features = ["derive"] }
thiserror = { version = "1.0.50", default-features = false }
38 changes: 5 additions & 33 deletions crates/core/src/procedure_store.rs
@@ -1,6 +1,6 @@
use std::{borrow::Cow, convert::Infallible};

use specta::{ts, DataType, DataTypeFrom, DefOpts, NamedDataType, StructType, Type, TypeMap};
use specta::{ts, DataType, DataTypeFrom, NamedDataType, StructType, Type, TypeMap};

use crate::{
layer::{boxed, DynLayer, Layer},
Expand Down Expand Up @@ -56,13 +56,7 @@ pub struct ProcedureDef {
}

fn never() -> DataType {
Infallible::inline(
DefOpts {
parent_inline: false,
type_map: &mut Default::default(),
},
&[],
)
Infallible::inline(&mut Default::default(), &[])
}

impl ProcedureDef {
Expand All @@ -77,34 +71,12 @@ impl ProcedureDef {
{
Ok(ProcedureDef {
key,
input: match TArg::reference(
DefOpts {
parent_inline: false,
type_map,
},
&[],
)
.inner
{
input: match TArg::reference(type_map, &[]).inner {
DataType::Tuple(tuple) if tuple.elements().is_empty() => never(),
t => t,
},
result: TResult::reference(
DefOpts {
parent_inline: false,
type_map,
},
&[],
)
.inner,
error: TError::reference(
DefOpts {
parent_inline: false,
type_map,
},
&[],
)
.inner,
result: TResult::reference(type_map, &[]).inner,
error: TError::reference(type_map, &[]).inner,
})
}
}
Expand Down
47 changes: 15 additions & 32 deletions crates/core/src/router.rs
Expand Up @@ -127,42 +127,25 @@ where
)?;

// We sort by name to detect duplicate types BUT also to ensure the output is deterministic. The SID can change between builds so is not suitable for this.
let types = self
.typ_store
.clone()
.into_iter()
.filter(|(_, v)| match v {
Some(_) => true,
None => {
unreachable!(
"Placeholder type should never be returned from the Specta functions!"
)
}
})
.collect::<BTreeMap<_, _>>();
let types = self.typ_store.iter().collect::<BTreeMap<_, _>>();

// This is a clone of `detect_duplicate_type_names` but using a `BTreeMap` for deterministic ordering
let mut map = BTreeMap::new();
for (sid, dt) in &types {
match dt {
Some(dt) => {
if let Some(ext) = dt.ext() {
if let Some((existing_sid, existing_impl_location)) =
map.insert(dt.name(), (sid, *ext.impl_location()))
{
if existing_sid != sid {
return Err(ExportError::TsExportErr(
ts::ExportError::DuplicateTypeName(
dt.name().clone(),
*ext.impl_location(),
existing_impl_location,
),
));
}
}
if let Some(ext) = dt.ext() {
if let Some((existing_sid, existing_impl_location)) =
map.insert(dt.name(), (sid, *ext.impl_location()))
{
if existing_sid != sid {
return Err(ExportError::TsExportErr(
ts::ExportError::DuplicateTypeName(
dt.name().clone(),
*ext.impl_location(),
existing_impl_location,
),
));
}
}
None => unreachable!(),
}
}

Expand All @@ -173,10 +156,10 @@ where
ts::export_named_datatype(
&config,
match types.get(sid) {
Some(Some(v)) => v,
Some(v) => v,
_ => unreachable!(),
},
&types
&self.typ_store
)?
)?;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri/Cargo.toml
Expand Up @@ -9,5 +9,5 @@ rspc-core = { version = "1.0.0-rc.5", path = "../core", default-features = false
serde = { workspace = true, features = ["derive"] }
serde_json = "1.0.107"
tauri = { version = "1.5.2", default-features = false, features = ["wry"] } # TODO: Work without wry
tauri-specta = { version = "=2.0.0-rc.3", default-features = false, features = ["typescript"] }
tauri-specta = { version = "=2.0.0-rc.4", default-features = false, features = ["typescript"] }
specta = { workspace = true }
4 changes: 4 additions & 0 deletions examples/axum/src/main.rs
Expand Up @@ -194,6 +194,10 @@ async fn main() {
}
})
})
// .middleware(mw(|mw, ctx| async move {
// println!("Client requested operation '{}'", ctx.operation);
// mw.next(ctx).await
// }))
.build()
.unwrap()
.arced(); // This function is a shortcut to wrap the router in an `Arc`.
Expand Down
30 changes: 3 additions & 27 deletions examples/bindings.ts
@@ -1,30 +1,6 @@
// This file was generated by [rspc](https://github.com/oscartbeaumont/rspc). Do not edit this file manually.
export type Procedures = {
queries:
| { key: "X-Demo-Header"; input: never; result: string; error: Error }
| { key: "customErr"; input: never; result: null; error: MyCustomError }
| { key: "echo"; input: string; result: string; error: Error }
| { key: "echo2"; input: string; result: string; error: Error }
| { key: "error"; input: never; result: string; error: Error }
| { key: "transformMe"; input: never; result: string; error: Error }
| { key: "version"; input: never; result: string; error: Infallible };
mutations:
| { key: "error"; input: never; result: string; error: Error }
| { key: "sendMsg"; input: string; result: string; error: Error };
subscriptions:
| { key: "batchingTest"; input: never; result: string; error: Error }
| { key: "errorPings"; input: never; result: string; error: Error }
| { key: "pings"; input: never; result: string; error: Error }
| {
key: "testSubscriptionShutdown";
input: never;
result: number;
error: Error;
};
};
export type Procedures = { queries: { key: "X-Demo-Header"; input: never; result: string; error: Error } | { key: "customErr"; input: never; result: null; error: MyCustomError } | { key: "echo"; input: string; result: string; error: Error } | { key: "echo2"; input: string; result: string; error: Error } | { key: "error"; input: never; result: string; error: Error } | { key: "transformMe"; input: never; result: string; error: Error } | { key: "version"; input: [string, null]; result: string; error: Error }; mutations: { key: "error"; input: never; result: string; error: Error } | { key: "sendMsg"; input: string; result: string; error: Error }; subscriptions: { key: "batchingTest"; input: never; result: string; error: Error } | { key: "errorPings"; input: never; result: string; error: Error } | { key: "pings"; input: never; result: string; error: Error } | { key: "testSubscriptionShutdown"; input: never; result: number; error: Error } }

export type Error = string;
export type Error = string

export type Infallible = never;

export type MyCustomError = "IAmBroke";
export type MyCustomError = "IAmBroke"
5 changes: 1 addition & 4 deletions src/internal/procedure.rs
Expand Up @@ -51,10 +51,7 @@ macro_rules! resolvers {
let $this = self;

let resolver = HasResolver::new(resolver, ProcedureKind::$kind, |type_map| <<$mw_ty as crate::internal::middleware::SealedMiddlewareBuilder>::Arg<TArg> as specta::Type>::reference(
specta::DefOpts {
parent_inline: false,
type_map,
},
type_map,
&[],
));

Expand Down
10 changes: 2 additions & 8 deletions src/internal/resolver/function.rs
Expand Up @@ -2,7 +2,7 @@ use std::{borrow::Cow, marker::PhantomData};

use serde::{de::DeserializeOwned, Serialize};
use serde_json::Value;
use specta::{reference::Reference, ts, DataType, DefOpts, Type, TypeMap};
use specta::{reference::Reference, ts, DataType, Type, TypeMap};

use rspc_core::internal::{IntoResolverError, Layer, ProcedureDef, ProcedureKind, RequestContext};

Expand Down Expand Up @@ -93,11 +93,5 @@ mod private {
pub(crate) use private::M;

fn never() -> DataType {
std::convert::Infallible::inline(
DefOpts {
parent_inline: false,
type_map: &mut Default::default(),
},
&[],
)
std::convert::Infallible::inline(&mut Default::default(), &[])
}

0 comments on commit 22eff02

Please sign in to comment.