Skip to content

Commit 65ca557

Browse files
authored
feat: add (De)Serialize and JsonSchema to Html (#487)
1 parent 560f28b commit 65ca557

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cot-core/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ http-body-util.workspace = true
2929
http-body.workspace = true
3030
http.workspace = true
3131
indexmap.workspace = true
32-
serde.workspace = true
32+
schemars = { workspace = true, optional = true, features = ["derive"] }
33+
serde = { workspace = true, features = ["derive"] }
3334
serde_html_form = { workspace = true, features = ["de", "std"] }
3435
serde_json.workspace = true
3536
serde_path_to_error.workspace = true
@@ -47,3 +48,4 @@ tokio.workspace = true
4748
[features]
4849
default = []
4950
json = []
51+
schemars = ["dep:schemars"]

cot-core/src/html.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ use std::fmt::Write;
5454

5555
use askama::filters::Escaper;
5656
use derive_more::{Deref, Display, From};
57+
#[cfg(feature = "schemars")]
58+
use schemars::JsonSchema;
59+
use serde::{Deserialize, Serialize};
5760

5861
/// A type that represents HTML content as a string.
5962
///
@@ -65,7 +68,23 @@ use derive_more::{Deref, Display, From};
6568
/// let html = Html::new("<div>Hello</div>");
6669
/// assert_eq!(html.as_str(), "<div>Hello</div>");
6770
/// ```
68-
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Deref, From, Display)]
71+
#[derive(
72+
Debug,
73+
Clone,
74+
PartialEq,
75+
Eq,
76+
PartialOrd,
77+
Ord,
78+
Hash,
79+
Default,
80+
Deref,
81+
From,
82+
Display,
83+
Serialize,
84+
Deserialize,
85+
)]
86+
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
87+
#[serde(transparent)]
6988
pub struct Html(pub String);
7089

7190
impl Html {

cot/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ multer.workspace = true
5050
password-auth = { workspace = true, features = ["std", "argon2"] }
5151
pin-project-lite.workspace = true
5252
redis = { workspace = true, features = ["aio", "tokio-comp"], optional = true }
53-
schemars = { workspace = true, optional = true }
53+
schemars = { workspace = true, optional = true, features = ["derive"] }
5454
sea-query = { workspace = true, optional = true }
5555
sea-query-binder = { workspace = true, features = ["with-chrono", "runtime-tokio"], optional = true }
5656
serde = { workspace = true, features = ["derive"] }
@@ -113,7 +113,7 @@ postgres = ["db", "sea-query/backend-postgres", "sea-query-binder/sqlx-postgres"
113113
mysql = ["db", "sea-query/backend-mysql", "sea-query-binder/sqlx-mysql", "sqlx/mysql"]
114114
redis = ["cache", "dep:deadpool-redis", "dep:redis", "json"]
115115
json = ["dep:serde_json", "cot_core/json"]
116-
openapi = ["json", "dep:aide", "dep:schemars"]
116+
openapi = ["json", "cot_core/schemars", "dep:aide", "dep:schemars"]
117117
swagger-ui = ["openapi", "dep:swagger-ui-redist"]
118118
live-reload = ["dep:tower-livereload"]
119119
cache = ["json"]

0 commit comments

Comments
 (0)