瀏覽代碼

fix hex color schema pattern

amrbashir 9 月之前
父節點
當前提交
b2e2091989

+ 5 - 4
Cargo.lock

@@ -8759,6 +8759,7 @@ dependencies = [
 [[package]]
 name = "tao"
 version = "0.30.3"
+source = "git+https://github.com/tauri-apps/tao?branch=feat/background-color#b59ce5dd9a2f6b21c209be4445034c3c3d6992d5"
 dependencies = [
  "bitflags 2.6.0",
  "cocoa 0.26.0",
@@ -8784,7 +8785,7 @@ dependencies = [
  "parking_lot",
  "raw-window-handle",
  "scopeguard",
- "tao-macros 0.1.3",
+ "tao-macros 0.1.3 (git+https://github.com/tauri-apps/tao?branch=feat/background-color)",
  "unicode-segmentation",
  "url",
  "windows",
@@ -8796,6 +8797,8 @@ dependencies = [
 [[package]]
 name = "tao-macros"
 version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f4e16beb8b2ac17db28eab8bca40e62dbfbb34c0fcdc6d9826b11b7b5d047dfd"
 dependencies = [
  "proc-macro2",
  "quote",
@@ -8805,8 +8808,7 @@ dependencies = [
 [[package]]
 name = "tao-macros"
 version = "0.1.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f4e16beb8b2ac17db28eab8bca40e62dbfbb34c0fcdc6d9826b11b7b5d047dfd"
+source = "git+https://github.com/tauri-apps/tao?branch=feat/background-color#b59ce5dd9a2f6b21c209be4445034c3c3d6992d5"
 dependencies = [
  "proc-macro2",
  "quote",
@@ -9363,7 +9365,6 @@ dependencies = [
  "kuchikiki",
  "log",
  "memchr",
- "once_cell",
  "phf 0.11.2",
  "proc-macro2",
  "quote",

+ 5 - 7
crates/tauri-cli/config.schema.json

@@ -838,10 +838,12 @@
       ]
     },
     "Color": {
-      "$ref": "#/definitions/InnerColor"
-    },
-    "InnerColor": {
       "anyOf": [
+        {
+          "description": "Color hex string, for example: #fff, #ffffff, or #ffffffff.",
+          "type": "string",
+          "pattern": "#?([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$"
+        },
         {
           "description": "Array of RGB colors. Each value has minimum of 0 and maximum of 255.",
           "type": "array",
@@ -924,10 +926,6 @@
               "minimum": 0.0
             }
           }
-        },
-        {
-          "description": "Color hex string, for example: #fff, #ffffff, or #ffffffff.",
-          "type": "string"
         }
       ]
     },

+ 5 - 7
crates/tauri-schema-generator/schemas/config.schema.json

@@ -838,10 +838,12 @@
       ]
     },
     "Color": {
-      "$ref": "#/definitions/InnerColor"
-    },
-    "InnerColor": {
       "anyOf": [
+        {
+          "description": "Color hex string, for example: #fff, #ffffff, or #ffffffff.",
+          "type": "string",
+          "pattern": "#?([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$"
+        },
         {
           "description": "Array of RGB colors. Each value has minimum of 0 and maximum of 255.",
           "type": "array",
@@ -924,10 +926,6 @@
               "minimum": 0.0
             }
           }
-        },
-        {
-          "description": "Color hex string, for example: #fff, #ffffff, or #ffffffff.",
-          "type": "string"
         }
       ]
     },

+ 1 - 1
crates/tauri-schema-worker/src/config.rs

@@ -106,7 +106,7 @@ async fn try_next_schema() -> anyhow::Result<String> {
 
 async fn schema_file_for_version(version: Version) -> anyhow::Result<String> {
   let cache = Cache::open("schema".to_string()).await;
-  let cache_key = format!("https://scheam.tauri.app/config/{version}");
+  let cache_key = format!("https://schema.tauri.app/config/{version}");
   if let Some(mut cached) = cache.get(cache_key.clone(), true).await? {
     console_log!("Serving schema for {version} from cache");
     return cached.text().await.map_err(Into::into);

+ 1 - 2
crates/tauri-utils/Cargo.toml

@@ -46,7 +46,6 @@ log = "0.4.21"
 cargo_metadata = { version = "0.18", optional = true }
 serde-untagged = "0.1"
 uuid = { version = "1", features = ["serde"] }
-once_cell = { version = "1", optional = true }
 
 [target."cfg(target_os = \"macos\")".dependencies]
 swift-rs = { version = "1.0.7", optional = true, features = ["build"] }
@@ -58,7 +57,7 @@ serial_test = "3.1"
 [features]
 build = ["proc-macro2", "quote", "cargo_metadata", "schema", "swift-rs"]
 compression = ["brotli"]
-schema = ["schemars", "once_cell"]
+schema = ["schemars"]
 isolation = ["aes-gcm", "getrandom", "serialize-to-javascript"]
 process-relaunch-dangerous-allow-symlink-macos = []
 config-json5 = ["json5"]

+ 15 - 11
crates/tauri-utils/src/config.rs

@@ -1323,15 +1323,12 @@ fn default_alpha() -> u8 {
   255
 }
 
-#[cfg(feature = "schema")]
-static HEX_COLOR_RE: once_cell::sync::Lazy<regex::Regex> = once_cell::sync::Lazy::new(|| {
-  regex::Regex::new(r"^#?([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$").unwrap()
-});
-
 #[derive(Deserialize)]
 #[cfg_attr(feature = "schema", derive(JsonSchema))]
 #[serde(untagged)]
 enum InnerColor {
+  /// Color hex string, for example: #fff, #ffffff, or #ffffffff.
+  String(String),
   /// Array of RGB colors. Each value has minimum of 0 and maximum of 255.
   Rgb((u8, u8, u8)),
   /// Array of RGBA colors. Each value has minimum of 0 and maximum of 255.
@@ -1344,9 +1341,6 @@ enum InnerColor {
     #[serde(default = "default_alpha")]
     alpha: u8,
   },
-  /// Color hex string, for example: #fff, #ffffff, or #ffffffff.
-  #[cfg_attr(feature = "schema", validate(regex(path = *HEX_COLOR_RE)))]
-  String(String),
 }
 
 impl<'de> Deserialize<'de> for Color {
@@ -1356,6 +1350,7 @@ impl<'de> Deserialize<'de> for Color {
   {
     let color = InnerColor::deserialize(deserializer)?;
     let color = match color {
+      InnerColor::String(string) => string.parse().map_err(serde::de::Error::custom)?,
       InnerColor::Rgb(rgb) => Color(rgb.0, rgb.1, rgb.2, 255),
       InnerColor::Rgba(rgb) => rgb.into(),
       InnerColor::RgbaObject {
@@ -1364,7 +1359,6 @@ impl<'de> Deserialize<'de> for Color {
         blue,
         alpha,
       } => Color(red, green, blue, alpha),
-      InnerColor::String(string) => string.parse().map_err(serde::de::Error::custom)?,
     };
 
     Ok(color)
@@ -1377,8 +1371,18 @@ impl schemars::JsonSchema for Color {
     "Color".to_string()
   }
 
-  fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
-    gen.subschema_for::<InnerColor>()
+  fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
+    let mut schema = schemars::schema_for!(InnerColor).schema;
+    schema.metadata = None; // Remove `title: InnerColor` from schema
+
+    // add hex color pattern validation
+    let any_of = schema.subschemas().any_of.as_mut().unwrap();
+    let schemars::schema::Schema::Object(str_schema) = any_of.first_mut().unwrap() else {
+      unreachable!()
+    };
+    str_schema.string().pattern = Some("#?([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$".into());
+
+    schema.into()
   }
 }