Buckets:
| use bincode::{Decode, Encode}; | |
| use serde::{Deserialize, Serialize}; | |
| use turbo_rcstr::{RcStr, rcstr}; | |
| use turbo_tasks::{NonLocalValue, TaskInput, trace::TraceRawVcs}; | |
| /// The top-most structure encoded into the query param in requests to | |
| /// `next/font/local` generated by the next/font swc transform. e.g. | |
| /// `next/font/local/target.css?{"path": "index.js", "arguments": {"src":... | |
| pub(super) struct NextFontLocalRequest { | |
| pub arguments: (NextFontLocalRequestArguments,), | |
| pub variable_name: RcStr, | |
| } | |
| pub(super) struct NextFontLocalDeclaration { | |
| pub prop: RcStr, | |
| pub value: RcStr, | |
| } | |
| pub(super) struct NextFontLocalRequestArguments { | |
| pub src: SrcRequest, | |
| pub weight: Option<RcStr>, | |
| pub style: Option<RcStr>, | |
| pub display: RcStr, | |
| pub preload: bool, | |
| pub fallback: Option<Vec<RcStr>>, | |
| pub adjust_font_fallback: AdjustFontFallback, | |
| pub variable: Option<RcStr>, | |
| pub declarations: Option<Vec<NextFontLocalDeclaration>>, | |
| } | |
| pub(super) enum SrcRequest { | |
| One(RcStr), | |
| Many(Vec<SrcDescriptor>), | |
| } | |
| pub(super) struct SrcDescriptor { | |
| pub path: RcStr, | |
| pub weight: Option<RcStr>, | |
| pub style: Option<RcStr>, | |
| } | |
| /// The user's desired fallback font | |
| pub(super) enum AdjustFontFallback { | |
| Arial, | |
| TimesNewRoman, | |
| None, | |
| } | |
| fn default_adjust_font_fallback() -> AdjustFontFallback { | |
| AdjustFontFallback::Arial | |
| } | |
| /// Deserializes and validates JS (bool | string) into [[AdjustFontFallback]]'s | |
| /// None, Arial, TimesNewRoman | |
| fn deserialize_adjust_font_fallback<'de, D>( | |
| de: D, | |
| ) -> std::result::Result<AdjustFontFallback, D::Error> | |
| where | |
| D: serde::Deserializer<'de>, | |
| { | |
| enum AdjustFontFallbackInner { | |
| Named(String), | |
| None(bool), | |
| } | |
| match AdjustFontFallbackInner::deserialize(de)? { | |
| AdjustFontFallbackInner::Named(name) => match name.as_str() { | |
| "Arial" => Ok(AdjustFontFallback::Arial), | |
| "Times New Roman" => Ok(AdjustFontFallback::TimesNewRoman), | |
| _ => Err(serde::de::Error::invalid_value( | |
| serde::de::Unexpected::Other("adjust_font_fallback"), | |
| &"Expected either \"Arial\" or \"Times New Roman\"", | |
| )), | |
| }, | |
| AdjustFontFallbackInner::None(val) => { | |
| if val { | |
| Err(serde::de::Error::invalid_value( | |
| serde::de::Unexpected::Other("adjust_font_fallback"), | |
| &"Expected string or `false`. Received `true`", | |
| )) | |
| } else { | |
| Ok(AdjustFontFallback::None) | |
| } | |
| } | |
| } | |
| } | |
| fn default_preload() -> bool { | |
| true | |
| } | |
| fn default_display() -> RcStr { | |
| rcstr!("swap") | |
| } | |
| mod tests { | |
| use anyhow::Result; | |
| use serde::Deserialize; | |
| use super::{ | |
| AdjustFontFallback, default_adjust_font_fallback, deserialize_adjust_font_fallback, | |
| }; | |
| struct TestFallback { | |
| pub adjust_font_fallback: AdjustFontFallback, | |
| } | |
| fn test_deserialize_adjust_font_fallback_fails_on_true() { | |
| match serde_json::from_str::<TestFallback>(r#"{"adjustFontFallback": true}"#) { | |
| Ok(_) => panic!("Should fail"), | |
| Err(error) => assert!(error.to_string().contains( | |
| "invalid value: adjust_font_fallback, expected Expected string or `false`. \ | |
| Received `true`" | |
| )), | |
| }; | |
| } | |
| fn test_deserialize_adjust_font_fallback_fails_on_unknown_string() { | |
| match serde_json::from_str::<TestFallback>(r#"{"adjustFontFallback": "Roboto"}"#) { | |
| Ok(_) => panic!("Should fail"), | |
| Err(error) => assert!( | |
| error.to_string().contains( | |
| r#"invalid value: adjust_font_fallback, expected Expected either "Arial" or "Times New Roman""# | |
| ) | |
| ), | |
| }; | |
| } | |
| fn test_deserializes_false_as_none() -> Result<()> { | |
| assert_eq!( | |
| serde_json::from_str::<TestFallback>(r#"{"adjustFontFallback": false}"#)?, | |
| TestFallback { | |
| adjust_font_fallback: AdjustFontFallback::None | |
| } | |
| ); | |
| Ok(()) | |
| } | |
| fn test_deserializes_arial() -> Result<()> { | |
| assert_eq!( | |
| serde_json::from_str::<TestFallback>(r#"{"adjustFontFallback": "Arial"}"#)?, | |
| TestFallback { | |
| adjust_font_fallback: AdjustFontFallback::Arial | |
| } | |
| ); | |
| Ok(()) | |
| } | |
| } | |
Xet Storage Details
- Size:
- 5.57 kB
- Xet hash:
- 827f751da9e1cbceedf77047cbf82c406faf5feae0f84c405c781ef9a8ee8ecb
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.