-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
96 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
/target | ||
*.lock | ||
*.winmd | ||
winrt |
19 changes: 19 additions & 0 deletions
19
crates/samples/components/json_validator_winrt_client_cpp/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "sample_component_json_validator_winrt_client_cpp" | ||
version = "0.0.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[build-dependencies] | ||
cc = "1.0" | ||
|
||
[build-dependencies.cppwinrt] | ||
path = "../../../../crates/libs/cppwinrt" | ||
|
||
[dependencies.windows-targets] | ||
path = "../../../../crates/libs/targets" | ||
|
||
# TODO: this causes a warning about lack of linkage target. The point is to ensure that this binary dependency is built first but | ||
# Cargo doesn't respect cdylib targets. https://github.com/rust-lang/cargo/issues/7825 | ||
[dependencies.sample_component_json_validator_winrt] | ||
path = "../json_validator_winrt" |
23 changes: 23 additions & 0 deletions
23
crates/samples/components/json_validator_winrt_client_cpp/build.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
fn main() { | ||
if !cfg!(target_env = "msvc") { | ||
return; | ||
} | ||
|
||
println!("cargo:rerun-if-changed=src/client.cpp"); | ||
println!("cargo:rustc-link-lib=windows.0.52.0"); | ||
|
||
cppwinrt::cppwinrt([ | ||
"-in", | ||
"../json_validator_winrt/sample.winmd", | ||
&format!("{}\\System32\\WinMetadata", env!("windir")), | ||
"-out", | ||
"src", | ||
]) | ||
.unwrap(); | ||
|
||
cc::Build::new() | ||
.cpp(true) | ||
.std("c++20") | ||
.file("src/client.cpp") | ||
.compile("client"); | ||
} |
34 changes: 34 additions & 0 deletions
34
crates/samples/components/json_validator_winrt_client_cpp/src/client.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include "winrt/Sample.h" | ||
|
||
using namespace winrt::Sample; | ||
|
||
extern "C" { | ||
void __stdcall client() { | ||
auto schema = LR"( | ||
{ | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
"age": { | ||
"type": "integer" | ||
} | ||
} | ||
} | ||
)"; | ||
|
||
auto value = LR"( | ||
{ | ||
"name": "Kenny", | ||
"age": 21 | ||
} | ||
)"; | ||
|
||
// Create a validator with the given schema. | ||
auto validator = JsonValidator(schema); | ||
|
||
// Validate and check the sanitized return value. | ||
auto sanitized = validator.Validate(value); | ||
assert(sanitized == LR"({"age":21,"name":"Kenny"})"); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
crates/samples/components/json_validator_winrt_client_cpp/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#![cfg(target_env = "msvc")] | ||
|
||
#[test] | ||
fn test() { | ||
extern "system" { | ||
fn client(); | ||
} | ||
unsafe { | ||
client(); | ||
} | ||
} |