Debian Codegraph @

examples/base64.rs

raw symbols SCIP
1 //! ron initially encoded byte-slices and byte-bufs as base64-encoded strings.
2 //! However, since v0.9, ron now uses Rusty byte string literals instead.
3 //!
4 //! This example shows how the previous behaviour can be restored by serialising
5 //! bytes with strongly-typed base64-encoded strings, or accepting both Rusty
6 //! byte strings and the legacy base64-encoded string syntax.
7
8 use base64rust-analyzer cargo base64 0.22.1 crate/::enginerust-analyzer cargo base64 0.22.1 engine/::{general_purposerust-analyzer cargo base64 0.22.1 engine/general_purpose/::STANDARDrust-analyzer cargo base64 0.22.1 engine/general_purpose/STANDARD. as BASE64rust-analyzer cargo base64 0.22.1 engine/general_purpose/STANDARD., Enginerust-analyzer cargo base64 0.22.1 engine/Engine#};
9 use serderust-analyzer cargo serde 1.0.228 crate/::{derust-analyzer cargo serde_core 1.0.228 de/::Visitorrust-analyzer cargo serde_core 1.0.228 de/Visitor#, Deserializerust-analyzer cargo serde_core 1.0.228 de/Deserialize#, Deserializerrust-analyzer cargo serde_core 1.0.228 de/Deserializer#, Serializerust-analyzer cargo serde_core 1.0.228 ser/Serialize#, Serializerrust-analyzer cargo serde_core 1.0.228 ser/Serializer#};
10
11 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Serializerust-analyzer cargo serde_derive 1.0.228 Serialize:, Deserializerust-analyzer cargo serde_derive 1.0.228 Deserialize:)]
12 struct Configrust-analyzer cargo ron 0.12.0 Config# {
13 #[serderust-analyzer cargo serde_derive 1.0.228 serde:(with = "ByteStr")]
14 bytesrust-analyzer cargo ron 0.12.0 Config#bytes.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>,
15 #[serderust-analyzer cargo serde_derive 1.0.228 serde:(with = "Base64")]
16 base64rust-analyzer cargo ron 0.12.0 Config#base64.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>,
17 #[serderust-analyzer cargo serde_derive 1.0.228 serde:(with = "ByteStrOrBase64")]
18 bytes_or_base64rust-analyzer cargo ron 0.12.0 Config#bytes_or_base64.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>,
19 }
20
21 enum ByteStrrust-analyzer cargo ron 0.12.0 ByteStr# {}
22
23 impl ByteStrrust-analyzer cargo ron 0.12.0 ByteStr# {
24 fn serializerust-analyzer cargo ron 0.12.0 impl#[ByteStr]serialize().<S: Serializerrust-analyzer cargo serde_core 1.0.228 ser/Serializer#>(data: &[u8], serializer: S) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<S::Okrust-analyzer cargo serde_core 1.0.228 ser/Serializer#Ok#, S::Errorrust-analyzer cargo serde_core 1.0.228 ser/Serializer#Error#> {
25 serializer.serialize_bytesrust-analyzer cargo serde_core 1.0.228 ser/Serializer#serialize_bytes().(data)
26 }
27
28 fn deserializerust-analyzer cargo ron 0.12.0 impl#[ByteStr]deserialize().<'de, D: Deserializerrust-analyzer cargo serde_core 1.0.228 de/Deserializer#<'de>>(deserializer: D) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, D::Errorrust-analyzer cargo serde_core 1.0.228 de/Deserializer#Error#> {
29 struct ByteStrVisitorrust-analyzer cargo ron 0.12.0 ByteStrVisitor#;
30
31 impl<'de> Visitorrust-analyzer cargo serde_core 1.0.228 de/Visitor#<'de> for ByteStrVisitorrust-analyzer cargo ron 0.12.0 ByteStrVisitor# {
32 type Valuerust-analyzer cargo ron 0.12.0 impl#[ByteStrVisitor][`Visitor<'de>`]Value# = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>;
33
34 fn expectingrust-analyzer cargo ron 0.12.0 impl#[ByteStrVisitor][`Visitor<'de>`]expecting().(&self, fmt: &mut corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
35 fmt.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("a Rusty byte string")
36 }
37
38 fn visit_bytesrust-analyzer cargo ron 0.12.0 impl#[ByteStrVisitor][`Visitor<'de>`]visit_bytes().<E: serderust-analyzer cargo serde 1.0.228 crate/::derust-analyzer cargo serde_core 1.0.228 de/::Errorrust-analyzer cargo serde_core 1.0.228 de/Error#>(self, bytes: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo ron 0.12.0 impl#[ByteStrVisitor][`Visitor<'de>`]::Valuerust-analyzer cargo serde_core 1.0.228 de/Visitor#Value#, E> {
39 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(bytes.to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().())
40 }
41
42 fn visit_byte_bufrust-analyzer cargo ron 0.12.0 impl#[ByteStrVisitor][`Visitor<'de>`]visit_byte_buf().<E: serderust-analyzer cargo serde 1.0.228 crate/::derust-analyzer cargo serde_core 1.0.228 de/::Errorrust-analyzer cargo serde_core 1.0.228 de/Error#>(self, bytes: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo ron 0.12.0 impl#[ByteStrVisitor][`Visitor<'de>`]::Valuerust-analyzer cargo serde_core 1.0.228 de/Visitor#Value#, E> {
43 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(bytes)
44 }
45 }
46
47 deserializer.deserialize_byte_bufrust-analyzer cargo serde_core 1.0.228 de/Deserializer#deserialize_byte_buf().(ByteStrVisitorrust-analyzer cargo ron 0.12.0 ByteStrVisitor#)
48 }
49 }
50
51 enum Base64rust-analyzer cargo ron 0.12.0 Base64# {}
52
53 impl Base64rust-analyzer cargo ron 0.12.0 Base64# {
54 fn serializerust-analyzer cargo ron 0.12.0 impl#[Base64]serialize().<S: Serializerrust-analyzer cargo serde_core 1.0.228 ser/Serializer#>(data: &[u8], serializer: S) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<S::Okrust-analyzer cargo serde_core 1.0.228 ser/Serializer#Ok#, S::Errorrust-analyzer cargo serde_core 1.0.228 ser/Serializer#Error#> {
55 serializer.serialize_strrust-analyzer cargo serde_core 1.0.228 ser/Serializer#serialize_str().(&BASE64rust-analyzer cargo base64 0.22.1 engine/general_purpose/STANDARD..encoderust-analyzer cargo base64 0.22.1 engine/Engine#encode().(data))
56 }
57
58 fn deserializerust-analyzer cargo ron 0.12.0 impl#[Base64]deserialize().<'de, D: Deserializerrust-analyzer cargo serde_core 1.0.228 de/Deserializer#<'de>>(deserializer: D) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, D::Errorrust-analyzer cargo serde_core 1.0.228 de/Deserializer#Error#> {
59 let base64_str = <&str>::deserializerust-analyzer cargo serde_core 1.0.228 de/impls/impl#[`&'a str`][`Deserialize<'de>`]deserialize().(deserializer)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
60 BASE64rust-analyzer cargo base64 0.22.1 engine/general_purpose/STANDARD..decoderust-analyzer cargo base64 0.22.1 engine/Engine#decode().(base64_str).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(serderust-analyzer cargo serde 1.0.228 crate/::derust-analyzer cargo serde_core 1.0.228 de/::Errorrust-analyzer cargo serde_core 1.0.228 de/Error#::customrust-analyzer cargo serde_core 1.0.228 de/Error#custom().)
61 }
62 }
63
64 enum ByteStrOrBase64rust-analyzer cargo ron 0.12.0 ByteStrOrBase64# {}
65
66 impl ByteStrOrBase64rust-analyzer cargo ron 0.12.0 ByteStrOrBase64# {
67 fn serializerust-analyzer cargo ron 0.12.0 impl#[ByteStrOrBase64]serialize().<S: Serializerrust-analyzer cargo serde_core 1.0.228 ser/Serializer#>(data: &[u8], serializer: S) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<S::Okrust-analyzer cargo serde_core 1.0.228 ser/Serializer#Ok#, S::Errorrust-analyzer cargo serde_core 1.0.228 ser/Serializer#Error#> {
68 if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(all()) {
69 // either of these would work
70 serializer.serialize_strrust-analyzer cargo serde_core 1.0.228 ser/Serializer#serialize_str().(&BASE64rust-analyzer cargo base64 0.22.1 engine/general_purpose/STANDARD..encoderust-analyzer cargo base64 0.22.1 engine/Engine#encode().(data))
71 } else {
72 serializer.serialize_bytesrust-analyzer cargo serde_core 1.0.228 ser/Serializer#serialize_bytes().(data)
73 }
74 }
75
76 fn deserializerust-analyzer cargo ron 0.12.0 impl#[ByteStrOrBase64]deserialize().<'de, D: Deserializerrust-analyzer cargo serde_core 1.0.228 de/Deserializer#<'de>>(deserializer: D) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, D::Errorrust-analyzer cargo serde_core 1.0.228 de/Deserializer#Error#> {
77 struct ByteStrOrBase64Visitorrust-analyzer cargo ron 0.12.0 ByteStrOrBase64Visitor#;
78
79 impl<'de> Visitorrust-analyzer cargo serde_core 1.0.228 de/Visitor#<'de> for ByteStrOrBase64Visitorrust-analyzer cargo ron 0.12.0 ByteStrOrBase64Visitor# {
80 type Valuerust-analyzer cargo ron 0.12.0 impl#[ByteStrOrBase64Visitor][`Visitor<'de>`]Value# = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>;
81
82 fn expectingrust-analyzer cargo ron 0.12.0 impl#[ByteStrOrBase64Visitor][`Visitor<'de>`]expecting().(&self, fmt: &mut corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
83 fmt.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("a Rusty byte string or a base64-encoded string")
84 }
85
86 fn visit_strrust-analyzer cargo ron 0.12.0 impl#[ByteStrOrBase64Visitor][`Visitor<'de>`]visit_str().<E: serderust-analyzer cargo serde 1.0.228 crate/::derust-analyzer cargo serde_core 1.0.228 de/::Errorrust-analyzer cargo serde_core 1.0.228 de/Error#>(self, base64_str: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo ron 0.12.0 impl#[ByteStrOrBase64Visitor][`Visitor<'de>`]::Valuerust-analyzer cargo serde_core 1.0.228 de/Visitor#Value#, E> {
87 BASE64rust-analyzer cargo base64 0.22.1 engine/general_purpose/STANDARD..decoderust-analyzer cargo base64 0.22.1 engine/Engine#decode().(base64_str).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(serderust-analyzer cargo serde 1.0.228 crate/::derust-analyzer cargo serde_core 1.0.228 de/::Errorrust-analyzer cargo serde_core 1.0.228 de/Error#::customrust-analyzer cargo serde_core 1.0.228 de/Error#custom().)
88 }
89
90 fn visit_bytesrust-analyzer cargo ron 0.12.0 impl#[ByteStrOrBase64Visitor][`Visitor<'de>`]visit_bytes().<E: serderust-analyzer cargo serde 1.0.228 crate/::derust-analyzer cargo serde_core 1.0.228 de/::Errorrust-analyzer cargo serde_core 1.0.228 de/Error#>(self, bytes: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo ron 0.12.0 impl#[ByteStrOrBase64Visitor][`Visitor<'de>`]::Valuerust-analyzer cargo serde_core 1.0.228 de/Visitor#Value#, E> {
91 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(bytes.to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().())
92 }
93
94 fn visit_byte_bufrust-analyzer cargo ron 0.12.0 impl#[ByteStrOrBase64Visitor][`Visitor<'de>`]visit_byte_buf().<E: serderust-analyzer cargo serde 1.0.228 crate/::derust-analyzer cargo serde_core 1.0.228 de/::Errorrust-analyzer cargo serde_core 1.0.228 de/Error#>(self, bytes: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo ron 0.12.0 impl#[ByteStrOrBase64Visitor][`Visitor<'de>`]::Valuerust-analyzer cargo serde_core 1.0.228 de/Visitor#Value#, E> {
95 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(bytes)
96 }
97 }
98
99 deserializer.deserialize_anyrust-analyzer cargo serde_core 1.0.228 de/Deserializer#deserialize_any().(ByteStrOrBase64Visitorrust-analyzer cargo ron 0.12.0 ByteStrOrBase64Visitor#)
100 }
101 }
102
103 fn mainrust-analyzer cargo ron 0.12.0 main().() {
104 let ron = r#"Config(
105 bytes: b"only byte strings are allowed",
106 base64: "b25seSBiYXNlNjQtZW5jb2RlZCBzdHJpbmdzIGFyZSBhbGxvd2Vk",
107 bytes_or_base64: b"both byte strings and base64-encoded strings work",
108 )"#;
109
110 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
111 ronrust-analyzer cargo ron 0.12.0 crate/::from_strrust-analyzer cargo ron 0.12.0 de/from_str().::<Configrust-analyzer cargo ron 0.12.0 Config#>(ron).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
112 Configrust-analyzer cargo ron 0.12.0 Config# {
113 bytesrust-analyzer cargo ron 0.12.0 Config#bytes.: b"only byte strings are allowed".to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().(),
114 base64rust-analyzer cargo ron 0.12.0 Config#base64.: b"only base64-encoded strings are allowed".to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().(),
115 bytes_or_base64rust-analyzer cargo ron 0.12.0 Config#bytes_or_base64.: b"both byte strings and base64-encoded strings work".to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().()
116 }
117 );
118
119 let ron = r#"Config(
120 bytes: b"only byte strings are allowed",
121 base64: "b25seSBiYXNlNjQtZW5jb2RlZCBzdHJpbmdzIGFyZSBhbGxvd2Vk",
122 bytes_or_base64: "Ym90aCBieXRlIHN0cmluZ3MgYW5kIGJhc2U2NC1lbmNvZGVkIHN0cmluZ3Mgd29yaw==",
123 )"#;
124
125 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
126 ronrust-analyzer cargo ron 0.12.0 crate/::from_strrust-analyzer cargo ron 0.12.0 de/from_str().::<Configrust-analyzer cargo ron 0.12.0 Config#>(ron).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
127 Configrust-analyzer cargo ron 0.12.0 Config# {
128 bytesrust-analyzer cargo ron 0.12.0 Config#bytes.: b"only byte strings are allowed".to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().(),
129 base64rust-analyzer cargo ron 0.12.0 Config#base64.: b"only base64-encoded strings are allowed".to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().(),
130 bytes_or_base64rust-analyzer cargo ron 0.12.0 Config#bytes_or_base64.: b"both byte strings and base64-encoded strings work".to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().()
131 }
132 );
133
134 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(
135 "{}",
136 ronrust-analyzer cargo ron 0.12.0 crate/::serrust-analyzer cargo ron 0.12.0 ser/::to_string_prettyrust-analyzer cargo ron 0.12.0 ser/to_string_pretty().(
137 &Configrust-analyzer cargo ron 0.12.0 Config# {
138 bytesrust-analyzer cargo ron 0.12.0 Config#bytes.: b"only byte strings are allowed".to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().(),
139 base64rust-analyzer cargo ron 0.12.0 Config#base64.: b"only base64-encoded strings are allowed".to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().(),
140 bytes_or_base64rust-analyzer cargo ron 0.12.0 Config#bytes_or_base64.: b"both byte strings and base64-encoded strings work".to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().()
141 },
142 ronrust-analyzer cargo ron 0.12.0 crate/::serrust-analyzer cargo ron 0.12.0 ser/::PrettyConfigrust-analyzer cargo ron 0.12.0 ser/PrettyConfig#::defaultrust-analyzer cargo ron 0.12.0 ser/impl#[PrettyConfig][Default]default().().struct_namesrust-analyzer cargo ron 0.12.0 ser/impl#[PrettyConfig]struct_names().(true)
143 )
144 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
145 );
146 }