Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Jul 31, 2024
1 parent 62e40a0 commit 168dc35
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
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,40 +1,75 @@
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() {
Expand All @@ -47,9 +82,10 @@ impl TypeCollection {
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

0 comments on commit 168dc35

Please sign in to comment.