-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
[package] | ||
name = "specta-jsdoc" | ||
description = "Export your Rust types to JSDoc" | ||
version = "0.0.3" | ||
authors = ["Oscar Beaumont <[email protected]>"] | ||
edition = "2021" | ||
license = "MIT" | ||
repository = "https://github.com/oscartbeaumont/specta" | ||
documentation = "https://docs.rs/specta-jsdoc/latest/specta-jsdoc" | ||
keywords = ["async", "specta", "jsdoc", "rspc", "typescript", "typesafe"] | ||
categories = ["web-programming", "asynchronous"] | ||
|
||
# /bin/sh RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --all-features | ||
[package.metadata."docs.rs"] | ||
all-features = true | ||
rustdoc-args = ["--cfg", "docsrs"] | ||
|
||
[features] | ||
default = [] | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[dependencies] | ||
specta = { version = "=2.0.0-rc.16", path = "../specta" } | ||
specta-typescript = { version = "=0.0.3", path = "../specta-typescript" } | ||
# TODO: Don't depend on serde | ||
specta-serde = { version = "=0.0.3", path = "../specta-serde" } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
//! [JSDoc](https://jsdoc.app) language exporter. | ||
Check warning on line 1 in specta-jsdoc/src/lib.rs GitHub Actions / clippypackage `specta-zod` is missing `package.readme` metadata
Check warning on line 1 in specta-jsdoc/src/lib.rs GitHub Actions / clippypackage `specta-util` is missing `package.readme` metadata
Check warning on line 1 in specta-jsdoc/src/lib.rs GitHub Actions / clippypackage `specta-swift` is missing `package.readme` metadata
Check warning on line 1 in specta-jsdoc/src/lib.rs GitHub Actions / clippypackage `specta-rust` is missing `package.readme` metadata
Check warning on line 1 in specta-jsdoc/src/lib.rs GitHub Actions / clippypackage `specta-openapi` is missing `package.readme` metadata
Check warning on line 1 in specta-jsdoc/src/lib.rs GitHub Actions / clippypackage `specta-kotlin` is missing `package.readme` metadata
Check warning on line 1 in specta-jsdoc/src/lib.rs GitHub Actions / clippypackage `specta-typescript` is missing `package.readme` metadata
Check warning on line 1 in specta-jsdoc/src/lib.rs GitHub Actions / clippypackage `specta-serde` is missing `package.readme` metadata
Check warning on line 1 in specta-jsdoc/src/lib.rs GitHub Actions / clippypackage `specta-jsdoc` is missing `package.readme` metadata
Check warning on line 1 in specta-jsdoc/src/lib.rs GitHub Actions / clippypackage `specta-go` is missing `package.readme` metadata
Check warning on line 1 in specta-jsdoc/src/lib.rs GitHub Actions / clippypackage `specta-datatype-from` is missing `package.readme` metadata
Check warning on line 1 in specta-jsdoc/src/lib.rs GitHub Actions / clippypackage `specta` is missing `package.readme` metadata
|
||
#![cfg_attr(docsrs, feature(doc_cfg))] | ||
#![doc( | ||
html_logo_url = "https://github.com/oscartbeaumont/specta/raw/main/.github/logo-128.png", | ||
html_favicon_url = "https://github.com/oscartbeaumont/specta/raw/main/.github/logo-128.png" | ||
)] | ||
|
||
use std::{borrow::Cow, path::Path}; | ||
|
||
use specta::{Language, TypeMap}; | ||
use specta_typescript::{BigIntExportBehavior, CommentFormatterFn, FormatterFn}; | ||
|
||
// TODO: Ensure this is up to our `Typescript` exporters standards. | ||
|
||
/// JSDoc language exporter. | ||
#[derive(Debug, Clone, Default)] | ||
pub struct JSDoc(pub specta_typescript::Typescript); | ||
|
||
impl From<specta_typescript::Typescript> for JSDoc { | ||
fn from(ts: specta_typescript::Typescript) -> Self { | ||
Self(ts) | ||
} | ||
} | ||
|
||
impl JSDoc { | ||
/// Construct a new JSDoc exporter with the default options configured. | ||
pub fn new() -> Self { | ||
Default::default() | ||
} | ||
|
||
/// Configure a header for the file. | ||
/// | ||
/// This is perfect for configuring lint ignore rules or other file-level comments. | ||
pub fn header(mut self, header: impl Into<Cow<'static, str>>) -> Self { | ||
self.0.header = header.into(); | ||
self | ||
} | ||
|
||
// TODO: Only keep this is TS stays responsible for exporting which it probs won't. | ||
/// Removes the default Specta header from the output. | ||
pub fn remove_default_header(mut self) -> Self { | ||
self.0.remove_default_header = true; | ||
self | ||
} | ||
|
||
/// Configure the BigInt handling behaviour | ||
pub fn bigint(mut self, bigint: BigIntExportBehavior) -> Self { | ||
self.0.bigint = bigint; | ||
self | ||
} | ||
|
||
/// Configure a function which is responsible for styling the comments to be exported | ||
/// | ||
/// Implementations: | ||
/// - [`js_doc`](crate::lang::ts::js_doc) | ||
/// | ||
/// Not calling this method will default to the [`js_doc`](crate::lang::ts::js_doc) exporter. | ||
/// `None` will disable comment exporting. | ||
/// `Some(exporter)` will enable comment exporting using the provided exporter. | ||
pub fn comment_style(mut self, exporter: CommentFormatterFn) -> Self { | ||
self.0.comment_exporter = Some(exporter); | ||
self | ||
} | ||
|
||
/// Configure a function which is responsible for formatting the result file or files | ||
/// | ||
/// | ||
/// Built-in implementations: | ||
/// - [`prettier`](specta_typescript:formatter:::prettier) | ||
/// - [`ESLint`](specta_typescript::formatter::eslint) | ||
/// - [`Biome`](specta_typescript::formatter::biome)e | ||
pub fn formatter(mut self, formatter: FormatterFn) -> Self { | ||
self.0.formatter = Some(formatter); | ||
self | ||
} | ||
} | ||
|
||
impl Language for JSDoc { | ||
type Error = specta_typescript::ExportError; // TODO: Custom error type | ||
|
||
// TODO: Make this properly export JSDoc | ||
fn export(&self, type_map: TypeMap) -> Result<String, Self::Error> { | ||
Check warning on line 82 in specta-jsdoc/src/lib.rs GitHub Actions / clippyunused variable: `type_map`
|
||
todo!("Coming soon..."); | ||
Check warning on line 83 in specta-jsdoc/src/lib.rs GitHub Actions / clippy`todo` should not be present in production code
|
||
// let mut out = self.0.header.to_string(); | ||
// if !self.0.remove_default_header { | ||
// out += "// This file has been generated by Specta. DO NOT EDIT.\n\n"; | ||
// } | ||
|
||
// if let Some((ty_name, l0, l1)) = detect_duplicate_type_names(&type_map).into_iter().next() { | ||
// return Err(ExportError::DuplicateTypeName(ty_name, l0, l1)); | ||
// } | ||
|
||
// for (_, ty) in type_map.iter() { | ||
// is_valid_ty(&ty.inner, &type_map)?; | ||
|
||
// out += &export_named_datatype(&self.0, ty, &type_map)?; | ||
// out += "\n\n"; | ||
// } | ||
|
||
// Ok(out) | ||
} | ||
|
||
fn format(&self, path: &Path) -> Result<(), Self::Error> { | ||
if let Some(formatter) = self.0.formatter { | ||
formatter(path)?; | ||
} | ||
Ok(()) | ||
} | ||
} |