Skip to content
Merged
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
65 changes: 65 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ serde_yaml_ng = { version = "0.10" }
browser_sdk_schema = { path = "./crates/browser_sdk_schema" }
browser_sdk_ts_codegen = { path = "./crates/browser_sdk_ts_codegen" }
browser_sdk_ts_codegen_macros = { path = "./crates/browser_sdk_ts_codegen_macros" }
browser_sdk_utils = { path = "./crates/browser_sdk_utils" }
markdown = { git = "https://github.com/cirnov/markdown-rs" }
mdast_util_to_markdown = { git = "https://github.com/cirnov/markdown-rs" }
1 change: 1 addition & 0 deletions crates/browser_sdk_dart_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ edition = "2024"

[dependencies]
browser_sdk_schema.workspace = true
browser_sdk_utils.workspace = true
convert_case = { workspace = true }
8 changes: 4 additions & 4 deletions crates/browser_sdk_dart_codegen/src/ast/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ mod tests {
fn enum_with_variant() {
let empty = Enum {
name: Identifier::try_from("Test1").unwrap(),
description: Some(Comment("Test1 Enum".into())),
description: Some(Comment::try_from("Test1 Enum").unwrap()),
variants: vec![
EnumVariant {
name: Identifier::try_from("VARIANT_A").unwrap(),
Expand All @@ -76,9 +76,9 @@ mod tests {
EnumVariant {
name: Identifier::try_from("VARIANT_B").unwrap(),
value: "value_b".into(),
description: Some(Comment(
"This is a variant\nwith a multi-line description".into(),
)),
description: Some(Comment::try_from(
"This is a variant\nwith a multi-line description",
).unwrap()),
},
],
union_parents: vec![],
Expand Down
25 changes: 24 additions & 1 deletion crates/browser_sdk_dart_codegen/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod intersection;
mod object;
mod union;

use browser_sdk_utils::{MdastNodeExt, ToMdastExt};
pub use r#enum::*;
pub use ident::*;
pub use intersection::*;
Expand All @@ -22,14 +23,36 @@ impl fmt::Display for Indent {
}

#[derive(Debug, Clone)]
pub struct Comment(pub String);
pub struct Comment(String);

impl Comment {
pub fn lines(&self) -> impl Iterator<Item = &str> {
self.0.trim().lines().map(str::trim)
}
}

impl TryFrom<&str> for Comment {
type Error = String;

fn try_from(value: &str) -> Result<Self, Self::Error> {
let cleaned = value
.to_mdast()
.map_err(|e| format!("Failed to parse markdown: {}", e))?
.remove_jsx_elements()
.to_markdown_string()
.map_err(|e| format!("Failed to convert markdown to string: {}", e))?;
Ok(Comment(cleaned))
}
}

impl TryFrom<String> for Comment {
type Error = String;

fn try_from(value: String) -> Result<Self, Self::Error> {
Comment::try_from(value.as_str())
}
}

#[derive(Debug, Clone)]
pub struct TypeReference {
pub path: String,
Expand Down
26 changes: 13 additions & 13 deletions crates/browser_sdk_dart_codegen/src/ast/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ mod tests {
fn empty_object() {
let object = Object {
name: Identifier::try_from("Test").unwrap(),
description: Some(Comment("Test Object".into())),
description: Some(Comment::try_from("Test Object").unwrap()),
fields: vec![],
is_one_of: false,
union_parents: vec![UnionParent::Union {
Expand All @@ -229,7 +229,7 @@ class Test {
fn object_with_fields() {
let object = Object {
name: Identifier::try_from("Address").unwrap(),
description: Some(Comment("주소 정보".into())),
description: Some(Comment::try_from("주소 정보").unwrap()),
fields: vec![
ObjectField {
name: Identifier::try_from("country").unwrap(),
Expand All @@ -252,7 +252,7 @@ class Test {
is_list: false,
is_required: true,
},
description: Some(Comment("**일반주소**".into())),
description: Some(Comment::try_from("**일반주소**").unwrap()),
},
ObjectField {
name: Identifier::try_from("addressLine2").unwrap(),
Expand All @@ -262,7 +262,7 @@ class Test {
is_list: false,
is_required: true,
},
description: Some(Comment("**상세주소**".into())),
description: Some(Comment::try_from("**상세주소**").unwrap()),
},
ObjectField {
name: Identifier::try_from("city").unwrap(),
Expand All @@ -272,7 +272,7 @@ class Test {
is_list: false,
is_required: false,
},
description: Some(Comment("**도시**".into())),
description: Some(Comment::try_from("**도시**").unwrap()),
},
ObjectField {
name: Identifier::try_from("province").unwrap(),
Expand All @@ -282,7 +282,7 @@ class Test {
is_list: false,
is_required: false,
},
description: Some(Comment("**주, 도, 시**".into())),
description: Some(Comment::try_from("**주, 도, 시**").unwrap()),
},
],
is_one_of: false,
Expand Down Expand Up @@ -326,7 +326,7 @@ class Address {
fn one_of_object() {
let object = Object {
name: Identifier::try_from("MonthOption").unwrap(),
description: Some(Comment("**할부 개월 수 설정**".into())),
description: Some(Comment::try_from("**할부 개월 수 설정**").unwrap()),
fields: vec![
ObjectField {
name: Identifier::try_from("fixedMonth").unwrap(),
Expand All @@ -336,9 +336,9 @@ class Address {
is_list: false,
is_required: true,
},
description: Some(Comment(
"**구매자가 선택할 수 없도록 고정된 할부 개월수**".into(),
)),
description: Some(Comment::try_from(
"**구매자가 선택할 수 없도록 고정된 할부 개월수**",
).unwrap()),
},
ObjectField {
name: Identifier::try_from("availableMonthList").unwrap(),
Expand All @@ -348,9 +348,9 @@ class Address {
is_list: true,
is_required: true,
},
description: Some(Comment(
"**구매자가 선택할 수 있는 할부 개월수 리스트**".into(),
)),
description: Some(Comment::try_from(
"**구매자가 선택할 수 있는 할부 개월수 리스트**",
).unwrap()),
},
],
is_one_of: true,
Expand Down
24 changes: 12 additions & 12 deletions crates/browser_sdk_dart_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl ResourceProcessor {
name: field_name,
serialized_name: name.to_string(),
value_type,
description: parameter.description.clone().map(Comment),
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
};
}
ParameterType::ResourceRef(r) => {
Expand All @@ -135,7 +135,7 @@ impl ResourceProcessor {
name: field_name,
serialized_name: name.to_string(),
value_type,
description: parameter.description.clone().map(Comment),
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
}
}

Expand Down Expand Up @@ -228,21 +228,21 @@ impl ResourceProcessor {
match &parameter.r#type {
ParameterType::Object { properties, hide_if_empty: _ } => Some(Entity::Object(Object {
name: name.clone(),
description: parameter.description.clone().map(Comment),
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
fields: Self::build_field_list(properties.iter()),
is_one_of: false,
union_parents: vec![],
})),
ParameterType::EmptyObject => Some(Entity::Object(Object {
name: name.clone(),
description: parameter.description.clone().map(Comment),
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
fields: vec![],
is_one_of: false,
union_parents: vec![],
})),
ParameterType::Enum { variants, .. } => Some(Entity::Enum(Enum {
name: name.clone(),
description: parameter.description.clone().map(Comment),
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
variants: variants
.iter()
.map(|(value, variant)| EnumVariant {
Expand All @@ -252,21 +252,21 @@ impl ResourceProcessor {
Identifier::try_from(value.as_str()).unwrap()
},
value: value.clone(),
description: variant.description.clone().map(Comment),
description: variant.description.clone().map(|d| Comment::try_from(d).unwrap()),
})
.collect(),
union_parents: vec![],
})),
ParameterType::OneOf { properties, hide_if_empty: _ } => Some(Entity::Object(Object {
name: name.clone(),
description: parameter.description.clone().map(Comment),
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
fields: Self::build_field_list(properties.iter()),
is_one_of: true,
union_parents: vec![],
})),
ParameterType::Union { types, hide_if_empty: _ } => Some(Entity::Union(Union {
name: name.clone(),
description: parameter.description.clone().map(Comment),
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
variants: types
.iter()
.map(|parameter| match &parameter.r#type {
Expand All @@ -279,7 +279,7 @@ impl ResourceProcessor {
.to_case(Case::Camel)
.try_into()
.unwrap(),
description: parameter.description.clone().map(Comment),
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
type_name: type_reference,
}
}
Expand All @@ -289,7 +289,7 @@ impl ResourceProcessor {
})),
ParameterType::Intersection { types, hide_if_empty: _ } => Some(Entity::Intersection(Intersection {
name: name.clone(),
description: parameter.description.clone().map(Comment),
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
constituents: types
.iter()
.map(|parameter| match &parameter.r#type {
Expand Down Expand Up @@ -318,7 +318,7 @@ impl ResourceProcessor {
hide_if_empty: _,
} => Some(Entity::DiscriminatedUnion(DiscriminatedUnion {
name: name.clone(),
description: parameter.description.clone().map(Comment),
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
discriminator: Identifier::try_from(discriminator.as_str()).unwrap(),
variants: types
.iter()
Expand All @@ -329,7 +329,7 @@ impl ResourceProcessor {
discriminator_value: value.clone(),
name: value.to_case(Case::Camel).try_into().unwrap(),
type_name: type_reference,
description: parameter.description.clone().map(Comment),
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
}
}
_ => unreachable!(),
Expand Down
1 change: 1 addition & 0 deletions crates/browser_sdk_ts_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ indexmap = { workspace = true }
indoc = { workspace = true }
browser_sdk_schema = { workspace = true }
browser_sdk_ts_codegen_macros = { workspace = true }
browser_sdk_utils.workspace = true
pretty_assertions = { workspace = true }
pathdiff = "0.2.1"
Loading