Debian Codegraph @

src/types/hstore.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{
2 collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::{btree_maprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree_map/, BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#},
3 memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/,
4 opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::{Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#, DerefMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/DerefMut#},
5 strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/,
6 };
7
8 use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::{
9 decoderust-analyzer cargo sqlx-core 0.8.3 decode/::Decoderust-analyzer cargo sqlx-core 0.8.3 decode/Decode#,
10 encoderust-analyzer cargo sqlx-core 0.8.3 encode/::{Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#, IsNullrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#},
11 errorrust-analyzer cargo sqlx-postgres 0.8.3 error/::BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#,
12 typesrust-analyzer cargo sqlx-postgres 0.8.3 types/::Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#,
13 PgArgumentBufferrust-analyzer cargo sqlx-postgres 0.8.3 arguments/PgArgumentBuffer#, PgHasArrayTyperust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#, PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#, PgValueRefrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#,
14 };
15 use serderust-analyzer cargo serde 1.0.229 crate/::{Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#, Serializerust-analyzer cargo serde_core 1.0.229 ser/Serialize#};
16 use sqlx_corerust-analyzer cargo sqlx-core 0.8.3 crate/::bytesrust-analyzer cargo bytes 1.12.1 crate/::Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#;
17
18 /// Key-value support (`hstore`) for Postgres.
19 ///
20 /// SQLx currently maps `hstore` to a `BTreeMap<String, Option<String>>` but this may be expanded in
21 /// future to allow for user defined types.
22 ///
23 /// See [the Postgres manual, Appendix F, Section 18][PG.F.18]
24 ///
25 /// [PG.F.18]: https://www.postgresql.org/docs/current/hstore.html
26 ///
27 /// ### Note: Requires Postgres 8.3+
28 /// Introduced as a method for storing unstructured data, the `hstore` extension was first added in
29 /// Postgres 8.3.
30 ///
31 ///
32 /// ### Note: Extension Required
33 /// The `hstore` extension is not enabled by default in Postgres. You will need to do so explicitly:
34 ///
35 /// ```ignore
36 /// CREATE EXTENSION IF NOT EXISTS hstore;
37 /// ```
38 ///
39 /// # Examples
40 ///
41 /// ```
42 /// # use sqlx_postgres::types::PgHstore;
43 /// // Shows basic usage of the PgHstore type.
44 /// //
45 /// #[derive(Clone, Debug, Default, Eq, PartialEq)]
46 /// struct UserCreate<'a> {
47 /// username: &'a str,
48 /// password: &'a str,
49 /// additional_data: PgHstore
50 /// }
51 ///
52 /// let mut new_user = UserCreate {
53 /// username: "[email protected]",
54 /// password: "@super_secret_1",
55 /// ..Default::default()
56 /// };
57 ///
58 /// new_user.additional_data.insert("department".to_string(), Some("IT".to_string()));
59 /// new_user.additional_data.insert("equipment_issued".to_string(), None);
60 /// ```
61 /// ```ignore
62 /// query_scalar::<_, i64>(
63 /// "insert into user(username, password, additional_data) values($1, $2, $3) returning id"
64 /// )
65 /// .bind(new_user.username)
66 /// .bind(new_user.password)
67 /// .bind(new_user.additional_data)
68 /// .fetch_one(pg_conn)
69 /// .await?;
70 /// ```
71 ///
72 /// ```
73 /// # use sqlx_postgres::types::PgHstore;
74 /// // PgHstore implements FromIterator to simplify construction.
75 /// //
76 /// let additional_data = PgHstore::from_iter([
77 /// ("department".to_string(), Some("IT".to_string())),
78 /// ("equipment_issued".to_string(), None),
79 /// ]);
80 ///
81 /// assert_eq!(additional_data["department"], Some("IT".to_string()));
82 /// assert_eq!(additional_data["equipment_issued"], None);
83 ///
84 /// // Also IntoIterator for ease of iteration.
85 /// //
86 /// for (key, value) in additional_data {
87 /// println!("{key}: {value:?}");
88 /// }
89 /// ```
90 ///
91 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Deserializerust-analyzer cargo serde_derive 1.0.229 Deserialize:, Serializerust-analyzer cargo serde_derive 1.0.229 Serialize:)]
92 pub struct PgHstorerust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore#(pub BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>>);
93
94 impl Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref# for PgHstorerust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore# {
95 type Targetrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][Deref]Target# = BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>>;
96
97 fn derefrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][Deref]deref().(&self) -> &Selfrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][Deref]::Targetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#Target# {
98 &self.0rust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore#0.
99 }
100 }
101
102 impl DerefMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/DerefMut# for PgHstorerust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore# {
103 fn deref_mutrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][DerefMut]deref_mut().(&mut self) -> &mut Selfrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][DerefMut]::Targetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#Target# {
104 &mut self.0rust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore#0.
105 }
106 }
107
108 impl FromIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/FromIterator#<(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#)> for PgHstorerust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore# {
109 fn from_iterrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][`FromIterator<(String, String)>`]from_iter().<T: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = (Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#)>>(iter: T) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][`FromIterator<(String, String)>`] {
110 iter.into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#into_iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(k, v)| (k, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(v))).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()
111 }
112 }
113
114 impl FromIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/FromIterator#<(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>)> for PgHstorerust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore# {
115 fn from_iterrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][`FromIterator<(String, Option<String>)>`]from_iter().<T: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = (Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>)>>(iter: T) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][`FromIterator<(String, Option<String>)>`] {
116 let mut result = Selfrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][`FromIterator<(String, Option<String>)>`]::defaultrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][Default]default().();
117
118 for (key, value) in iter {
119 result.0rust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore#0..insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]insert().(key, value);
120 }
121
122 result
123 }
124 }
125
126 impl IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator# for PgHstorerust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore# {
127 type Itemrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][IntoIterator]Item# = (Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>);
128 type IntoIterrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][IntoIterator]IntoIter# = btree_maprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree_map/::IntoIterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/IntoIter#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>>;
129
130 fn into_iterrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][IntoIterator]into_iter().(self) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][IntoIterator]::IntoIterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#IntoIter# {
131 self.0rust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore#0..into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`][IntoIterator]into_iter().()
132 }
133 }
134
135 impl Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#<Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#> for PgHstorerust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore# {
136 fn type_inforust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][`Type<Postgres>`]type_info().() -> PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo# {
137 PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#::with_namerust-analyzer cargo sqlx-postgres 0.8.3 type_info/impl#[PgTypeInfo]with_name().("hstore")
138 }
139 }
140
141 impl PgHasArrayTyperust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType# for PgHstorerust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore# {
142 fn array_type_inforust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][PgHasArrayType]array_type_info().() -> PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo# {
143 PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#::array_ofrust-analyzer cargo sqlx-postgres 0.8.3 type_info/impl#[PgTypeInfo]array_of().("hstore")
144 }
145 }
146
147 impl<'r> Decoderust-analyzer cargo sqlx-core 0.8.3 decode/Decode#<'r, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#> for PgHstorerust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore# {
148 fn decoderust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][`Decode<'r, Postgres>`]decode().(value: PgValueRefrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#<'r>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][`Decode<'r, Postgres>`], BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#> {
149 let mut buf = <&[u8] as Decoderust-analyzer cargo sqlx-core 0.8.3 decode/Decode#<Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#>>::decoderust-analyzer cargo sqlx-postgres 0.8.3 types/bytes/impl#[`&'r [u8]`][`Decode<'r, Postgres>`]decode().(value)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
150 let len = read_lengthrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/read_length().(&mut buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
151
152 let len =
153 usize::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/ptr_try_from_impls/impl#[usize][`TryFrom<i32>`]try_from().(len).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("PgHstore: length out of range: {len}"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
154
155 let mut result = Selfrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][`Decode<'r, Postgres>`]::defaultrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][Default]default().();
156
157 for i in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#len {
158 let key = read_stringrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/read_string().(&mut buf)
159 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("PgHstore: error reading {i}th key: {e}"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
160 .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("PgHstore: expected {i}th key, got nothing"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
161
162 let value = read_stringrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/read_string().(&mut buf)
163 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("PgHstore: error reading value for key {key:?}: {e}"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
164
165 result.insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]insert().(key, value);
166 }
167
168 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().buf.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() {
169 tracingrust-analyzer cargo tracing 0.1.44 crate/::warnrust-analyzer cargo tracing 0.1.44 macros/warn!!("{} unread bytes at the end of HSTORE value", buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().());
170 }
171
172 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(result)
173 }
174 }
175
176 impl Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#<'_, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#> for PgHstorerust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore# {
177 fn encode_by_refrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][`Encode<'_, Postgres>`]encode_by_ref().(&self, buf: &mut PgArgumentBufferrust-analyzer cargo sqlx-postgres 0.8.3 arguments/PgArgumentBuffer#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<IsNullrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#, BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#> {
178 buf.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(&i32::to_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]to_be_bytes().(
179 self.0rust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore#0.
180 .lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]len().()
181 .try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().()
182 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("PgHstore length out of range: {}", self.0rust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore#0..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]len().()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
183 ));
184
185 for (i, (key, val)) in self.0rust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore#0..iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() {
186 let key_bytes = key.as_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_bytes().();
187
188 let key_len = i32::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/ptr_try_from_impls/impl#[i32][`TryFrom<usize>`]try_from().(key_bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| {
189 // Doesn't make sense to print the key itself: it's more than 2 GiB long!
190 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
191 "PgHstore: length of {i}th key out of range: {} bytes",
192 key_bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()
193 )
194 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
195
196 buf.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(&i32::to_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]to_be_bytes().(key_len));
197 buf.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(key_bytes);
198
199 match val {
200 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(val) => {
201 let val_bytes = val.as_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_bytes().();
202
203 let val_len = i32::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/ptr_try_from_impls/impl#[i32][`TryFrom<usize>`]try_from().(val_bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| {
204 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
205 "PgHstore: value length for key {key:?} out of range: {} bytes",
206 val_bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()
207 )
208 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
209 buf.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(&i32::to_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]to_be_bytes().(val_len));
210 buf.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(val_bytes);
211 }
212 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => {
213 buf.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(&i32::to_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]to_be_bytes().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1));
214 }
215 }
216 }
217
218 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(IsNullrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#::Norust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#No#)
219 }
220 }
221
222 fn read_lengthrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/read_length().(buf: &mut &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<i32, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
223 if buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<i32>() {
224 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
225 "expected {} bytes, got {}",
226 memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<i32>(),
227 buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()
228 ));
229 }
230
231 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(buf.get_i32rust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#get_i32().())
232 }
233
234 fn read_stringrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/read_string().(buf: &mut &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
235 let len = read_lengthrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/read_length().(buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
236
237 match len {
238 -1 => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#),
239 len => {
240 let len =
241 usize::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/ptr_try_from_impls/impl#[usize][`TryFrom<i32>`]try_from().(len).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("string length out of range: {len}"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
242
243 if buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().len {
244 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("expected {len} bytes, got {}", buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()));
245 }
246
247 let (val, rest) = buf.split_atrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]split_at().(len);
248 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().buf = rest;
249
250 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(
251 strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(val).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| e.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
252 ))
253 }
254 }
255 }
256
257 #[cfg(test)]
258 mod testrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/test/ {
259 use superrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/::*;
260 use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::PgValueFormatrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueFormat#;
261
262 const EMPTYrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/test/EMPTY.: &str = "00000000";
263
264 const NAME_SURNAME_AGErust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/test/NAME_SURNAME_AGE.: &str =
265 "0000000300000003616765ffffffff000000046e616d65000000044a6f686e000000077375726e616d6500000003446f65";
266
267 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
268 fn hstore_deserialize_okrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/test/hstore_deserialize_ok().() {
269 let empty = hexrust-analyzer cargo hex 0.4.3 crate/::decoderust-analyzer cargo hex 0.4.3 decode().(EMPTYrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/test/EMPTY.).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
270 let name_surname_age = hexrust-analyzer cargo hex 0.4.3 crate/::decoderust-analyzer cargo hex 0.4.3 decode().(NAME_SURNAME_AGErust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/test/NAME_SURNAME_AGE.).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
271
272 let empty = PgValueRefrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef# {
273 valuerust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#value.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(empty.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().()),
274 rowrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#row.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
275 type_inforust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#type_info.: PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#::with_namerust-analyzer cargo sqlx-postgres 0.8.3 type_info/impl#[PgTypeInfo]with_name().("hstore"),
276 formatrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#format.: PgValueFormatrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueFormat#::Binaryrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueFormat#Binary#,
277 };
278
279 let name_surname = PgValueRefrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef# {
280 valuerust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#value.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(name_surname_age.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().()),
281 rowrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#row.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
282 type_inforust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#type_info.: PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#::with_namerust-analyzer cargo sqlx-postgres 0.8.3 type_info/impl#[PgTypeInfo]with_name().("hstore"),
283 formatrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#format.: PgValueFormatrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueFormat#::Binaryrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueFormat#Binary#,
284 };
285
286 let res_empty = PgHstorerust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore#::decoderust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][`Decode<'r, Postgres>`]decode().(empty).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
287 let res_name_surname = PgHstorerust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore#::decoderust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][`Decode<'r, Postgres>`]decode().(name_surname).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
288
289 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(res_empty.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]is_empty().());
290 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(res_name_surname[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()."name"]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("John".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()));
291 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(res_name_surname[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()."surname"]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("Doe".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()));
292 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(res_name_surname[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()."age"]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
293 }
294
295 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
296 #[should_panic(expected = "PgHstore: length out of range: -5")]
297 fn hstore_deserialize_buffer_length_errorrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/test/hstore_deserialize_buffer_length_error().() {
298 let buf = PgValueRefrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef# {
299 valuerust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#value.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&[255, 255, 255, 251]),
300 rowrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#row.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
301 type_inforust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#type_info.: PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#::with_namerust-analyzer cargo sqlx-postgres 0.8.3 type_info/impl#[PgTypeInfo]with_name().("hstore"),
302 formatrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#format.: PgValueFormatrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueFormat#::Binaryrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueFormat#Binary#,
303 };
304
305 PgHstorerust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore#::decoderust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][`Decode<'r, Postgres>`]decode().(buf).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
306 }
307
308 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
309 fn hstore_serialize_okrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/test/hstore_serialize_ok().() {
310 let mut buff = PgArgumentBufferrust-analyzer cargo sqlx-postgres 0.8.3 arguments/PgArgumentBuffer#::defaultrust-analyzer cargo sqlx-postgres 0.8.3 arguments/impl#[PgArgumentBuffer][Default]default().();
311 let _ = PgHstorerust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore#::from_iterrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][`FromIterator<(String, String)>`]from_iter().::<[(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#); 0]>([])
312 .encode_by_refrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][`Encode<'_, Postgres>`]encode_by_ref().(&mut buff)
313 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
314
315 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(hexrust-analyzer cargo hex 0.4.3 crate/::encoderust-analyzer cargo hex 0.4.3 encode().(buff.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().()), EMPTYrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/test/EMPTY.);
316
317 buff.clearrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]clear().();
318
319 let _ = PgHstorerust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/PgHstore#::from_iterrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][`FromIterator<(String, Option<String>)>`]from_iter().([
320 ("name".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("John".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())),
321 ("surname".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("Doe".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())),
322 ("age".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#),
323 ])
324 .encode_by_refrust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/impl#[PgHstore][`Encode<'_, Postgres>`]encode_by_ref().(&mut buff)
325 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
326
327 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(hexrust-analyzer cargo hex 0.4.3 crate/::encoderust-analyzer cargo hex 0.4.3 encode().(buff.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().()), NAME_SURNAME_AGErust-analyzer cargo sqlx-postgres 0.8.3 types/hstore/test/NAME_SURNAME_AGE.);
328 }
329 }