1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
//! This module provides extensions to the [HTTP](https://crates.io/crates/http),
//! [Hyper](https://crates.io/crates/hyper), and [Axum](https://crates.io/crates/axum)
//! crates.
//!
//! Hyper and Axum are built on top of the HTTP crate, and Axum uses parts of
//! Hyper, so it makes sense to combine all of these in one module.
// Modules
#[cfg(test)]
#[path = "tests/http.rs"]
mod tests;
// Packages
use base64::{DecodeError, engine::{Engine as _, general_purpose::STANDARD as BASE64}};
use core::{
cmp::Ordering,
convert::Infallible,
error::Error,
fmt::{Debug, Display, Write, self},
ops::{Add, AddAssign},
str::FromStr,
};
use futures::executor;
use http::{Response, StatusCode};
use http_body::combinators::UnsyncBoxBody;
use hyper::{
body::{Body as HyperBody, Bytes, to_bytes},
HeaderMap,
header::HeaderValue,
};
use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Error as DeError};
use serde_json::Value as Json;
use std::borrow::Cow;
// Enums
// ContentType
/// The content type of an HTTP response, for use by [`UnpackedResponseBody`].
///
/// The content type is used to determine how to represent and interpret the
/// response body when performing serialisation and deserialisation, including
/// for display.
///
/// The default content type is [`Text`](ContentType::Text).
///
/// This enum is exhaustive and will never have any additional variants added
/// to it, as all possibilities are already covered.
///
#[expect(clippy::exhaustive_enums, reason = "Exhaustive")]
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub enum ContentType {
/// The response body is text. It will be represented as an ordinary
/// [`String`] when serialised.
#[default]
Text,
/// The response body is binary. It will be represented as a [`String`]
/// in base64 format when serialised.
Binary,
}
// ResponseError
/// The possible errors that can occur when working with an HTTP response.
#[derive(Debug)]
#[non_exhaustive]
pub enum ResponseError {
/// An error encountered while converting the response body to bytes.
ConversionError(Box<dyn Error>),
}
// Display
impl Display for ResponseError {
// fmt
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[expect(clippy::pattern_type_mismatch, reason = "Cannot dereference a Box")]
let description = match self {
Self::ConversionError(err) => format!("Error encountered while converting response body to bytes: {err}"),
};
write!(f, "{description}")
}
}
// Error
impl Error for ResponseError {}
// Structs
// UnpackedResponse
/// An HTTP response in comparison-friendly form for interrogation.
///
/// Data in [`hyper::Response`] (and indeed [`http::Response`] as well) is
/// stored in a specific form, made up of a header map object and a generic body
/// type, which can be empty, a [`String`], or a streaming body future. This
/// struct provides a way to use the data in a more accessible form, to allow it
/// to be checked and compared. This is useful for testing, as the entire set of
/// headers plus body can be checked all at once, and also for printing/logging.
///
/// If specific headers or body content needs to be checked, it is recommended
/// to use the standard functions as they will be more efficient and performant.
///
/// Note that the [`body`](UnpackedResponse::body) property, which is stored as
/// a vector of bytes, will get converted to a [`String`] if it is run through
/// the standard [`Debug`] or [`Display`] formatters. This is because
/// human-readable output is the intuitively-expected outcome in this situation.
/// The behaviour can be controlled with the [`ContentType`] enum, which is used
/// to determine whether the data is binary or text. If [`Text`](ContentType::Text),
/// then the conversion uses [`from_utf8_lossy()`](String::from_utf8_lossy()),
/// so no errors will occur, but if the body is not valid UTF8 then the
/// resulting [`String`] will not be exactly the same. If an accurate
/// representation of the body is required then it should be set to [`Binary`](ContentType::Binary),
/// or else it should be extracted and converted to a `Vec<u8>` and then run
/// through the [`Debug`] or [`Display`] formatters directly.
///
/// # See also
///
/// * [`axum::response`](https://docs.rs/axum/latest/axum/response/index.html)
/// * [`axum::response::Response`](https://docs.rs/axum/latest/axum/response/type.Response.html)
/// * [`http::Response`]
/// * [`hyper::Response`]
/// * [`ResponseExt`]
/// * [`ResponseExt::unpack()`]
/// * [`UnpackedResponseHeader`]
///
#[derive(Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct UnpackedResponse {
// Public properties
/// The response status code. This is an enum, so is not directly comparable
/// to a number. The standard [`Display`] formatter will convert it to a
/// string in the format `"200 OK"`, but the standard [`FromStr`]
/// implementation will error if this is given back to it, as it expects
/// only `"200"`. Because this round-trip is basically broken, this struct
/// provides custom serialisation and deserialisation functions to convert
/// the status code to and from an actual number (a [`u16`]). This allows
/// the struct to be serialised and deserialised in a round-trip without
/// error, and is also the more intuitive representation of the status code
/// in serialised form such as JSON.
#[serde(serialize_with = "serialize_status_code", deserialize_with = "deserialize_status_code")]
pub status: StatusCode,
/// The response headers. These are in a vector rather than a hashmap
/// because there may be multiple headers with the same name. They are
/// sorted by name, and then by value, allowing for reliable comparison.
/// Sorting does break the original order of the headers, but this should
/// only very rarely matter, even when logging, and sorting allows
/// duplicates to be spotted by eye more easily in logs.
pub headers: Vec<UnpackedResponseHeader>,
/// The response body. This originates from the response body as a [`Bytes`]
/// container, but gets stored here as a vector of bytes for convenience.
/// This may not be valid UTF8, so is not converted to a [`String`]. That
/// step is left as optional for the caller, if required (and happens when
/// running the [`UnpackedResponse`] struct through the [`Debug`] or
/// [`Display`] formatters).
pub body: UnpackedResponseBody,
}
// UnpackedResponse
impl UnpackedResponse {
// new
/// Creates a new unpacked response instance.
///
/// This constructor builds a new [`UnpackedResponse`] instance from the
/// response status code, header data, and body data. This is useful when
/// the parts
///
/// # Parameters
///
/// * `status` - The response status code. See [`status`](UnpackedResponse::status).
/// * `headers` - The response headers. See [`headers`](UnpackedResponse::headers).
/// * `body` - The response body. See [`body`](UnpackedResponse::body).
///
#[must_use]
pub fn new<T: Into<UnpackedResponseBody>>(
status: StatusCode,
headers: Vec<(String, String)>,
body: T
) -> Self {
Self::new_from_parts(
status,
headers.into_iter().map(|(name, value)| UnpackedResponseHeader::new(name, value)).collect(),
body.into(),
)
}
// new_from_parts
/// Creates a new unpacked response instance from existing parts.
///
/// This constructor builds a new [`UnpackedResponse`] instance from
/// constituent part instances that are already in the correct form. This is
/// useful when the parts are already available.
///
/// # Parameters
///
/// * `status` - The response status code. See [`status`](UnpackedResponse::status).
/// * `headers` - The response headers. See [`headers`](UnpackedResponse::headers).
/// * `body` - The response body. See [`body`](UnpackedResponse::body).
///
#[must_use]
pub const fn new_from_parts(
status: StatusCode,
headers: Vec<UnpackedResponseHeader>,
body: UnpackedResponseBody
) -> Self {
Self {
status,
headers,
body,
}
}
}
// PartialEq
impl PartialEq for UnpackedResponse {
// eq
fn eq(&self, other: &Self) -> bool {
self.status == other.status && self.headers == other.headers && self.body == other.body
}
}
// UnpackedResponseHeader
/// An HTTP response header.
///
/// A simple representation of an HTTP response header as a key-value pair. The
/// purpose of this struct is to formalise the data structure used by
/// [`UnpackedResponse`] for storing headers.
///
/// No other properties are planned or logically considerable at present, and so
/// this struct is seen as being exhaustive.
///
/// # See also
///
/// * [`UnpackedResponse`]
///
#[expect(clippy::exhaustive_structs, reason = "Exhaustive")]
#[derive(Debug, Deserialize, Serialize)]
pub struct UnpackedResponseHeader {
// Public properties
/// The response header name.
pub name: String,
/// The response header value.
pub value: String,
}
// UnpackedResponseHeader
impl UnpackedResponseHeader {
// new
/// Creates a new response header instance.
///
/// # Parameters
///
/// * `name` - The response header name.
/// * `value` - The response header value.
///
#[must_use]
pub const fn new(name: String, value: String) -> Self {
Self {
name,
value,
}
}
}
// PartialEq
impl PartialEq for UnpackedResponseHeader {
// eq
fn eq(&self, other: &Self) -> bool {
self.name == other.name && self.value == other.value
}
}
// UnpackedResponseBody
/// An HTTP response body.
///
/// A simple representation of an HTTP response body as a vector of bytes. The
/// purpose of this struct is to formalise the data structure used by
/// [`UnpackedResponse`] for storing the body.
///
/// The data originates from the response body as a [`Bytes`] container, but
/// gets stored here as a vector of bytes for convenience. This may not be valid
/// UTF8, so is not converted to a [`String`]. That step is left as optional for
/// the caller, if required (and happens when running through the [`Debug`] or
/// [`Display`] formatters).
///
/// The conversion to a [`String`] when run through the [`Debug`] and
/// [`Display`] formatters is because human-readable output is the
/// intuitively-expected outcome in this situation. The behaviour can be
/// controlled with the [`ContentType`] enum, which is used to determine whether
/// the data is binary or text. If [`Text`](ContentType::Text), then the
/// conversion uses [`from_utf8_lossy()`](String::from_utf8_lossy()), so no
/// errors will occur, but if the body is not valid UTF8 then the resulting
/// [`String`] will not be exactly the same. If an accurate representation of
/// the body is required then it should be set to [`Binary`](ContentType::Binary),
/// or else it should be extracted and converted to a `Vec<u8>` and then run
/// through the [`Debug`] or [`Display`] formatters directly.
///
/// This struct is very similar in nature to the standard Rust [`String`]
/// struct, in that it is a wrapper around a vector of bytes, and so its design
/// and function names are modelled after it. The main difference is that it
/// does not require its contents to be valid UTF8, and also that it is a tuple
/// struct rather than a regular struct.
///
/// Note that serialisation/deserialisation of this struct directly will produce
/// and expect a [`String`], not a vector of bytes. This is because this is the
/// most useful and fitting behaviour for the intended purpose, as with the
/// implementations of [`Display`] and [`FromStr`]. As noted above, this is
/// lossy if the [`ContentType`] is [`Text`](ContentType::Text) and the data is
/// not valid UTF8, but not if set to [`Binary`](ContentType::Binary).
///
/// # See also
///
/// * [`UnpackedResponse`]
///
#[derive(Default)]
#[non_exhaustive]
pub struct UnpackedResponseBody {
// Private properties
/// The response body as a vector of bytes. The data originates from the
/// response body as a [`Bytes`] container, but gets stored here as a vector
/// of bytes for convenience. This may not be valid UTF8, so is not
/// converted to a [`String`]. That step is left as optional for the caller,
/// if required (and happens when running through the [`Debug`] or
/// [`Display`] formatters).
body: Vec<u8>,
/// The content type of the response body. This is used to determine how to
/// represent and interpret the response body when performing serialisation
/// and deserialisation, including for display. The default content type is
/// [`Text`](ContentType::Text).
content_type: ContentType,
}
// UnpackedResponseBody
impl UnpackedResponseBody {
// new
/// Creates a new response body instance.
///
/// # Parameters
///
/// * `data` - The response body as any type for which there is a [`From`]
/// implementation.
///
pub fn new<T: Into<Self>>(data: T) -> Self {
data.into()
}
// content_type
/// Returns the content type of the response body.
///
/// # See also
///
/// * [`ContentType`]
/// * [`UnpackedResponseBody::is_binary()`]
/// * [`UnpackedResponseBody::is_text()`]
/// * [`UnpackedResponseBody::set_content_type()`]
///
#[must_use]
pub const fn content_type(&self) -> ContentType {
self.content_type
}
// set_content_type
/// Sets the content type of the response body.
///
/// This method is chainable, as it returns a mutable reference to the
/// response body instance.
///
/// # See also
///
/// * [`ContentType`]
/// * [`UnpackedResponseBody::content_type()`]
///
pub fn set_content_type(&mut self, content_type: ContentType) -> &mut Self {
self.content_type = content_type;
self
}
// is_binary
/// Returns whether the response body is binary.
///
/// # See also
///
/// * [`ContentType`]
/// * [`UnpackedResponseBody::content_type()`]
/// * [`UnpackedResponseBody::is_text()`]
///
#[must_use]
pub fn is_binary(&self) -> bool {
self.content_type == ContentType::Binary
}
// is_text
/// Returns whether the response body is text.
///
/// # See also
///
/// * [`ContentType`]
/// * [`UnpackedResponseBody::content_type()`]
/// * [`UnpackedResponseBody::is_binary()`]
///
#[must_use]
pub fn is_text(&self) -> bool {
self.content_type == ContentType::Text
}
// as_bytes
/// Returns a byte slice of the response body's contents.
///
/// Provides a read-only view of the byte data within the response body,
/// without consuming the data. The returned slice is a reference to the
/// actual data stored in the response body, not a copy. Because of this, it
/// is not possible to mutate the contents of the response body through the
/// returned slice. It does not allocate new memory or change the ownership
/// of the byte data. This method is useful when you need to work with the
/// bytes of the response body in a read-only fashion, or when you want to
/// avoid copying the data.
///
/// - This method returns a slice (`&[u8]`) referencing the bytes of the
/// response body contents.
/// - The original response body value remains intact, and can still be
/// used afterward.
/// - No reallocation or copying of data occurs since it's just providing
/// a view into the original memory.
///
/// Use this method when you need to work with the byte data in a
/// non-destructive, read-only manner while keeping the original response
/// body intact.
///
/// # See also
///
/// * [`UnpackedResponseBody::as_mut_bytes()`]
/// * [`UnpackedResponseBody::into_bytes()`]
/// * [`UnpackedResponseBody::to_bytes()`]
///
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
&self.body
}
// as_mut_bytes
/// Returns a mutable referenced to the response body's contents.
///
/// Provides a mutable view of the byte data within the response body,
/// without consuming the data. The returned vector is a reference to the
/// actual data stored in the response body, not a copy. This method is
/// useful when you need to work with, and modify, the bytes of the response
/// body directly, without copying the data.
///
/// - This method returns a mutable vector (`&mut Vec<u8>`) referencing
/// the bytes of the response body contents.
/// - The original response body value remains intact, and can still be
/// used afterward.
/// - No reallocation or copying of data occurs since it's just providing
/// a reference to the original memory.
///
/// Use this method when you need to work directly with the byte data in a
/// mutable manner.
///
/// Note that unlike the function's [`String::as_mut_vec()`] counterpart,
/// this method is not unsafe. This is because the response body is not
/// required to be valid UTF8, so there is no risk of invalid UTF8 being
/// created.
///
/// Note also that a better name for this method could be `as_mut_vec()`,
/// which would be consistent with the standard library's
/// [`String::as_mut_vec()`] method, which this method is modelled after,
/// but that would break consistency with the other methods on this struct.
/// In addition, there is another method called [`str::as_bytes_mut()`],
/// which appears to be named quite inconsistently with other comparable
/// methods, and so calling this method `as_mut_bytes()` might cause
/// confusion, but is at least self-consistent.
///
/// # See also
///
/// * [`UnpackedResponseBody::as_bytes()`]
/// * [`UnpackedResponseBody::into_bytes()`]
/// * [`UnpackedResponseBody::to_bytes()`]
///
pub fn as_mut_bytes(&mut self) -> &mut Vec<u8> {
&mut self.body
}
// into_bytes
/// Returns the response body as a vector of bytes.
///
/// This consumes the response body, without cloning or copying, and returns
/// a new vector containing the bytes of the response body. It transfers
/// ownership of the byte data from the response body to the new vector.
/// This method is useful when you need to move the byte data out of the
/// response body, for example to pass it to a function that expects a
/// `Vec<u8>`, or when you want to modify the byte data in-place without
/// affecting the original response body.
///
/// - This method consumes the response body contents and returns a
/// `Vec<u8>` containing its bytes.
/// - After calling this method, the original response body value is no
/// longer available for use, because it has been moved.
/// - Transforms the response body into a vector of bytes without any
/// copying.
///
/// Use this method when you want to consume the response body and obtain
/// ownership of its byte data in the form of a `Vec<u8>`. This is useful
/// when you need to modify or move the byte data, or when you want to pass
/// it to functions that expect a `Vec<u8>`.
///
/// Note that a better name for this method might be `into_vec()`, but that
/// would be inconsistent with the standard library's
/// [`String::into_bytes()`] method, which this method is modelled after.
///
/// # See also
///
/// * [`UnpackedResponseBody::as_bytes()`]
/// * [`UnpackedResponseBody::as_mut_bytes()`]
/// * [`UnpackedResponseBody::to_bytes()`]
///
#[must_use]
pub fn into_bytes(self) -> Vec<u8> {
self.body
}
// to_bytes
/// Returns a copy of the response body data converted to a vector of bytes.
///
/// This does not consume the response body, but clones it. Following Rust's
/// naming conventions and idioms, this method "converts" the data content
/// of the response body into a byte representation, in a `Vec<u8>`. (No
/// actual conversion takes place because the data is already stored
/// internally as a vector of bytes, but this is academic and could change
/// in future, so "conversion" is implied and expected as a theoretical
/// behaviour.) Ownership of the cloned and converted byte data is
/// transferred to the caller, and there are no side effects on the internal
/// state of the [`UnpackedResponseBody`] instance.
///
/// - This method returns a `Vec<u8>` vector of bytes without consuming
/// the response body contents.
/// - The original response body value remains intact, and can still be
/// used afterward.
/// - The response body data is copied, and converted/transformed into
/// the output value returned.
///
/// Use this method when you need to obtain a copy of the response body's
/// byte data in the form of a `Vec<u8>`, without consuming the response
/// body itself. This is useful when you need to pass the byte data to a
/// function that expects a `Vec<u8>`, or when you want to modify the byte
/// data without affecting the original response body.
///
/// Note that a better name for this method might be `to_vec()`, but that
/// would be inconsistent with the standard library's
/// [`String::into_bytes()`] method.
///
/// # See also
///
/// * [`UnpackedResponseBody::as_bytes()`]
/// * [`UnpackedResponseBody::as_mut_bytes()`]
/// * [`UnpackedResponseBody::into_bytes()`]
///
#[must_use]
pub fn to_bytes(&self) -> Vec<u8> {
self.body.clone()
}
// to_base64
/// Returns the response body data converted to a base64-encoded [`String`].
///
/// This does not consume the response body, but clones it, as is necessary
/// to perform the conversion to base64. It converts straight from bytes to
/// base64, without converting to a [`String`] first, because the response
/// body is binary data.
///
/// # See also
///
/// * [`UnpackedResponseBody::from_base64()`]
///
#[must_use]
pub fn to_base64(&self) -> String {
BASE64.encode(&self.body)
}
// from_base64
/// Converts a base64-encoded [`String`] to an [`UnpackedResponseBody`].
///
/// This method does not consume the input string, but clones it, as is
/// necessary to perform the conversion from [`base64`]. It converts
/// straight from base64 to bytes, without converting to a [`String`] first,
/// because the response body is binary data. This means that no UTF8
/// validation is performed.
///
/// Note that unlike the [`From`] type conversion implementations, this
/// returns a [`Result`].
///
/// # Errors
///
/// This method will return an error if the input string is not valid
/// base64. Such an error will be returned as a [`DecodeError`], which is
/// passed through from the [`base64`] crate.
///
/// # See also
///
/// * [`UnpackedResponseBody::to_base64()`]
///
pub fn from_base64(encoded: &str) -> Result<Self, DecodeError> {
let decoded = BASE64.decode(encoded)?;
Ok(Self { body: decoded, content_type: ContentType::Binary })
}
// clear
/// Removes all contents from the response body.
///
/// This method removes all data from the response body, resetting it to an
/// empty state. This method has no effect on the capacity of the response
/// body, and so does not affect any allocation.
///
pub fn clear(&mut self) {
self.body.clear();
}
// empty
/// Returns an empty response body.
///
/// This method returns an empty response body. This is equivalent to
/// creating a new response body with [`UnpackedResponseBody::new()`], but
/// without having to supply any parameters.
///
#[must_use]
pub fn empty() -> Self {
Self { body: Vec::new(), ..Default::default() }
}
// is_empty
/// Returns whether the response body is empty.
///
/// This method returns whether the response body is empty. This is
/// equivalent to checking whether the length of the response body is zero.
///
#[must_use]
pub fn is_empty(&self) -> bool {
self.body.is_empty()
}
// len
/// Returns the length of the response body.
///
/// This method returns the length of the response body, in bytes. This is
/// equivalent to the length of the vector of bytes that the response body
/// contains.
///
#[must_use]
pub fn len(&self) -> usize {
self.body.len()
}
// push
/// Appends a byte to the response body.
///
/// Appends a given byte onto the end of the response body's existing byte
/// data. The response body is not required to be valid UTF8, so this method
/// does not check the validity of the byte before appending it.
///
/// This method accepts a [`u8`] instead of a [`char`] because a [`char`]
/// represents a single Unicode scalar value. In Rust, a [`char`] is always
/// 4 bytes long because it can represent any Unicode scalar value,
/// including those outside the Basic Multilingual Plane. If `push()`
/// accepted a [`char`], it would be signaling that [`UnpackedResponseBody`]
/// is Unicode-aware and can handle any Unicode character — which is not the
/// case. A [`u8`], on the other hand, represents a single byte. By having
/// `push()` accept a [`u8`], it's signaling that [`UnpackedResponseBody`]
/// is byte-oriented. A specific [`push_char()`](UnpackedResponseBody::push_char())
/// method is also available, but `push()` is the most general method for
/// appending bytes to the response body.
///
/// # Parameters
///
/// * `byte` - The byte to append to the response body.
///
/// # See also
///
/// * [`UnpackedResponseBody::push_bytes()`]
/// * [`UnpackedResponseBody::push_char()`]
/// * [`UnpackedResponseBody::push_str()`]
///
pub fn push(&mut self, byte: u8) {
self.body.push(byte);
}
// push_bytes
/// Appends a byte slice to the response body.
///
/// Appends a given byte slice onto the end of the response body. The byte
/// slice is appended to the end of the response body's existing byte data.
/// The response body is not required to be valid UTF8, so this method does
/// not check the validity of the byte slice before appending it.
///
/// # Parameters
///
/// * `bytes` - The byte slice to append to the response body.
///
/// # See also
///
/// * [`UnpackedResponseBody::push()`]
/// * [`UnpackedResponseBody::push_char()`]
/// * [`UnpackedResponseBody::push_str()`]
///
pub fn push_bytes(&mut self, bytes: &[u8]) {
self.body.extend_from_slice(bytes);
}
// push_char
/// Appends a [`char`] to the response body.
///
/// Appends a given character onto the end of the response body. The
/// [`char`] is converted to bytes and then appended to the end of the
/// response body's existing byte data.
///
/// # Parameters
///
/// * `char` - The [`char`] to append to the response body.
///
/// # See also
///
/// * [`UnpackedResponseBody::push()`]
/// * [`UnpackedResponseBody::push_bytes()`]
/// * [`UnpackedResponseBody::push_str()`]
///
pub fn push_char(&mut self, char: &char) {
let mut bytes = [0; 4];
let used = char.encode_utf8(&mut bytes).len();
#[expect(clippy::indexing_slicing, reason = "Infallible")]
self.body.extend(&bytes[..used]);
}
// push_str
/// Appends a string slice to the response body.
///
/// Appends a given string slice onto the end of the response body. The
/// string slice is converted to bytes and then appended to the end of the
/// response body's existing byte data.
///
/// # Parameters
///
/// * `string` - The string slice to append to the response body.
///
/// # See also
///
/// * [`UnpackedResponseBody::push()`]
/// * [`UnpackedResponseBody::push_char()`]
/// * [`UnpackedResponseBody::push_bytes()`]
///
pub fn push_str(&mut self, string: &str) {
self.body.extend_from_slice(string.as_bytes());
}
}
// Add &[u8]
impl Add<&[u8]> for UnpackedResponseBody {
type Output = Self;
// add
/// Adds a [`&[u8]`](https://doc.rust-lang.org/std/primitive.slice.html) to
/// an [`UnpackedResponseBody`].
fn add(mut self, rhs: &[u8]) -> Self {
self.push_bytes(rhs);
self
}
}
// Add &[u8; N]
impl<const N: usize> Add<&[u8; N]> for UnpackedResponseBody {
type Output = Self;
// add
/// Adds a [`&[u8; N]`](https://doc.rust-lang.org/std/primitive.slice.html)
/// to an [`UnpackedResponseBody`].
fn add(mut self, rhs: &[u8; N]) -> Self {
self.push_bytes(rhs);
self
}
}
// Add char
impl Add<char> for UnpackedResponseBody {
type Output = Self;
// add
/// Adds a [`char`] to an [`UnpackedResponseBody`].
fn add(mut self, rhs: char) -> Self {
self.push_char(&rhs);
self
}
}
// Add &char
impl Add<&char> for UnpackedResponseBody {
type Output = Self;
// add
/// Adds a [`&char`](char) to an [`UnpackedResponseBody`].
fn add(mut self, rhs: &char) -> Self {
self.push_char(rhs);
self
}
}
// Add &str
impl Add<&str> for UnpackedResponseBody {
type Output = Self;
// add
/// Adds a [`&str`](str) to an [`UnpackedResponseBody`].
fn add(mut self, rhs: &str) -> Self {
self.push_str(rhs);
self
}
}
// Add String
impl Add<String> for UnpackedResponseBody {
type Output = Self;
// add
/// Adds a [`String`] to an [`UnpackedResponseBody`].
fn add(mut self, rhs: String) -> Self {
self.push_str(&rhs);
self
}
}
// Add &String
impl Add<&String> for UnpackedResponseBody {
type Output = Self;
// add
/// Adds a [`&String`](String) to an [`UnpackedResponseBody`].
fn add(mut self, rhs: &String) -> Self {
self.push_str(rhs);
self
}
}
// Add Box<str>
impl Add<Box<str>> for UnpackedResponseBody {
type Output = Self;
// add
/// Adds a [boxed](Box) [string](str) slice to an [`UnpackedResponseBody`].
fn add(mut self, rhs: Box<str>) -> Self::Output {
self.push_str(&rhs);
self
}
}
// Add Cow<str>
impl<'a> Add<Cow<'a, str>> for UnpackedResponseBody {
type Output = Self;
// add
/// Adds a [clone-on-write](Cow) [string](str) to an
/// [`UnpackedResponseBody`].
fn add(mut self, rhs: Cow<'a, str>) -> Self::Output {
self.push_str(&rhs);
self
}
}
// Add u8
impl Add<u8> for UnpackedResponseBody {
type Output = Self;
// add
/// Adds a [`u8`] to an [`UnpackedResponseBody`].
fn add(mut self, rhs: u8) -> Self {
self.push(rhs);
self
}
}
// Add Vec<u8>
impl Add<Vec<u8>> for UnpackedResponseBody {
type Output = Self;
// add
/// Adds a [`Vec[u8]`](Vec) to an [`UnpackedResponseBody`].
fn add(mut self, rhs: Vec<u8>) -> Self {
self.push_bytes(&rhs);
self
}
}
// Add &Vec<u8>
impl Add<&Vec<u8>> for UnpackedResponseBody {
type Output = Self;
// add
/// Adds a [`&Vec[u8]`](Vec) to an [`UnpackedResponseBody`].
fn add(mut self, rhs: &Vec<u8>) -> Self {
self.push_bytes(rhs);
self
}
}
// Add Self
impl Add<Self> for UnpackedResponseBody {
type Output = Self;
// add
/// Adds an [`UnpackedResponseBody`] to an [`UnpackedResponseBody`].
fn add(mut self, rhs: Self) -> Self {
self.push_bytes(&rhs.body);
self
}
}
// Add &Self
impl Add<&Self> for UnpackedResponseBody {
type Output = Self;
// add
/// Adds an [`&UnpackedResponseBody`](UnpackedResponseBody) to an
/// [`UnpackedResponseBody`].
fn add(mut self, rhs: &Self) -> Self {
self.push_bytes(rhs.as_bytes());
self
}
}
// AddAssign &[u8]
impl AddAssign<&[u8]> for UnpackedResponseBody {
// add_assign
/// Adds a [`&[u8]`](https://doc.rust-lang.org/std/primitive.slice.html) to
/// an [`UnpackedResponseBody`].
fn add_assign(&mut self, rhs: &[u8]) {
self.push_bytes(rhs);
}
}
// AddAssign &[u8; N]
impl<const N: usize> AddAssign<&[u8; N]> for UnpackedResponseBody {
// add_assign
/// Adds a [`&[u8; N]`](https://doc.rust-lang.org/std/primitive.slice.html)
/// to an [`UnpackedResponseBody`].
fn add_assign(&mut self, rhs: &[u8; N]) {
self.push_bytes(rhs);
}
}
// AddAssign char
impl AddAssign<char> for UnpackedResponseBody {
// add_assign
/// Adds a [`char`] to an [`UnpackedResponseBody`].
fn add_assign(&mut self, rhs: char) {
self.push_char(&rhs);
}
}
// AddAssign &char
impl AddAssign<&char> for UnpackedResponseBody {
// add_assign
/// Adds a [`&char`](char) to an [`UnpackedResponseBody`].
fn add_assign(&mut self, rhs: &char) {
self.push_char(rhs);
}
}
// AddAssign &str
impl AddAssign<&str> for UnpackedResponseBody {
// add_assign
/// Adds a [`&str`](str) to an [`UnpackedResponseBody`].
fn add_assign(&mut self, rhs: &str) {
self.push_str(rhs);
}
}
// AddAssign String
impl AddAssign<String> for UnpackedResponseBody {
// add_assign
/// Adds a [`String`] to an [`UnpackedResponseBody`].
fn add_assign(&mut self, rhs: String) {
self.push_str(&rhs);
}
}
// AddAssign &String
impl AddAssign<&String> for UnpackedResponseBody {
// add_assign
/// Adds a [`&String`](String) to an [`UnpackedResponseBody`].
fn add_assign(&mut self, rhs: &String) {
self.push_str(rhs);
}
}
// AddAssign Box<str>
impl AddAssign<Box<str>> for UnpackedResponseBody {
// add_assign
/// Adds a [boxed](Box) [string](str) slice to an [`UnpackedResponseBody`].
fn add_assign(&mut self, rhs: Box<str>) {
self.push_str(&rhs);
}
}
// AddAssign Cow<str>
impl<'a> AddAssign<Cow<'a, str>> for UnpackedResponseBody {
// add_assign
/// Adds a [clone-on-write](Cow) [string](str) to an
/// [`UnpackedResponseBody`].
fn add_assign(&mut self, rhs: Cow<'a, str>){
self.push_str(&rhs);
}
}
// AddAssign u8
impl AddAssign<u8> for UnpackedResponseBody {
// add_assign
/// Adds a [`u8`] to an [`UnpackedResponseBody`].
fn add_assign(&mut self, rhs: u8) {
self.push(rhs);
}
}
// AddAssign Vec<u8>
impl AddAssign<Vec<u8>> for UnpackedResponseBody {
// add_assign
/// Adds a [`Vec[u8]`](Vec) to an [`UnpackedResponseBody`].
fn add_assign(&mut self, rhs: Vec<u8>) {
self.push_bytes(&rhs);
}
}
// AddAssign &Vec<u8>
impl AddAssign<&Vec<u8>> for UnpackedResponseBody {
// add_assign
/// Adds a [`&Vec[u8]`](Vec) to an [`UnpackedResponseBody`].
fn add_assign(&mut self, rhs: &Vec<u8>) {
self.push_bytes(rhs);
}
}
// AddAssign Self
impl AddAssign<Self> for UnpackedResponseBody {
// add_assign
/// Adds an [`UnpackedResponseBody`] to an [`UnpackedResponseBody`].
fn add_assign(&mut self, rhs: Self) {
self.push_bytes(&rhs.body);
}
}
// AddAssign &Self
impl AddAssign<&Self> for UnpackedResponseBody {
// add_assign
/// Adds an [`&UnpackedResponseBody`](UnpackedResponseBody) to an
/// [`UnpackedResponseBody`].
fn add_assign(&mut self, rhs: &Self) {
self.push_bytes(rhs.as_bytes());
}
}
// AsMut [u8]
impl AsMut<[u8]> for UnpackedResponseBody {
// as_mut
fn as_mut(&mut self) -> &mut [u8] {
self.as_mut_bytes()
}
}
// AsRef [u8]
impl AsRef<[u8]> for UnpackedResponseBody {
// as_ref
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
// Clone
impl Clone for UnpackedResponseBody {
// clone
fn clone(&self) -> Self {
Self { body: self.body.clone(), ..Default::default() }
}
// clone_from
fn clone_from(&mut self, source: &Self) {
self.body.clone_from(&source.body);
}
}
// Debug
impl Debug for UnpackedResponseBody {
// fmt
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("UnpackedResponseBody")
.field("body", &self.to_string())
.field("content_type", &self.content_type)
.finish()
}
}
// Display
impl Display for UnpackedResponseBody {
// fmt
/// Formats the response body for display.
///
/// This method serialises the response body based on the content type. If
/// the content type is [`ContentType::Text`], then the response body is
/// serialised to an ordinary [`String`]. If the content type is
/// [`ContentType::Binary`], then the response body is serialised to a
/// base64-encoded [`String`].
///
/// Note that as no validation checks are performed on the response body
/// contents, it is not guaranteed to be UTF8, and therefore if not
/// specified as binary it is possible that the serialised string will not
/// totally match the original response body contents. This is because the
/// conversion of the response body bytes to a UTF8 string will be lossy if
/// there are invalid characters.
///
/// # See also
///
/// * [`UnpackedResponseBody::serialize()`]
/// * [`UnpackedResponseBody::to_base64()`]
///
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let body = match self.content_type {
ContentType::Text => String::from_utf8_lossy(&self.body),
ContentType::Binary => Cow::Owned(self.to_base64()),
};
write!(f, "{body}")
}
}
// From &[u8]
impl From<&[u8]> for UnpackedResponseBody {
// from
/// Converts a [`&[u8]`](https://doc.rust-lang.org/std/primitive.slice.html)
/// to an [`UnpackedResponseBody`].
fn from(b: &[u8]) -> Self {
Self { body: b.to_vec(), ..Default::default() }
}
}
// From &[u8; N]
impl<const N: usize> From<&[u8; N]> for UnpackedResponseBody {
// from
/// Converts a [`&[u8; N]`](https://doc.rust-lang.org/std/primitive.slice.html)
/// to an [`UnpackedResponseBody`].
fn from(b: &[u8; N]) -> Self {
Self { body: b.to_vec(), ..Default::default() }
}
}
// From char
impl From<char> for UnpackedResponseBody {
// from
/// Converts a [`char`] to an [`UnpackedResponseBody`].
///
/// Note that it does this in the way that is most compatible with
/// [`String`] conversion. The [`char`] type in Rust represents a Unicode
/// scalar value. That means a single [`char`] value corresponds to one
/// Unicode character. But Unicode characters can have a wide range of
/// values, from `0` to `0x10FFFF` (this range excludes the surrogate
/// pairs), and this value range doesn't fit into a single byte. That's why
/// [`char`] in Rust is 4 bytes, because it has to accommodate any possible
/// Unicode scalar value. The UTF-8 encoded representation of the `ñ`
/// character is `[195, 177]`, but in memory, a [`char`] containing `'ñ'`
/// does not hold the bytes `[195, 177]`. Instead, it holds the Unicode
/// scalar value for `ñ`, which is `U+00F1`, or in integer terms, `241`.
/// When we convert a [`char`] to a [`u32`] directly, we're taking this
/// scalar value (like `241` for `ñ`) and representing it in memory as a
/// 4-byte integer. So using code such as
/// `(c as u32).to_le_bytes().to_vec()` would result in [241, 0, 0, 0], and
/// not [195, 177]. This behaviour would not match expectation and would not
/// match the behaviour of [`String`] conversion. To get the UTF-8 encoded
/// bytes of a [`char`], we need to use encoding methods because we're
/// effectively translating from the Unicode scalar value to its UTF-8 byte
/// sequence. This is what the [`encode_utf8()`](char::encode_utf8()) method
/// provides. To put it another way: [`char`] isn't storing bytes, it's
/// storing a Unicode scalar value. UTF-8 is one of the ways to represent
/// that value (and the most common one in Rust).
///
fn from(c: char) -> Self {
let mut bytes = [0; 4];
let used = c.encode_utf8(&mut bytes).len();
#[expect(clippy::indexing_slicing, reason = "Infallible")]
Self { body: bytes[..used].to_vec(), ..Default::default() }
}
}
// From &char
impl From<&char> for UnpackedResponseBody {
// from
/// Converts a [`&char`](char) to an [`UnpackedResponseBody`].
fn from(c: &char) -> Self {
Self::from(c.to_owned())
}
}
// From Json
impl From<Json> for UnpackedResponseBody {
// from
/// Converts a [`serde_json::Value`] to an [`UnpackedResponseBody`].
fn from(j: Json) -> Self {
Self { body: j.to_string().into_bytes(), ..Default::default() }
}
}
// From &Json
impl From<&Json> for UnpackedResponseBody {
// from
/// Converts a [`&serde_json::Value`](serde_json::Value) to an
/// [`UnpackedResponseBody`].
fn from(j: &Json) -> Self {
Self { body: j.to_string().into_bytes(), ..Default::default() }
}
}
// From HyperBody
impl From<HyperBody> for UnpackedResponseBody {
// from
/// Converts a [`UnsyncBoxBody<Bytes, E>`](UnsyncBoxBody) to an
/// [`UnpackedResponseBody`].
fn from(b: HyperBody) -> Self {
let bytes = executor::block_on(to_bytes(b));
let body = bytes.map_or_else(|_| b"Conversion error".to_vec(), |body| body.to_vec());
Self { body, ..Default::default() }
}
}
// From &str
impl From<&str> for UnpackedResponseBody {
// from
/// Converts a [`&str`](str) to an [`UnpackedResponseBody`].
fn from(s: &str) -> Self {
Self { body: s.to_owned().as_bytes().to_vec(), ..Default::default() }
}
}
// From &mut str
impl From<&mut str> for UnpackedResponseBody {
// from
/// Converts a [`&mut str`](str) to an [`UnpackedResponseBody`].
fn from(s: &mut str) -> Self {
Self { body: s.to_owned().as_bytes().to_vec(), ..Default::default() }
}
}
// From String
impl From<String> for UnpackedResponseBody {
// from
/// Converts a [`String`] to an [`UnpackedResponseBody`].
fn from(s: String) -> Self {
Self { body: s.into_bytes(), ..Default::default() }
}
}
// From &String
impl From<&String> for UnpackedResponseBody {
// from
/// Converts a [`&String`](String) to an [`UnpackedResponseBody`].
fn from(s: &String) -> Self {
Self { body: s.as_str().as_bytes().to_vec(), ..Default::default() }
}
}
// From Box<str>
impl From<Box<str>> for UnpackedResponseBody {
// from
/// Converts a [boxed](Box) [string](str) slice to an
/// [`UnpackedResponseBody`].
fn from(s: Box<str>) -> Self {
Self { body: s.into_string().into_bytes(), ..Default::default() }
}
}
// From Cow<str>
impl<'a> From<Cow<'a, str>> for UnpackedResponseBody {
// from
/// Converts a [clone-on-write](Cow) [string](str) to an
/// [`UnpackedResponseBody`].
fn from(s: Cow<'a, str>) -> Self {
Self { body: s.into_owned().into_bytes(), ..Default::default() }
}
}
// From UnsyncBoxBody<Bytes>
impl<E> From<UnsyncBoxBody<Bytes, E>> for UnpackedResponseBody
where
E: Error + 'static,
{
// from
/// Converts a [`UnsyncBoxBody<Bytes, E>`](UnsyncBoxBody) to an
/// [`UnpackedResponseBody`].
fn from(b: UnsyncBoxBody<Bytes, E>) -> Self {
let bytes = executor::block_on(to_bytes(b));
let body = bytes.map_or_else(|_| b"Conversion error".to_vec(), |body| body.to_vec());
Self { body, ..Default::default() }
}
}
// From u8
impl From<u8> for UnpackedResponseBody {
// from
/// Converts a [`u8`] to an [`UnpackedResponseBody`].
fn from(c: u8) -> Self {
Self { body: Vec::from([c]), ..Default::default() }
}
}
// From Vec<u8>
impl From<Vec<u8>> for UnpackedResponseBody {
// from
/// Converts a [`Vec[u8]`](Vec) to an [`UnpackedResponseBody`].
fn from(v: Vec<u8>) -> Self {
Self { body: v, ..Default::default() }
}
}
// From &Vec<u8>
impl From<&Vec<u8>> for UnpackedResponseBody {
// from
/// Converts a [`&Vec[u8]`](Vec) to an [`UnpackedResponseBody`].
fn from(v: &Vec<u8>) -> Self {
Self { body: v.clone(), ..Default::default() }
}
}
// FromStr
impl FromStr for UnpackedResponseBody {
type Err = Infallible;
// from_str
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self { body: s.as_bytes().to_vec(), ..Default::default() })
}
}
// PartialEq
impl PartialEq for UnpackedResponseBody {
// eq
fn eq(&self, other: &Self) -> bool {
self.body == other.body
}
}
// Serialize
impl Serialize for UnpackedResponseBody {
// serialize
/// Serialises the response body to a [`String`].
///
/// This method serialises the response body based on the content type. If
/// the content type is [`ContentType::Text`], then the response body is
/// serialised to an ordinary [`String`]. If the content type is
/// [`ContentType::Binary`], then the response body is serialised to a
/// base64-encoded [`String`].
///
/// Note that as no validation checks are performed on the response body
/// contents, it is not guaranteed to be UTF8, and therefore if not
/// specified as binary it is possible that the serialised string will not
/// totally match the original response body contents. This is because the
/// conversion of the response body bytes to a UTF8 string will be lossy if
/// there are invalid characters.
///
/// # See also
///
/// * [`UnpackedResponseBody::deserialize()`]
/// * [`UnpackedResponseBody::<Display>fmt()`]
/// * [`UnpackedResponseBody::to_base64()`]
///
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
// Deserialize
impl <'de> Deserialize<'de> for UnpackedResponseBody {
// deserialize
/// Deserialises the response body from a [`String`].
///
/// This method deserialises the response body based on the content type.
/// However, as this method is not an instance method and it is not possible
/// to specify the content type in advance, it has to try to detect it. It
/// does this by attempting to decode the string as base64. If this succeeds
/// then it will set the content type as [`ContentType::Binary`]. If this
/// fails then it will assume the content type is [`ContentType::Text`], and
/// deserialises the string in standard fashion.
///
/// Note that as the incoming data is from a [`String`], and Rust strings
/// are are all valid UTF8, the resulting deserialised response body is
/// guaranteed to be UTF8 if the content type is determined to be
/// [`ContentType::Text`]. If base64 is detected then the deserialised bytes
/// are not guaranteed to be valid UTF8, as no validation checks of that
/// nature are performed against the response body.
///
/// # See also
///
/// * [`UnpackedResponseBody::deserialize()`]
/// * [`UnpackedResponseBody::from_base64()`]
///
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let string = String::deserialize(deserializer)?;
#[expect(clippy::option_if_let_else, reason = "Using map_or_else() here would not be as clear, and no more concise")]
match BASE64.decode(&string) {
Ok(decoded) => Ok(Self { body: decoded, content_type: ContentType::Binary }),
Err(_) => Ok(Self { body: string.into_bytes(), content_type: ContentType::Text }),
}
}
}
// Write
impl Write for UnpackedResponseBody {
// write_str
fn write_str(&mut self, s: &str) -> fmt::Result {
self.push_str(s);
Ok(())
}
}
// Traits
//§ ResponseExt
/// This trait provides additional functionality to [`Response`].
pub trait ResponseExt {
// unpack
/// Returns an [`UnpackedResponse`] containing the unpacked response data.
///
/// This will unpack the response and provide the headers and body in a
/// more accessible form, to allow it to be checked and compared. This is
/// useful for testing, as the entire set of headers plus body can be
/// checked all at once, and also for printing/logging.
///
/// If specific headers or body content needs to be checked, it is
/// recommended to use the standard functions as they will be more
/// efficient and performant. Notably, this function will consume the
/// response body, which is necessary because the response might be
/// streamed. In order to provide the full response, the whole body must be
/// read first. This will obviously use more memory than would be used under
/// normal circumstances, so it is not recommended to use this function
/// without considering purpose and effect. For tests, ensuring a response
/// body matches, this is fine, as the data is known and constrained, and
/// memory/performance is less of a concern.
///
/// # Errors
///
/// This function will potentially return an error if the response body
/// cannot be converted to bytes. This should not happen under normal
/// circumstances, but it may be possible if the response body is streamed
/// and the stream cannot be read. Many implementations of this function are
/// in fact infallible.
///
/// At present [`ResponseError`] only contains one error variant, but it is
/// possible that more will be added.
///
/// # See also
///
/// * [`axum::response`](https://docs.rs/axum/latest/axum/response/index.html)
/// * [`axum::response::Response`](https://docs.rs/axum/latest/axum/response/type.Response.html)
/// * [`http::Response`]
/// * [`hyper::Response`]
/// * [`UnpackedResponse`]
///
fn unpack(&mut self) -> Result<UnpackedResponse, ResponseError>;
}
// Response<()>
impl ResponseExt for Response<()> {
// unpack
fn unpack(&mut self) -> Result<UnpackedResponse, ResponseError> {
Ok(convert_response(self.status(), self.headers(), &Bytes::new()))
}
}
// Response<UnsyncBoxBody<Bytes>>
impl<E> ResponseExt for Response<UnsyncBoxBody<Bytes, E>>
where
E: Error + 'static,
{
// unpack
fn unpack(&mut self) -> Result<UnpackedResponse, ResponseError> {
let result = executor::block_on(to_bytes(self.body_mut()));
match result {
Ok(body) => Ok(convert_response(self.status(), self.headers(), &body)),
Err(err) => Err(ResponseError::ConversionError(Box::new(err))),
}
}
}
// Response<HyperBody>
impl ResponseExt for Response<HyperBody> {
// unpack
fn unpack(&mut self) -> Result<UnpackedResponse, ResponseError> {
let result = executor::block_on(to_bytes(self.body_mut()));
match result {
Ok(body) => Ok(convert_response(self.status(), self.headers(), &body)),
Err(err) => Err(ResponseError::ConversionError(Box::new(err))),
}
}
}
// Response<String>
impl ResponseExt for Response<String> {
// unpack
fn unpack(&mut self) -> Result<UnpackedResponse, ResponseError> {
let result = executor::block_on(to_bytes(self.body_mut()));
match result {
Ok(body) => Ok(convert_response(self.status(), self.headers(), &body)),
Err(err) => Err(ResponseError::ConversionError(Box::new(err))),
}
}
}
// Functions
// convert_headers
/// Returns a vector of unpacked response headers.
///
/// These are returned in a vector rather than a hashmap because there may be
/// multiple headers with the same name. They are sorted by name, and then by
/// value, allowing for reliable comparison. Sorting does break the original
/// order of the headers, but this should only very rarely matter.
///
/// # See also
///
/// * [`ResponseExt::unpack()`]
/// * [`UnpackedResponse`]
/// * [`UnpackedResponseHeader`]
///
fn convert_headers(headermap: &HeaderMap<HeaderValue>) -> Vec<UnpackedResponseHeader> {
let mut headers = vec![];
#[expect(clippy::shadow_reuse, reason = "Clear purpose")]
for (name, value) in headermap {
let name = name.as_str().to_owned();
let value = String::from_utf8_lossy(value.as_bytes()).into_owned();
headers.push(UnpackedResponseHeader { name, value });
}
headers.sort_by(|a, b| {
match a.name.cmp(&b.name) {
Ordering::Equal => a.value.cmp(&b.value),
Ordering::Greater => Ordering::Greater,
Ordering::Less => Ordering::Less,
}
});
headers
}
// convert_response
/// Returns an [`UnpackedResponse`] containing the unpacked response data.
///
/// This function carries out the common part of the conversion process for
/// [`ResponseExt::unpack()`]. As [`unpack()`](ResponseExt::unpack()) has a
/// number of implementations, the common code is abstracted out into this
/// function.
///
/// # Parameters
///
/// * `status` - The response status code.
/// * `headers` - The response headers.
/// * `body` - The response body.
///
/// # See also
///
/// * [`axum::response`](https://docs.rs/axum/latest/axum/response/index.html)
/// * [`axum::response::Response`](https://docs.rs/axum/latest/axum/response/type.Response.html)
/// * [`http::Response`]
/// * [`hyper::Response`]
/// * [`ResponseExt::unpack()`]
/// * [`UnpackedResponse`]
/// * [`UnpackedResponseHeader`]
///
fn convert_response(
status: StatusCode,
headers: &HeaderMap<HeaderValue>,
body: &Bytes,
) -> UnpackedResponse {
UnpackedResponse {
status,
headers: convert_headers(headers),
body: UnpackedResponseBody { body: body.to_vec(), ..Default::default() },
}
}
// serialize_status_code
/// Returns the status code as a number.
///
/// This function is used by [`serde`] to serialise the status code as a number
/// rather than an enum. This is necessary because the [`UnpackedResponse`]
/// struct is used for comparison, and the status code is not directly
/// comparable to a number.
///
/// # Parameters
///
/// * `status_code` - The status code to serialise.
/// * `serializer` - The serialiser to use.
///
/// # See also
///
/// * [`deserialize_status_code()`]
/// * [`http::StatusCode`]
/// * [`UnpackedResponse`]
///
#[expect(clippy::trivially_copy_pass_by_ref, reason = "Needs to match trait")]
fn serialize_status_code<S>(status_code: &StatusCode, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u16(status_code.as_u16())
}
// deserialize_status_code
/// Returns the status code as an enum.
///
/// This function is used by [`serde`] to deserialise the status code as an
/// enum rather than a number. This is necessary because the
/// [`UnpackedResponse`] struct is used for comparison, and the status code is
/// not directly comparable to a number.
///
/// # Parameters
///
/// * `deserializer` - The deserialiser to use.
///
/// # See also
///
/// * [`http::StatusCode`]
/// * [`serialize_status_code()`]
/// * [`UnpackedResponse`]
///
fn deserialize_status_code<'de, D>(deserializer: D) -> Result<StatusCode, D::Error>
where
D: Deserializer<'de>,
{
let status_code_value: u16 = Deserialize::deserialize(deserializer)?;
let status_code = StatusCode::from_u16(status_code_value).map_err(DeError::custom)?;
Ok(status_code)
}