blob: 0b127f97c7fcea4e31504edf67b6339308d5b1d2 [file] [log] [blame]
Chris Wailes977026a2023-02-13 09:13:10 -08001// This file is part of ICU4X. For terms of use, please see the file
2// called LICENSE at the top level of the ICU4X source tree
3// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5use crate::TinyAsciiStr;
Stephen Hines51393642024-02-02 00:10:59 -08006use crate::UnvalidatedTinyAsciiStr;
Chris Wailes977026a2023-02-13 09:13:10 -08007use databake::*;
8
9impl<const N: usize> Bake for TinyAsciiStr<N> {
10 fn bake(&self, env: &CrateEnv) -> TokenStream {
11 env.insert("tinystr");
12 let string = self.as_str();
13 quote! {
Stephen Hines51393642024-02-02 00:10:59 -080014 tinystr::tinystr!(#N, #string)
15 }
16 }
17}
18
19impl<const N: usize> databake::Bake for UnvalidatedTinyAsciiStr<N> {
20 fn bake(&self, env: &databake::CrateEnv) -> databake::TokenStream {
21 match self.try_into_tinystr() {
22 Ok(tiny) => {
23 let tiny = tiny.bake(env);
24 databake::quote! {
25 #tiny.to_unvalidated()
26 }
27 }
28 Err(_) => {
29 let bytes = self.0.bake(env);
30 env.insert("tinystr");
31 databake::quote! {
32 tinystr::UnvalidatedTinyAsciiStr::from_bytes_unchecked(*#bytes)
33 }
34 }
Chris Wailes977026a2023-02-13 09:13:10 -080035 }
36 }
37}
38
39#[test]
40fn test() {
41 test_bake!(TinyAsciiStr<10>, const: crate::tinystr!(10usize, "foo"), tinystr);
42}
Stephen Hines51393642024-02-02 00:10:59 -080043
44#[test]
45fn test_unvalidated() {
46 test_bake!(UnvalidatedTinyAsciiStr<10>, const: crate::tinystr!(10usize, "foo").to_unvalidated(), tinystr);
47 test_bake!(UnvalidatedTinyAsciiStr<3>, const: crate::UnvalidatedTinyAsciiStr::from_bytes_unchecked(*b"AB\xCD"), tinystr);
48}