-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add wasm-pack to allow publishing to npm #351
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏🏻 Some comments inline, I think we'll want to move some of the logic once this is in a working state and we've worked out any issues with binary size or embedding into the lab.
|
||
[profile.release] | ||
# Tell `rustc` to optimize for small code size. | ||
opt-level = "s" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may not want to use the same profile for the stellar-xdr CLI and the wasm crate, so having them in a shared workspace may not be ideal. Let's not change this now, but we might need to change this before we merge.
pub struct ReplaceAdditionalProperties; | ||
|
||
impl Visitor for ReplaceAdditionalProperties { | ||
fn visit_schema_object(&mut self, schema: &mut schemars::schema::SchemaObject) { | ||
schema.object().additional_properties = None; | ||
add_titles(schema.subschemas()); | ||
visit::visit_schema_object(self, schema); | ||
} | ||
} | ||
|
||
/// This function will add titles to all one_of schemas in the schema. So that it is more readable. | ||
/// E.g. it was `Option 1`, etc before | ||
fn add_titles(sub_schema: &mut schemars::schema::SubschemaValidation) { | ||
if let Some(ref mut one_of) = sub_schema.one_of { | ||
one_of.iter_mut().for_each(|schema| { | ||
if let schemars::schema::Schema::Object(ref mut obj) = schema { | ||
if let Some(inner_obj) = &mut obj.object { | ||
if let Some(title) = inner_obj.required.first() { | ||
obj.metadata().title = Some(title.clone()); | ||
} | ||
} else if let Some(enum_values) = &obj.enum_values { | ||
if let Some(title) = enum_values.get(2) { | ||
obj.metadata().title = Some(title.to_string()); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. We should move this into the generated schema so that no matter how the schema is being generated this same logic is in use. I'll have a look at copying this into:
pub struct Schema(pub schemars::schema::RootSchema); | ||
|
||
impl TryFrom<crate::curr::TypeVariant> for Schema { | ||
type Error = Error; | ||
fn try_from(variant: crate::curr::TypeVariant) -> Result<Schema, Error> { | ||
println!("hhh"); | ||
let settings = SchemaSettings::draft07().with_visitor(ReplaceAdditionalProperties); | ||
let generator = settings.into_generator(); | ||
Ok(Schema(variant.json_schema(generator))) | ||
} | ||
} | ||
|
||
impl FromStr for Schema { | ||
type Err = Error; | ||
fn from_str(s: &str) -> Result<Schema, Error> { | ||
s.parse::<crate::curr::TypeVariant>() | ||
.map_err(|_| { | ||
Error::UnknownType(s.to_string(), &crate::curr::TypeVariant::VARIANTS_STR) | ||
})? | ||
.try_into() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's depending on the TryFrom and the FromStr?
I'm not sure how we can support different versions of schema with them, and it looks like they primarily exist to inject the ReplaceAdditionalProperties which we should really do at the generation of the schema in xdrgen.
We'd also need these to be embedded under curr and next.
pub fn from_xdr(xdr_base64: String, variant: Option<String>) -> String { | ||
let mut f = curr::Limited::new(ResetRead::new(xdr_base64.as_bytes()), curr::Limits::none()); | ||
for variant in variant | ||
.map(|v| Some(vec![v.parse::<crate::curr::TypeVariant>().ok()?])) | ||
.unwrap_or_else(|| Some(TypeVariant::VARIANTS.to_vec())) | ||
.unwrap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the from_xdr function, by providing an auto-search for a value, is providing higher level functionality at a lower level than we need to. If we instead expose a pub fn types() -> Vec<TypeVariant>
, then any caller will be able to iterate over the variants and try them out. Some advantages: keep the logic in the lib simpler, no need to maintain and document string values that have a special form {"error":...}
that could actually collide with future valid XDR json.
.iter() | ||
.map(|s| format!("{s:?}")) | ||
.collect::<Vec<String>>() | ||
.join(", ") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should remove the space so that the list is comma separated values that don't need to be trimmed. A UI / presentation layer can add in any spaces between values if it is presenting the values that way.
.join(", ") | |
.join(",") |
.iter() | ||
.map(|s| format!("{s:?}")) | ||
.collect::<Vec<String>>() | ||
.join(", ") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be able to return a JsValue
and use the Array
type to build an array of strings to return to the JS side. Is that compatible with what we're doing here?
I'm picking this change up, but am continuing in #360 because for some reason even though the checkbox is showing a green tick next to "maintainers are allowed to edit this pull request," I seem to get a 403 error whenever I try to push to the branch. |
Currently
serde_json_wasm
caused an unreachable error. However,serde_json
is working.Size of current release wasm 1.6 MB.