Skip to content

Commit

Permalink
use actual ts types instead of JsValue
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-1991 committed Dec 26, 2024
1 parent 7219656 commit 5b37b1f
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 25 deletions.
59 changes: 55 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ wasm-bindgen = { version = "0.2.95", optional = true }
serde-wasm-bindgen = { version = "0.6.5", optional = true }
tokio = { version = "1.42.0", features = ["rt"] }
openai-api-rs = { version = "5.2.3", optional = true }
jsonschema = { version = "0.26.2", default-features = false }
jsonschema = { version = "0.27.1", default-features = false }
getrandom = { version = "0.2.15", features = ["js"] }
tsify-next = { version = "0.5.4", features = ["js"], optional = true }

[features]
default = ["openai"]
python = ["pyo3"]
wasm = ["wasm-bindgen", "serde-wasm-bindgen"]
wasm = ["wasm-bindgen", "serde-wasm-bindgen", "tsify-next"]
openai = ["openai-api-rs"]

[build-dependencies]
Expand Down
9 changes: 9 additions & 0 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ use std::{error::Error, fmt, str::FromStr};
#[cfg(feature = "python")]
use pyo3::{pyclass, pymethods};

#[cfg(feature = "wasm")]
use tsify_next::Tsify;

/// Represents an attribute with various properties and options.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "python", pyclass(get_all))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi))]
pub struct Attribute {
/// The name of the attribute.
pub name: String,
Expand Down Expand Up @@ -247,6 +252,8 @@ impl Attribute {
/// Represents an option for an attribute.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[cfg_attr(feature = "python", pyclass(get_all))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi))]
pub struct AttrOption {
/// The key of the option.
pub key: String,
Expand Down Expand Up @@ -289,6 +296,8 @@ impl AttrOption {

#[derive(Debug, Clone)]
#[cfg_attr(feature = "python", pyclass)]
#[cfg_attr(feature = "wasm", derive(Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi))]
pub enum DataType {
Boolean(bool),
Integer(i64),
Expand Down
24 changes: 8 additions & 16 deletions src/bindings/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use crate::datamodel::DataModel;
use crate::exporters::Templates;
use crate::json::export::to_json_schema;
use crate::validation::Validator;
use serde_wasm_bindgen::to_value;
use wasm_bindgen::prelude::*;

// Add console.log support for debugging
Expand All @@ -46,12 +45,10 @@ extern "C" {
/// A `Result` which is:
/// - `Ok(JsValue)` if the parsing and serialization are successful.
/// - `Err(JsValue)` if there is an error during parsing or serialization.
#[allow(clippy::result_large_err)]
#[wasm_bindgen]
pub fn parse_model(markdown_content: &str) -> Result<JsValue, JsValue> {
let model = DataModel::from_markdown_string(markdown_content)
.map_err(|e| JsValue::from_str(&format!("Error parsing markdown content: {}", e)))?;

to_value(&model).map_err(|e| JsValue::from_str(&format!("Error serializing model: {}", e)))
pub fn parse_model(markdown_content: &str) -> Result<DataModel, Validator> {
DataModel::from_markdown_string(markdown_content)
}

/// Converts the given markdown content into a specified template format.
Expand Down Expand Up @@ -115,25 +112,20 @@ pub fn json_schema(
Ok(serde_json::to_string(&json_schema).unwrap())
}

/// Validates the given markdown content and returns the validation result as a `JsValue`.
/// Validates the given markdown content and returns the validation result as a `Validator`.
///
/// # Arguments
///
/// * `markdown_content` - A string slice that holds the markdown content to be validated.
///
/// # Returns
///
/// A `Result` which is:
/// - `Ok(JsValue)` if the validation is successful.
/// - `Err(JsValue)` if there is an error during parsing or validation.
/// Either an empty `Validator` or an error `Validator`.
#[wasm_bindgen]
pub fn validate(markdown_content: &str) -> Result<JsValue, JsValue> {
pub fn validate(markdown_content: &str) -> Validator {
let model = DataModel::from_markdown_string(markdown_content);

match model {
Ok(_) => to_value(&Validator::new())
.map_err(|e| JsValue::from_str(&format!("Error serializing validator: {}", e))),
Err(res) => to_value(&res)
.map_err(|e| JsValue::from_str(&format!("Error serializing validator: {}", e))),
Ok(_) => Validator::new(),
Err(res) => res,
}
}
7 changes: 6 additions & 1 deletion src/datamodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ use crate::json::validation::{validate_json, ValidationError};
use crate::markdown::frontmatter::FrontMatter;
use crate::markdown::parser::parse_markdown;
use crate::object::{Enumeration, Object};
use crate::validation::Validator;
use colored::Colorize;

use crate::validation::Validator;
#[cfg(feature = "python")]
use pyo3::pyclass;

#[cfg(feature = "wasm")]
use tsify_next::Tsify;

// Data model
//
// Contains a list of objects that represent the data model
Expand All @@ -64,6 +67,8 @@ use pyo3::pyclass;
// * `internal_schema` - Generate an internal schema from the data model
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[cfg_attr(feature = "python", pyclass(get_all))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi))]
pub struct DataModel {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
Expand Down
5 changes: 5 additions & 0 deletions src/markdown/frontmatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ use serde::{Deserialize, Serialize};
#[cfg(feature = "python")]
use pyo3::pyclass;

#[cfg(feature = "wasm")]
use tsify_next::Tsify;

/// Represents the front matter data of a markdown file.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
#[cfg_attr(feature = "python", pyclass(get_all))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi))]
pub struct FrontMatter {
/// A boolean field with a default value, renamed from `id-field`.
#[serde(default = "default_id_field", rename = "id-field")]
Expand Down
7 changes: 7 additions & 0 deletions src/markdown/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@

use serde::{Deserialize, Serialize};

#[cfg(feature = "wasm")]
use tsify_next::Tsify;

#[cfg(feature = "python")]
use pyo3::prelude::*;

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "python", pyclass(get_all))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi))]
pub struct Position {
pub line: usize,
pub column: PositionRange,
Expand All @@ -42,6 +47,8 @@ impl PartialOrd for Position {

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "python", pyclass(get_all))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi))]
pub struct PositionRange {
pub start: usize,
pub end: usize,
Expand Down
7 changes: 7 additions & 0 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ use std::collections::BTreeMap;
#[cfg(feature = "python")]
use pyo3::pyclass;

#[cfg(feature = "wasm")]
use tsify_next::Tsify;

#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[cfg_attr(feature = "python", pyclass(get_all))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi))]
/// Represents an object with a name, attributes, docstring, and an optional term.
pub struct Object {
/// Name of the object.
Expand Down Expand Up @@ -168,6 +173,8 @@ impl Object {

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[cfg_attr(feature = "python", pyclass(get_all))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi))]
/// Represents an enumeration with a name and mappings.
pub struct Enumeration {
/// Name of the enumeration.
Expand Down
13 changes: 11 additions & 2 deletions src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,23 @@ use crate::{
};
use colored::Colorize;
use log::error;
use serde::Serialize;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::error::Error;
use std::fmt::{Display, Formatter};

#[cfg(feature = "wasm")]
use tsify_next::Tsify;

// Basic types that are ignored in the validation process
pub(crate) const BASIC_TYPES: [&str; 7] = [
"string", "number", "integer", "boolean", "float", "date", "bytes",
];

/// Represents a validation error in the data model.
#[derive(Debug, Clone, Serialize, PartialEq)]
#[cfg_attr(feature = "wasm", derive(Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi))]
pub struct ValidationError {
pub message: String,
pub object: Option<String>,
Expand Down Expand Up @@ -79,7 +84,9 @@ impl Display for ValidationError {
}

/// Enum representing the type of validation error.
#[derive(Debug, Clone, Serialize, PartialEq)]
#[derive(Debug, Clone, Serialize, PartialEq, Deserialize)]
#[cfg_attr(feature = "wasm", derive(Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi))]
pub enum ErrorType {
NameError,
TypeError,
Expand All @@ -101,6 +108,8 @@ impl Display for ErrorType {

/// Validator for checking the integrity of a data model.
#[derive(Debug, Clone, Serialize, PartialEq)]
#[cfg_attr(feature = "wasm", derive(Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi))]
pub struct Validator {
pub is_valid: bool,
pub errors: Vec<ValidationError>,
Expand Down
5 changes: 5 additions & 0 deletions src/xmltype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ use std::str::FromStr;
#[cfg(feature = "python")]
use pyo3::pyclass;

#[cfg(feature = "wasm")]
use tsify_next::Tsify;

/// Represents an XML type, either an attribute or an element.
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "python", pyclass)]
#[cfg_attr(feature = "wasm", derive(Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi))]
pub enum XMLType {
/// An XML attribute with a name.
Attribute { is_attr: bool, name: String },
Expand Down

0 comments on commit 5b37b1f

Please sign in to comment.