Some checks failed
Audit / audit-js (push) Failing after 0s
bench / bench (map[os:ubuntu-latest runner:xvfb-run --auto-servernum target:x86_64-unknown-linux-gnu], nightly) (push) Failing after 0s
covector version or publish / run-integration-tests (ubuntu-latest) (push) Failing after 0s
Audit / audit-rust (push) Failing after 0s
test `@tauri-apps/cli` / test (ubuntu-latest) (push) Failing after 0s
test `tauri-cli` / test (map[os:ubuntu-latest target:x86_64-unknown-linux-gnu]) (push) Failing after 0s
test core / test (map[args:--all-features key:all], map[cargo:cargo command:test os:ubuntu-latest target:x86_64-unknown-linux-gnu toolchain:1.90]) (push) Failing after 0s
test core / test (map[args:--no-default-features key:no-default], map[cargo:cargo command:test os:ubuntu-latest target:x86_64-unknown-linux-gnu toolchain:1.90]) (push) Failing after 0s
test core / test (map[args:--no-default-features key:no-default], map[cargo:cross command:build os:ubuntu-latest target:aarch64-linux-android toolchain:1.90]) (push) Failing after 0s
deploy schema worker / deploy (push) Failing after 0s
lint rust / clippy (map[cargo:cargo include: os:ubuntu-latest target:x86_64-unknown-linux-gnu]) (push) Failing after 0s
lint rust / clippy (map[cargo:cross include:--package api os:ubuntu-latest target:aarch64-linux-android]) (push) Failing after 0s
supply chain health status / check rust dependencies with cargo vet (push) Failing after 0s
test `@tauri-apps/api` e2e (mobile) / android (push) Failing after 0s
test `@tauri-apps/api` e2e / test (map[name:Linux os:ubuntu-latest]) (push) Failing after 0s
test core / test (map[args:--all-features key:all], map[cargo:cross command:build os:ubuntu-latest target:aarch64-linux-android toolchain:1.90]) (push) Failing after 0s
Udeps / changes (push) Failing after 0s
Udeps / setup (push) Has been skipped
Udeps / udeps (push) Has been skipped
bench / bench (map[os:macos-latest runner: target:aarch64-apple-darwin], nightly) (push) Has been cancelled
bench / bench (map[os:windows-latest runner: target:x86_64-pc-windows-msvc], nightly) (push) Has been cancelled
covector version or publish / run-integration-tests (macos-latest) (push) Has been cancelled
covector version or publish / run-integration-tests (windows-latest) (push) Has been cancelled
covector version or publish / version-or-publish (push) Has been cancelled
test `@tauri-apps/cli` / test (macos-latest) (push) Has been cancelled
test `tauri-cli` / test (map[args:--no-default-features --features native-tls-vendored os:windows-latest restore-only:true target:aarch64-pc-windows-msvc]) (push) Has been cancelled
test `tauri-cli` / test (map[os:macos-latest target:x86_64-apple-darwin]) (push) Has been cancelled
test `tauri-cli` / test (map[os:windows-latest target:x86_64-pc-windows-msvc]) (push) Has been cancelled
test core / test (map[args:--all-features key:all], map[cargo:cargo command:build os:macos-latest target:aarch64-apple-ios toolchain:1.90]) (push) Has been cancelled
test core / test (map[args:--all-features key:all], map[cargo:cargo command:test os:macos-14 target:aarch64-apple-darwin toolchain:1.90]) (push) Has been cancelled
lint rust / clippy (map[cargo:cargo include: os:macos-latest target:aarch64-apple-darwin]) (push) Has been cancelled
lint rust / clippy (map[cargo:cargo include: os:windows-latest target:x86_64-pc-windows-msvc]) (push) Has been cancelled
lint rust / clippy (map[cargo:cargo include:--package api os:macos-latest target:aarch64-apple-ios]) (push) Has been cancelled
test `@tauri-apps/api` e2e (mobile) / ios (push) Has been cancelled
test `@tauri-apps/api` e2e / test (map[name:Windows os:windows-latest]) (push) Has been cancelled
test `@tauri-apps/api` e2e / test (map[name:macOS os:macos-latest]) (push) Has been cancelled
test `@tauri-apps/cli` / test (windows-latest) (push) Has been cancelled
test core / test (map[args:--all-features key:all], map[cargo:cargo command:test os:windows-latest target:x86_64-pc-windows-msvc toolchain:1.90]) (push) Has been cancelled
test core / test (map[args:--no-default-features key:no-default], map[cargo:cargo command:build os:macos-latest target:aarch64-apple-ios toolchain:1.90]) (push) Has been cancelled
test core / test (map[args:--no-default-features key:no-default], map[cargo:cargo command:test os:macos-14 target:aarch64-apple-darwin toolchain:1.90]) (push) Has been cancelled
test core / test (map[args:--no-default-features key:no-default], map[cargo:cargo command:test os:windows-latest target:x86_64-pc-windows-msvc toolchain:1.90]) (push) Has been cancelled
176 lines
5.1 KiB
Rust
176 lines
5.1 KiB
Rust
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
//! Utilities to implement [`ToTokens`] for a type.
|
|
|
|
use std::path::Path;
|
|
|
|
use proc_macro2::TokenStream;
|
|
use quote::{ToTokens, quote};
|
|
use serde_json::Value as JsonValue;
|
|
use url::Url;
|
|
|
|
/// Write a `TokenStream` of the `$struct`'s fields to the `$tokens`.
|
|
///
|
|
/// All fields must represent a binding of the same name that implements `ToTokens`.
|
|
#[macro_export]
|
|
macro_rules! literal_struct {
|
|
($tokens:ident, $struct:path, $($field:ident),+) => {
|
|
$tokens.append_all(quote! {
|
|
$struct {
|
|
$($field: #$field),+
|
|
}
|
|
})
|
|
};
|
|
}
|
|
|
|
/// Create a `String` constructor `TokenStream`.
|
|
///
|
|
/// e.g. `"Hello World"` -> `String::from("Hello World")`.
|
|
/// This takes a `&String` to reduce casting all the `&String` -> `&str` manually.
|
|
pub fn str_lit(s: impl AsRef<str>) -> TokenStream {
|
|
let s = s.as_ref();
|
|
quote! { #s.into() }
|
|
}
|
|
|
|
/// Create an `Option` constructor `TokenStream`.
|
|
pub fn opt_lit(item: Option<&impl ToTokens>) -> TokenStream {
|
|
match item {
|
|
None => quote! { ::core::option::Option::None },
|
|
Some(item) => quote! { ::core::option::Option::Some(#item) },
|
|
}
|
|
}
|
|
|
|
/// Create an `Option` constructor `TokenStream` over an owned [`ToTokens`] impl type.
|
|
pub fn opt_lit_owned(item: Option<impl ToTokens>) -> TokenStream {
|
|
match item {
|
|
None => quote! { ::core::option::Option::None },
|
|
Some(item) => quote! { ::core::option::Option::Some(#item) },
|
|
}
|
|
}
|
|
|
|
/// Helper function to combine an `opt_lit` with `str_lit`.
|
|
pub fn opt_str_lit(item: Option<impl AsRef<str>>) -> TokenStream {
|
|
opt_lit(item.map(str_lit).as_ref())
|
|
}
|
|
|
|
/// Helper function to combine an `opt_lit` with a list of `str_lit`
|
|
pub fn opt_vec_lit<Raw, Tokens>(
|
|
item: Option<impl IntoIterator<Item = Raw>>,
|
|
map: impl Fn(Raw) -> Tokens,
|
|
) -> TokenStream
|
|
where
|
|
Tokens: ToTokens,
|
|
{
|
|
opt_lit(item.map(|list| vec_lit(list, map)).as_ref())
|
|
}
|
|
|
|
/// Create a `Vec` constructor, mapping items with a function that spits out `TokenStream`s.
|
|
pub fn vec_lit<Raw, Tokens>(
|
|
list: impl IntoIterator<Item = Raw>,
|
|
map: impl Fn(Raw) -> Tokens,
|
|
) -> TokenStream
|
|
where
|
|
Tokens: ToTokens,
|
|
{
|
|
let items = list.into_iter().map(map);
|
|
quote! { vec![#(#items),*] }
|
|
}
|
|
|
|
/// Create a `PathBuf` constructor `TokenStream`.
|
|
///
|
|
/// e.g. `"Hello World" -> String::from("Hello World").
|
|
pub fn path_buf_lit(s: impl AsRef<Path>) -> TokenStream {
|
|
let s = s.as_ref().to_string_lossy().into_owned();
|
|
quote! { ::std::path::PathBuf::from(#s) }
|
|
}
|
|
|
|
/// Creates a `Url` constructor `TokenStream`.
|
|
pub fn url_lit(url: &Url) -> TokenStream {
|
|
let url = url.as_str();
|
|
quote! { #url.parse().unwrap() }
|
|
}
|
|
|
|
/// Create a map constructor, mapping keys and values with other `TokenStream`s.
|
|
///
|
|
/// This function is pretty generic because the types of keys AND values get transformed.
|
|
pub fn map_lit<Map, Key, Value, TokenStreamKey, TokenStreamValue, FuncKey, FuncValue>(
|
|
map_type: TokenStream,
|
|
map: Map,
|
|
map_key: FuncKey,
|
|
map_value: FuncValue,
|
|
) -> TokenStream
|
|
where
|
|
<Map as IntoIterator>::IntoIter: ExactSizeIterator,
|
|
Map: IntoIterator<Item = (Key, Value)>,
|
|
TokenStreamKey: ToTokens,
|
|
TokenStreamValue: ToTokens,
|
|
FuncKey: Fn(Key) -> TokenStreamKey,
|
|
FuncValue: Fn(Value) -> TokenStreamValue,
|
|
{
|
|
let ident = quote::format_ident!("map");
|
|
let map = map.into_iter();
|
|
|
|
if map.len() > 0 {
|
|
let items = map.map(|(key, value)| {
|
|
let key = map_key(key);
|
|
let value = map_value(value);
|
|
quote! { #ident.insert(#key, #value); }
|
|
});
|
|
|
|
quote! {{
|
|
let mut #ident = #map_type::new();
|
|
#(#items)*
|
|
#ident
|
|
}}
|
|
} else {
|
|
quote! { #map_type::new() }
|
|
}
|
|
}
|
|
|
|
/// Create a `serde_json::Value` variant `TokenStream` for a number
|
|
pub fn json_value_number_lit(num: &serde_json::Number) -> TokenStream {
|
|
// See <https://docs.rs/serde_json/1/serde_json/struct.Number.html> for guarantees
|
|
let prefix = quote! { ::serde_json::Value };
|
|
if num.is_u64() {
|
|
// guaranteed u64
|
|
let num = num.as_u64().unwrap();
|
|
quote! { #prefix::Number(#num.into()) }
|
|
} else if num.is_i64() {
|
|
// guaranteed i64
|
|
let num = num.as_i64().unwrap();
|
|
quote! { #prefix::Number(#num.into()) }
|
|
} else if num.is_f64() {
|
|
// guaranteed f64
|
|
let num = num.as_f64().unwrap();
|
|
quote! { #prefix::Number(::serde_json::Number::from_f64(#num).unwrap(/* safe to unwrap, guaranteed f64 */)) }
|
|
} else {
|
|
// invalid number
|
|
quote! { #prefix::Null }
|
|
}
|
|
}
|
|
|
|
/// Create a `serde_json::Value` constructor `TokenStream`
|
|
pub fn json_value_lit(jv: &JsonValue) -> TokenStream {
|
|
let prefix = quote! { ::serde_json::Value };
|
|
|
|
match jv {
|
|
JsonValue::Null => quote! { #prefix::Null },
|
|
JsonValue::Bool(bool) => quote! { #prefix::Bool(#bool) },
|
|
JsonValue::Number(number) => json_value_number_lit(number),
|
|
JsonValue::String(str) => {
|
|
let s = str_lit(str);
|
|
quote! { #prefix::String(#s) }
|
|
}
|
|
JsonValue::Array(vec) => {
|
|
let items = vec.iter().map(json_value_lit);
|
|
quote! { #prefix::Array(vec![#(#items),*]) }
|
|
}
|
|
JsonValue::Object(map) => {
|
|
let map = map_lit(quote! { ::serde_json::Map }, map, str_lit, json_value_lit);
|
|
quote! { #prefix::Object(#map) }
|
|
}
|
|
}
|
|
}
|