Skip to content

Commit 1ab9e14

Browse files
committed
docs(rust): improve documentation of a number of public functions in the IdUri type.
1 parent 74e7881 commit 1ab9e14

File tree

1 file changed

+181
-15
lines changed
  • rust/catalyst-types/src/id_uri

1 file changed

+181
-15
lines changed

rust/catalyst-types/src/id_uri/mod.rs

Lines changed: 181 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
33
// cspell: words userinfo rngs Fftx csprng
44

5-
mod errors;
6-
mod key_rotation;
7-
mod role_index;
5+
pub mod errors;
6+
pub mod key_rotation;
7+
pub mod role_index;
88

99
use std::{
1010
fmt::{Display, Formatter},
@@ -162,10 +162,27 @@ impl IdUri {
162162
}
163163
}
164164

165-
/// Add or change the nonce to a specific value in a Catalyst ID URI.
165+
/// Add or change the nonce (a unique identifier for a data update) to a specific
166+
/// value in a Catalyst `IdUri`.
166167
///
167-
/// Note, this will not fail, but if the Datetime is < `MIN_NONCE`,
168-
/// or greater than `MAX_NONCE`, it will be clamped into that range.
168+
/// This method is intended for use with trusted data where the nonce is known and
169+
/// verified beforehand. It ensures that the provided nonce is within the valid
170+
/// range, clamping it if necessary between `MIN_NONCE` and `MAX_NONCE`.
171+
/// Properly generated or trusted nonces will not be altered by this function.
172+
///
173+
/// # Parameters
174+
/// - `nonce`: A `DateTime` representing the specific nonce value to set in the
175+
/// Catalyst `IdUri`. This should be a valid UTC datetime.
176+
///
177+
/// # Returns
178+
/// The updated Catalyst `IdUri` with the specified nonce, if it was within the
179+
/// allowed range; otherwise, it will be updated with a clamped value of the
180+
/// nonce.
181+
///
182+
/// # Safety
183+
/// - **Pre-validation of the nonce is required**: If you are working with untrusted
184+
/// data, ensure that the nonce has been pre-validated and take appropriate action
185+
/// before calling this function.
169186
#[must_use]
170187
pub fn with_specific_nonce(self, nonce: DateTime<Utc>) -> Self {
171188
let secs = nonce.timestamp();
@@ -182,13 +199,44 @@ impl IdUri {
182199
Self { nonce, ..self }
183200
}
184201

185-
/// Add or change the nonce in a Catalyst ID URI.
202+
/// Add or change the nonce in a Catalyst ID URI. The nonce will be set to the current
203+
/// UTC time when this method is called.
204+
///
205+
/// This function returns a new instance of the type with the nonce field updated to
206+
/// the current UTC time.
207+
///
208+
/// # Examples
209+
/// ```rust
210+
/// use catalyst_types::id_uri::IdUri;
211+
/// use chrono::Utc;
212+
///
213+
/// let id_uri = IdUri::default();
214+
/// let id_uri_with_nonce = id_uri.with_nonce();
215+
/// assert!(id_uri_with_nonce.nonce.is_some());
216+
/// ```
186217
#[must_use]
187218
pub fn with_nonce(self) -> Self {
188219
self.with_specific_nonce(Utc::now())
189220
}
190221

191222
/// Set that there is no Nonce in the ID or URI
223+
/// Represents an ID or URI without a Nonce.
224+
///
225+
/// This method creates a new instance of the type, but sets the nonce field to
226+
/// `None`. The rest of the fields are inherited from the original instance.
227+
///
228+
/// # Examples
229+
/// ```rust
230+
/// use catalyst_types::id_uri::IdUri;
231+
/// use chrono::{DateTime, Duration, Utc};
232+
/// let id_uri = "id.catalyst://cardano/FftxFnOrj2qmTuB2oZG2v0YEWJfKvQ9Gg8AgNAhDsKE"
233+
/// .parse::<IdUri>()
234+
/// .unwrap()
235+
/// .with_nonce();
236+
///
237+
/// let id_uri_without_nonce = id_uri.without_nonce();
238+
/// assert_eq!(id_uri_without_nonce.nonce(), None);
239+
/// ```
192240
#[must_use]
193241
pub fn without_nonce(self) -> Self {
194242
Self {
@@ -197,7 +245,27 @@ impl IdUri {
197245
}
198246
}
199247

200-
/// Create a new `IdUri` for an Encryption Key
248+
/// Set that the `IdUri` is used to identify an encryption key.
249+
///
250+
/// This method sets `IdUri` is identifying an encryption key.
251+
///
252+
/// # Returns
253+
///
254+
/// A new instance of the type with the updated encryption flag.
255+
///
256+
/// # Examples
257+
///
258+
/// ```rust
259+
/// use catalyst_types::id_uri::IdUri;
260+
///
261+
/// let id_uri = "id.catalyst://cardano/FftxFnOrj2qmTuB2oZG2v0YEWJfKvQ9Gg8AgNAhDsKE"
262+
/// .parse::<IdUri>()
263+
/// .unwrap();
264+
/// assert_eq!(id_uri.is_encryption_key(), false);
265+
///
266+
/// let id_uri = id_uri.with_encryption();
267+
/// assert_eq!(id_uri.is_encryption_key(), true);
268+
/// ```
201269
#[must_use]
202270
pub fn with_encryption(self) -> Self {
203271
Self {
@@ -206,7 +274,27 @@ impl IdUri {
206274
}
207275
}
208276

209-
/// Set that the ID is not for encryption
277+
/// Set that the `IdUri` is not for encryption
278+
///
279+
/// This method sets `IdUri` is not identifying an encryption key.
280+
///
281+
/// # Returns
282+
///
283+
/// A new instance of the type with the updated encryption flag.
284+
///
285+
/// # Examples
286+
///
287+
/// ```rust
288+
/// use catalyst_types::id_uri::IdUri;
289+
///
290+
/// let id_uri = "id.catalyst://cardano/FftxFnOrj2qmTuB2oZG2v0YEWJfKvQ9Gg8AgNAhDsKE#encrypt"
291+
/// .parse::<IdUri>()
292+
/// .unwrap();
293+
/// assert_eq!(id_uri.is_encryption_key(), true);
294+
///
295+
/// let id_uri = id_uri.without_encryption();
296+
/// assert_eq!(id_uri.is_encryption_key(), false);
297+
/// ```
210298
#[must_use]
211299
pub fn without_encryption(self) -> Self {
212300
Self {
@@ -215,13 +303,60 @@ impl IdUri {
215303
}
216304
}
217305

218-
/// Set the role explicitly
306+
/// Set the role explicitly.
307+
///
308+
/// This method sets the role field to the specified value while leaving other
309+
/// fields unchanged.
310+
///
311+
/// # Parameters
312+
/// - `role`: The new value for the role field.
313+
///
314+
/// # Returns
315+
///
316+
/// A new instance of the type with the updated role field.
317+
///
318+
/// # Examples
319+
///
320+
/// ```rust
321+
/// use catalyst_types::id_uri::{role_index::RoleIndex, IdUri};
322+
///
323+
/// let id_uri = "id.catalyst://cardano/FftxFnOrj2qmTuB2oZG2v0YEWJfKvQ9Gg8AgNAhDsKE"
324+
/// .parse::<IdUri>()
325+
/// .unwrap();
326+
/// let new_role: RoleIndex = 5.into();
327+
/// let id_uri_with_role = id_uri.with_role(new_role);
328+
/// let (role, _) = id_uri_with_role.role_and_rotation();
329+
/// assert_eq!(role, new_role);
330+
/// ```
219331
#[must_use]
220332
pub fn with_role(self, role: RoleIndex) -> Self {
221333
Self { role, ..self }
222334
}
223335

224-
/// Set the rotation explicitly
336+
/// Set the rotation explicitly.
337+
///
338+
/// This method sets the rotation field to the specified value while leaving other
339+
/// fields unchanged.
340+
///
341+
/// # Parameters
342+
/// - `rotation`: The new value for the rotation field. 0 = First Key, 1+ is each
343+
/// subsequent rotation.
344+
///
345+
/// # Returns
346+
/// A new instance of the type with the updated rotation field.
347+
///
348+
/// # Examples
349+
/// ```rust
350+
/// use catalyst_types::id_uri::{key_rotation::KeyRotation, IdUri};
351+
///
352+
/// let id_uri = "id.catalyst://cardano/FftxFnOrj2qmTuB2oZG2v0YEWJfKvQ9Gg8AgNAhDsKE"
353+
/// .parse::<IdUri>()
354+
/// .unwrap();
355+
/// let new_rotation: KeyRotation = 4.into();
356+
/// let id_uri_with_rotation = id_uri.with_rotation(new_rotation);
357+
/// let (_, rotation) = id_uri_with_rotation.role_and_rotation();
358+
/// assert_eq!(rotation, new_rotation);
359+
/// ```
225360
#[must_use]
226361
pub fn with_rotation(self, rotation: KeyRotation) -> Self {
227362
Self { rotation, ..self }
@@ -286,10 +421,41 @@ impl IdUri {
286421
}
287422
}
288423

289-
/// Convert the `IdUri` to its shortest form.
290-
/// This is an ID without any role/rotation information, no scheme, no username or
291-
/// nonce.
292-
/// This is used to get the most generalized form of a Catalyst ID.
424+
/// Converts the `IdUri` to its shortest form.
425+
/// This method returns a new instance of the type with no role information, no
426+
/// scheme, no username, no nonce, and no encryption settings. It effectively
427+
/// strips away all additional metadata to provide a most generalized form of the
428+
/// Catalyst ID.
429+
///
430+
/// # Returns
431+
///
432+
/// A new `IdUri` instance representing the shortest form of the current `IdUri`.
433+
///
434+
/// # Examples
435+
///
436+
/// ```rust
437+
/// use catalyst_types::id_uri::{key_rotation::KeyRotation, role_index::RoleIndex, IdUri};
438+
///
439+
/// let id_uri =
440+
/// "id.catalyst://user:1735689600@cardano/FftxFnOrj2qmTuB2oZG2v0YEWJfKvQ9Gg8AgNAhDsKE/7/5"
441+
/// .parse::<IdUri>()
442+
/// .unwrap();
443+
///
444+
/// let short_id = id_uri.as_short_id();
445+
/// assert_eq!(
446+
/// short_id.role_and_rotation(),
447+
/// (RoleIndex::default(), KeyRotation::default())
448+
/// );
449+
/// assert_eq!(short_id.username(), None);
450+
/// assert_eq!(short_id.nonce(), None);
451+
/// assert_eq!(short_id.is_encryption_key(), false);
452+
///
453+
/// let short_id_str = format!("{short_id}");
454+
/// assert_eq!(
455+
/// short_id_str,
456+
/// "cardano/FftxFnOrj2qmTuB2oZG2v0YEWJfKvQ9Gg8AgNAhDsKE"
457+
/// )
458+
/// ```
293459
#[must_use]
294460
pub fn as_short_id(&self) -> Self {
295461
self.clone()

0 commit comments

Comments
 (0)