Debian Codegraph @

third_party/rust/tinystr/src/databake.rs

raw
1 // 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
5 use crate::TinyAsciiStr;
6 use crate::UnvalidatedTinyAsciiStr;
7 use databake::*;
8
9 impl<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! {
14 tinystr::tinystr!(#N, #string)
15 }
16 }
17 }
18
19 impl<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 }
35 }
36 }
37 }
38
39 #[test]
40 fn test() {
41 test_bake!(TinyAsciiStr<10>, const: crate::tinystr!(10usize, "foo"), tinystr);
42 }
43
44 #[test]
45 fn 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 }