Skip to content

Commit 53e4c2c

Browse files
committed
refactor(qbittorrent-e2e): introduce PayloadSize and PieceLength newtypes
Replace the two bare usize constants PAYLOAD_SIZE_BYTES and TORRENT_PIECE_LENGTH in filesystem_setup.rs with PayloadSize and PieceLength newtypes. Promote the constants to these types using const fn constructors, and update build_payload_fixture and build_torrent_fixture to accept the typed values. The inner usize is extracted just before the lower-level torrent_artifacts helpers that still work with primitives.
1 parent f643b44 commit 53e4c2c

4 files changed

Lines changed: 49 additions & 7 deletions

File tree

src/console/ci/qbittorrent/filesystem_setup.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use anyhow::Context;
3535
use super::qbittorrent_client::QbittorrentCredentials;
3636
use super::qbittorrent_config::QbittorrentConfigBuilder;
3737
use super::scenario_steps::{build_payload_fixture, build_torrent_fixture};
38-
use super::types::{ComposeProjectName, ContainerPath, Deadline, FileName, PollInterval};
38+
use super::types::{ComposeProjectName, ContainerPath, Deadline, FileName, PayloadSize, PieceLength, PollInterval};
3939
use super::workspace::{
4040
EphemeralWorkspace, PeerConfig, PermanentWorkspace, PreparedWorkspace, SharedFixtures, TimingConfig, TorrentFixture,
4141
TrackerFilesystem, WorkspaceResources,
@@ -46,8 +46,8 @@ const SEEDER_PASSWORD: &str = "seeder-pass";
4646
const LEECHER_PASSWORD: &str = "leecher-pass";
4747
const PAYLOAD_FILE_NAME: &str = "payload.bin";
4848
const TORRENT_FILE_NAME: &str = "payload.torrent";
49-
const PAYLOAD_SIZE_BYTES: usize = 1024 * 1024;
50-
const TORRENT_PIECE_LENGTH: usize = 16 * 1024;
49+
const PAYLOAD_SIZE_BYTES: PayloadSize = PayloadSize::new(1024 * 1024);
50+
const TORRENT_PIECE_LENGTH: PieceLength = PieceLength::new(16 * 1024);
5151
const QBITTORRENT_DOWNLOADS_PATH: &str = "/downloads";
5252
const TORRENT_POLL_INTERVAL: Duration = Duration::from_millis(500);
5353
const LOGIN_POLL_INTERVAL: Duration = Duration::from_secs(1);
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::super::super::torrent_artifacts::build_payload_bytes;
2+
use super::super::super::types::PayloadSize;
23

34
/// In-memory payload fixture used to generate torrent metadata and integrity checks.
45
pub struct GeneratedPayload {
@@ -8,8 +9,8 @@ pub struct GeneratedPayload {
89
/// Builds deterministic payload bytes for the E2E scenario.
910
///
1011
/// The generated payload is stable for a given size, which keeps test behavior reproducible.
11-
pub fn build_payload_fixture(payload_size_bytes: usize) -> GeneratedPayload {
12+
pub fn build_payload_fixture(payload_size_bytes: PayloadSize) -> GeneratedPayload {
1213
GeneratedPayload {
13-
bytes: build_payload_bytes(payload_size_bytes),
14+
bytes: build_payload_bytes(payload_size_bytes.as_usize()),
1415
}
1516
}

src/console/ci/qbittorrent/scenario_steps/fixtures/build_torrent_fixture.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use anyhow::Context;
22

33
use super::super::super::torrent_artifacts::build_torrent_bytes;
4+
use super::super::super::types::PieceLength;
45
use super::build_payload_fixture::GeneratedPayload;
56

67
/// In-memory `.torrent` fixture generated from a payload fixture.
@@ -17,9 +18,9 @@ pub fn build_torrent_fixture(
1718
payload: &GeneratedPayload,
1819
payload_name: &str,
1920
announce_url: &str,
20-
piece_length: usize,
21+
piece_length: PieceLength,
2122
) -> anyhow::Result<GeneratedTorrent> {
22-
let bytes = build_torrent_bytes(&payload.bytes, payload_name, announce_url, piece_length)
23+
let bytes = build_torrent_bytes(&payload.bytes, payload_name, announce_url, piece_length.as_usize())
2324
.context("failed to build torrent fixture bytes from payload fixture")?;
2425

2526
Ok(GeneratedTorrent { bytes })

src/console/ci/qbittorrent/types.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,43 @@ impl<'de> serde::Deserialize<'de> for TorrentHash {
443443
Ok(Self(value))
444444
}
445445
}
446+
447+
/// The total byte size of a test payload used in the E2E torrent scenario.
448+
///
449+
/// Distinct from [`PieceLength`] to prevent an accidental swap of the two
450+
/// `usize` torrent-construction arguments.
451+
#[derive(Debug, Clone, Copy)]
452+
pub(crate) struct PayloadSize(usize);
453+
454+
impl PayloadSize {
455+
/// Creates a new [`PayloadSize`] from a byte count.
456+
pub(crate) const fn new(bytes: usize) -> Self {
457+
Self(bytes)
458+
}
459+
460+
/// Returns the byte count as a `usize`.
461+
#[must_use]
462+
pub(crate) fn as_usize(self) -> usize {
463+
self.0
464+
}
465+
}
466+
467+
/// The piece length for a torrent, in bytes.
468+
///
469+
/// Distinct from [`PayloadSize`] to prevent an accidental swap of the two
470+
/// `usize` torrent-construction arguments.
471+
#[derive(Debug, Clone, Copy)]
472+
pub(crate) struct PieceLength(usize);
473+
474+
impl PieceLength {
475+
/// Creates a new [`PieceLength`] from a byte count.
476+
pub(crate) const fn new(bytes: usize) -> Self {
477+
Self(bytes)
478+
}
479+
480+
/// Returns the piece length as a `usize`.
481+
#[must_use]
482+
pub(crate) fn as_usize(self) -> usize {
483+
self.0
484+
}
485+
}

0 commit comments

Comments
 (0)