Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specta constants #276

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions specta-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ specta = { version = "=2.0.0-rc.16", path = "../specta" }
specta-macros = { version = "=2.0.0-rc.16", path = "../specta-macros", default-features = false, optional = true }
ctor = { version = "0.2.8", default-features = false, optional = true }
serde = "1.0.204" # TODO: Can we remove this or at least make it optional behind the `serde` flag
serde_json = "1" # TODO: Remove this or make it optional
46 changes: 41 additions & 5 deletions specta-util/src/type_collection.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,79 @@
use std::{borrow::Borrow, collections::HashMap, path::Path};
use std::{
borrow::{Borrow, Cow},
collections::HashMap,
path::Path,
};

use specta::{Language, NamedDataType, NamedType, SpectaID, TypeMap};

/// Define a set of types which can be exported together
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TypeCollection {
// TODO: Make into a single `Vec<Enum>` to maintain global ordering?
types: HashMap<SpectaID, fn(&mut TypeMap) -> NamedDataType>,
constants: HashMap<Cow<'static, str>, serde_json::Value>, // TODO: Can we make this format agnostic?
}

impl Default for TypeCollection {
fn default() -> Self {
Self {
types: HashMap::new(),
types: Default::default(),
constants: Default::default(),
}
}
}

Check warning on line 24 in specta-util/src/type_collection.rs

View workflow job for this annotation

GitHub Actions / clippy

this `impl` can be derived

warning: this `impl` can be derived --> specta-util/src/type_collection.rs:17:1 | 17 | / impl Default for TypeCollection { 18 | | fn default() -> Self { 19 | | Self { 20 | | types: Default::default(), ... | 23 | | } 24 | | } | |_^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#derivable_impls = note: `-W clippy::derivable-impls` implied by `-W clippy::all` = help: to override `-W clippy::all` add `#[allow(clippy::derivable_impls)]` = help: remove the manual implementation... help: ...and instead derive it | 11 + #[derive(Default)] 12 | pub struct TypeCollection { |

impl TypeCollection {
#[allow(unused)]
pub(crate) fn from_raw(types: HashMap<SpectaID, fn(&mut TypeMap) -> NamedDataType>) -> Self {
Self { types }
Self {
types,
constants: Default::default(),
}
}

// TODO: Maybe register framework info for default header

/// Join another type collection into this one.
pub fn extend(&mut self, collection: impl Borrow<TypeCollection>) -> &mut Self {
self.types.extend(collection.borrow().types.iter());
let collection = collection.borrow();
self.types.extend(collection.types.iter());
self.constants.extend(
collection
.constants
.iter()
.map(|(k, v)| (k.clone(), v.clone())),
);
self
}

// TODO: Should you be allowed to merge in a `TypeMap`???

// TODO: Should you be able to output a type_map from our internal registry

/// Register a type with the collection.
pub fn register<T: NamedType>(&mut self) -> &mut Self {
self.types
.insert(T::sid(), |type_map| T::definition_named_data_type(type_map));
self
}

/// TODO
// #[cfg(feature = "serde")] // TODO
pub fn constant<T: serde::Serialize>(
&mut self,
name: impl Into<Cow<'static, str>>,
value: T,
) -> &mut Self {
self.constants
.insert(name.into(), serde_json::to_value(value).unwrap()); // TODO: Error handling

Check warning on line 69 in specta-util/src/type_collection.rs

View workflow job for this annotation

GitHub Actions / clippy

used `unwrap()` on a `Result` value

warning: used `unwrap()` on a `Result` value --> specta-util/src/type_collection.rs:69:34 | 69 | .insert(name.into(), serde_json::to_value(value).unwrap()); // TODO: Error handling | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if this value is an `Err`, it will panic = help: consider using `expect()` to provide a better panic message = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used = note: requested on the command line with `-W clippy::unwrap-used`
self
}

/// Export all the types in the collection into the given type map.
pub fn collect(&self, mut type_map: &mut TypeMap) {
for (sid, export) in self.types.iter() {
let dt = export(&mut type_map);

Check warning on line 76 in specta-util/src/type_collection.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> specta-util/src/type_collection.rs:76:29 | 76 | let dt = export(&mut type_map); | ^^^^^^^^^^^^^ help: change this to: `type_map` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `-W clippy::needless-borrow` implied by `-W clippy::all` = help: to override `-W clippy::all` add `#[allow(clippy::needless_borrow)]`
type_map.insert(*sid, dt);
}
}
Expand All @@ -47,9 +82,10 @@
pub fn export<L: Language>(&self, language: L) -> Result<String, L::Error> {
let mut type_map = TypeMap::default();
self.collect(&mut type_map);
language.export(type_map)
language.export(type_map) // TODO: &self.constants
}

// TODO: Maybe we could put `path` on `Language` and remove this?
/// TODO
pub fn export_to<L: Language>(
&self,
Expand Down
Loading