From 6ae2905f69f1fca37c3c82e485940753b6280b32 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Thu, 23 May 2024 15:31:55 -0500 Subject: [PATCH] sample --- .github/workflows/clippy.yml | 2 ++ .github/workflows/test.yml | 10 +++--- .gitignore | 1 + .../Cargo.toml | 19 +++++++++++ .../json_validator_winrt_client_cpp/build.rs | 23 +++++++++++++ .../src/client.cpp | 34 +++++++++++++++++++ .../src/lib.rs | 11 ++++++ 7 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 crates/samples/components/json_validator_winrt_client_cpp/Cargo.toml create mode 100644 crates/samples/components/json_validator_winrt_client_cpp/build.rs create mode 100644 crates/samples/components/json_validator_winrt_client_cpp/src/client.cpp create mode 100644 crates/samples/components/json_validator_winrt_client_cpp/src/lib.rs diff --git a/.github/workflows/clippy.yml b/.github/workflows/clippy.yml index 8898385d02..b437916967 100644 --- a/.github/workflows/clippy.yml +++ b/.github/workflows/clippy.yml @@ -52,6 +52,8 @@ jobs: run: cargo clippy -p sample_component_json_validator_winrt - name: Clippy sample_component_json_validator_winrt_client run: cargo clippy -p sample_component_json_validator_winrt_client + - name: Clippy sample_component_json_validator_winrt_client_cpp + run: cargo clippy -p sample_component_json_validator_winrt_client_cpp - name: Clippy sample_consent run: cargo clippy -p sample_consent - name: Clippy sample_core_app diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3888009540..c96865f987 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -76,6 +76,8 @@ jobs: run: cargo test -p sample_component_json_validator_winrt --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test sample_component_json_validator_winrt_client run: cargo test -p sample_component_json_validator_winrt_client --target ${{ matrix.target }} ${{ matrix.etc }} + - name: Test sample_component_json_validator_winrt_client_cpp + run: cargo test -p sample_component_json_validator_winrt_client_cpp --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test sample_consent run: cargo test -p sample_consent --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test sample_core_app @@ -154,10 +156,10 @@ jobs: run: cargo test -p test_alternate_success_code --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_arch run: cargo test -p test_arch --target ${{ matrix.target }} ${{ matrix.etc }} - - name: Test test_arch_feature - run: cargo test -p test_arch_feature --target ${{ matrix.target }} ${{ matrix.etc }} - name: Clean run: cargo clean + - name: Test test_arch_feature + run: cargo test -p test_arch_feature --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_array run: cargo test -p test_array --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_bcrypt @@ -256,10 +258,10 @@ jobs: run: cargo test -p test_standalone --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_string_param run: cargo test -p test_string_param --target ${{ matrix.target }} ${{ matrix.etc }} - - name: Test test_structs - run: cargo test -p test_structs --target ${{ matrix.target }} ${{ matrix.etc }} - name: Clean run: cargo clean + - name: Test test_structs + run: cargo test -p test_structs --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_sys run: cargo test -p test_sys --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_targets diff --git a/.gitignore b/.gitignore index 4c6fca839a..ca33c1fc2c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /target *.lock *.winmd +winrt diff --git a/crates/samples/components/json_validator_winrt_client_cpp/Cargo.toml b/crates/samples/components/json_validator_winrt_client_cpp/Cargo.toml new file mode 100644 index 0000000000..6e5dbd2125 --- /dev/null +++ b/crates/samples/components/json_validator_winrt_client_cpp/Cargo.toml @@ -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" diff --git a/crates/samples/components/json_validator_winrt_client_cpp/build.rs b/crates/samples/components/json_validator_winrt_client_cpp/build.rs new file mode 100644 index 0000000000..f39067a2a5 --- /dev/null +++ b/crates/samples/components/json_validator_winrt_client_cpp/build.rs @@ -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"); +} diff --git a/crates/samples/components/json_validator_winrt_client_cpp/src/client.cpp b/crates/samples/components/json_validator_winrt_client_cpp/src/client.cpp new file mode 100644 index 0000000000..615a37230b --- /dev/null +++ b/crates/samples/components/json_validator_winrt_client_cpp/src/client.cpp @@ -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"})"); + } +} diff --git a/crates/samples/components/json_validator_winrt_client_cpp/src/lib.rs b/crates/samples/components/json_validator_winrt_client_cpp/src/lib.rs new file mode 100644 index 0000000000..6f1497b02a --- /dev/null +++ b/crates/samples/components/json_validator_winrt_client_cpp/src/lib.rs @@ -0,0 +1,11 @@ +#![cfg(target_env = "msvc")] + +#[test] +fn test() { + extern "system" { + fn client(); + } + unsafe { + client(); + } +}