Skip to content
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

Add JSON validator sample #2815

Merged
merged 6 commits into from
Jan 24, 2024
Merged

Add JSON validator sample #2815

merged 6 commits into from
Jan 24, 2024

Conversation

kennykerr
Copy link
Collaborator

@kennykerr kennykerr commented Jan 23, 2024

This sample follows on from #2814 (article) and is meant to show how you can implement a traditional Win32-style API in Rust.

This Rust implementation maps to the following hypothetical Win32-style API:

HRESULT __stdcall CreateJsonValidator(char const* schema, size_t schema_len, uintptr_t* handle);
HRESULT __stdcall ValidateJson(uintptr_t handle, char const* value, size_t value_len, char** sanitized_value, size_t* sanitized_value_len);
void __stdcall CloseJsonValidator(uintptr_t handle);

Here's the accompanying article for Getting Started with Rust.

@kennykerr
Copy link
Collaborator Author

I'd appreciate any feedback - need to present a good case for using Rust for systems programming on - and in - Windows.

@ChrisDenton @riverar @tim-weis

@riverar
Copy link
Collaborator

riverar commented Jan 23, 2024

Just throwing out there, you could wire up to WinRT types in Windows.Data.Json for extra pizazz.

@kennykerr
Copy link
Collaborator Author

kennykerr commented Jan 23, 2024

Also, is this a Rust 1.56 bug? https://github.com/microsoft/windows-rs/actions/runs/7632102514/job/20791559920?pr=2815

Why would a dependency in a sample break these unrelated builds?

And I get the same kind of failure on the master branch when I switch to 1.56 as well:

D:\git\windows-rs>cargo check -p windows-sys
warning: D:\git\windows-rs\Cargo.toml: unused manifest key: workspace.lints
warning: D:\git\windows-rs\crates\libs\metadata\Cargo.toml: unused manifest key: lints
warning: D:\git\windows-rs\crates\libs\sys\Cargo.toml: unused manifest key: lints
warning: D:\git\windows-rs\crates\libs\targets\Cargo.toml: unused manifest key: lints
warning: D:\git\windows-rs\crates\libs\implement\Cargo.toml: unused manifest key: lints
warning: D:\git\windows-rs\crates\libs\version\Cargo.toml: unused manifest key: lints
warning: D:\git\windows-rs\crates\libs\bindgen\Cargo.toml: unused manifest key: lints
warning: D:\git\windows-rs\crates\libs\windows\Cargo.toml: unused manifest key: lints
warning: D:\git\windows-rs\crates\libs\interface\Cargo.toml: unused manifest key: lints
warning: D:\git\windows-rs\crates\libs\core\Cargo.toml: unused manifest key: lints
    Updating crates.io index
error: failed to select a version for the requirement `rayon = "=1.8.1"`
candidate versions found which didn't match: 1.8.0, 1.7.0, 1.6.1, ...
location searched: crates.io index
required by package `windows-bindgen v0.52.0 (D:\git\windows-rs\crates\libs\bindgen)`

These older versions of Cargo are quite painful.

@ChrisDenton
Copy link
Collaborator

That error doesn't really make sense to me. The crate version definitely exists and aren't yanked. Maybe going back to old cargo corrupts the cache? Does deleting the .cargo/registry help? I think it would be worth opening an issue on the Cargo repo.

@ChrisDenton
Copy link
Collaborator

Oh, this might be rust-lang/cargo#10623

tl;dr old cargo didn't understand dep syntax and this results in an unhelpful error message. Any chance of bumping the MSRV to 1.60?

Copy link
Collaborator

@ChrisDenton ChrisDenton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I think I'd be tempted to use a type alias for the handle type. Or even a transparent new type wrapper if you don't mind explaining how this prevents accidentally mixing handle types on the Rust side of the FFI.

Speaking of handles, I'd be remiss if I didn't mention that technically speaking it should be a pointer like *mut core::ffi::c_void or something as it is actually a real pointer to real allocated memory.

crates/samples/components/json_validator/src/lib.rs Outdated Show resolved Hide resolved
@kennykerr
Copy link
Collaborator Author

Looks like bumping MSRV to 1.60 fixed the build. Thanks Chris.

Copy link
Member

@jonwis jonwis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is much easier to read than what we have currently. Thanks @kennykerr !

crates/samples/components/json_validator/src/lib.rs Outdated Show resolved Hide resolved
@jonwis
Copy link
Member

jonwis commented Jan 24, 2024

Just throwing out there, you could wire up to WinRT types in Windows.Data.Json for extra pizazz.

Please don't. :) Use literally anything other than Windows.Data.* if you can.

Not everything needed to be in the platform SDK 😞

Copy link
Collaborator

@riverar riverar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me; going to skip the explicit approval to avoid chasing green ✅ here while you finish up some tweaks.

@kennykerr kennykerr merged commit 496cf5a into master Jan 24, 2024
65 checks passed
@kennykerr kennykerr deleted the json_validator branch January 24, 2024 20:03
@kennykerr
Copy link
Collaborator Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants