rusty_cat/file_transfer_record.rs
1use crate::direction::Direction;
2use crate::ids::TaskId;
3use crate::transfer_status::TransferStatus;
4
5/// Immutable progress/state record emitted by transfer callbacks.
6///
7/// This snapshot is safe to clone and pass across threads.
8#[derive(Debug, Clone)]
9pub struct FileTransferRecord {
10 /// Unique task identifier.
11 task_id: TaskId,
12 /// File signature (upload usually MD5; download may use client-defined value).
13 file_sign: String,
14 /// Display file name.
15 file_name: String,
16 /// Total file size in bytes.
17 total_size: u64,
18 /// Progress ratio in range `0.0..=1.0`.
19 progress: f32,
20 /// Current transfer state.
21 status: TransferStatus,
22 /// Transfer direction.
23 direction: Direction,
24}
25
26impl FileTransferRecord {
27 /// Creates a new transfer record.
28 ///
29 /// # Range guidance
30 ///
31 /// - `total_size >= 0`
32 /// - `progress` should stay in `0.0..=1.0`
33 ///
34 /// # Examples
35 ///
36 /// ```no_run
37 /// use rusty_cat::api::{Direction, FileTransferRecord, TaskId, TransferStatus};
38 ///
39 /// # fn sample(task_id: TaskId) {
40 /// let record = FileTransferRecord::new(
41 /// task_id,
42 /// "sign".to_string(),
43 /// "file.bin".to_string(),
44 /// 1024,
45 /// 0.5,
46 /// TransferStatus::Transmission,
47 /// Direction::Download,
48 /// );
49 /// assert_eq!(record.total_size(), 1024);
50 /// # }
51 /// ```
52 pub fn new(
53 task_id: TaskId,
54 file_sign: String,
55 file_name: String,
56 total_size: u64,
57 progress: f32,
58 status: TransferStatus,
59 direction: Direction,
60 ) -> Self {
61 Self {
62 task_id,
63 file_sign,
64 file_name,
65 total_size,
66 progress,
67 status,
68 direction,
69 }
70 }
71
72 /// Returns file signature.
73 ///
74 /// # Examples
75 ///
76 /// ```no_run
77 /// use rusty_cat::api::FileTransferRecord;
78 ///
79 /// fn read_sign(record: &FileTransferRecord) {
80 /// let _ = record.file_sign();
81 /// }
82 /// ```
83 pub fn file_sign(&self) -> &str {
84 &self.file_sign
85 }
86
87 /// Returns display file name.
88 ///
89 /// # Examples
90 ///
91 /// ```no_run
92 /// use rusty_cat::api::FileTransferRecord;
93 ///
94 /// fn read_name(record: &FileTransferRecord) {
95 /// let _ = record.file_name();
96 /// }
97 /// ```
98 pub fn file_name(&self) -> &str {
99 &self.file_name
100 }
101
102 /// Returns total file size in bytes.
103 ///
104 /// # Examples
105 ///
106 /// ```no_run
107 /// use rusty_cat::api::FileTransferRecord;
108 ///
109 /// fn read_size(record: &FileTransferRecord) {
110 /// let _ = record.total_size();
111 /// }
112 /// ```
113 pub fn total_size(&self) -> u64 {
114 self.total_size
115 }
116
117 /// Returns progress ratio in range `0.0..=1.0`.
118 ///
119 /// # Examples
120 ///
121 /// ```no_run
122 /// use rusty_cat::api::FileTransferRecord;
123 ///
124 /// fn read_progress(record: &FileTransferRecord) {
125 /// let _ = record.progress();
126 /// }
127 /// ```
128 pub fn progress(&self) -> f32 {
129 self.progress
130 }
131
132 /// Returns current transfer status.
133 ///
134 /// # Examples
135 ///
136 /// ```no_run
137 /// use rusty_cat::api::FileTransferRecord;
138 ///
139 /// fn read_status(record: &FileTransferRecord) {
140 /// let _ = record.status();
141 /// }
142 /// ```
143 pub fn status(&self) -> &TransferStatus {
144 &self.status
145 }
146
147 /// Returns transfer direction.
148 ///
149 /// # Examples
150 ///
151 /// ```no_run
152 /// use rusty_cat::api::FileTransferRecord;
153 ///
154 /// fn read_direction(record: &FileTransferRecord) {
155 /// let _ = record.direction();
156 /// }
157 /// ```
158 pub fn direction(&self) -> Direction {
159 self.direction
160 }
161
162 /// Returns task ID.
163 ///
164 /// # Examples
165 ///
166 /// ```no_run
167 /// use rusty_cat::api::FileTransferRecord;
168 ///
169 /// fn read_task_id(record: &FileTransferRecord) {
170 /// let _ = record.task_id();
171 /// }
172 /// ```
173 pub fn task_id(&self) -> TaskId {
174 self.task_id
175 }
176}