qubit_mixin/with_code.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! Traits for code property functionality
11//!
12
13/// A trait indicating that an entity class has a code property
14///
15/// This trait provides access and setting functionality for the code of
16/// domain objects, where the code is usually a globally unique identifier.
17///
18/// # Example
19///
20/// ```rust
21/// use qubit_mixin::WithCode;
22///
23/// struct Product {
24/// code: String,
25/// name: String,
26/// }
27///
28/// impl WithCode for Product {
29/// fn code(&self) -> &str {
30/// &self.code
31/// }
32///
33/// fn set_code(&mut self, code: &str) {
34/// self.code = code.to_string();
35/// }
36/// }
37///
38/// let mut product = Product {
39/// code: "PROD001".to_string(),
40/// name: "Widget".to_string(),
41/// };
42/// assert_eq!(product.code(), "PROD001");
43///
44/// product.set_code("PROD002");
45/// assert_eq!(product.code(), "PROD002");
46/// ```
47///
48pub trait WithCode {
49 /// Gets the code of the current object
50 ///
51 /// # Returns
52 ///
53 /// The code of the current object
54 fn code(&self) -> &str;
55
56 /// Sets the code of the current object
57 ///
58 /// # Parameters
59 ///
60 /// * `code` - The new code to be set
61 fn set_code(&mut self, code: &str);
62}