Expand description
Object-oriented programming (OOP) has been around since the 1960s and was first introduced in the late 1950s in artificial intelligence by an MMIT group. It is no wonder then that over the years, the concept of objects being represented by classes and attributes with inheritanted behavior.
Rust addresses this design by providing structures, traits, and implementations. However, the native ability to
extend
a class (like in other languages) makes OOP a bit of a challenge. To address this gap, Scaffolding
utilizes
Rust’s procedural macros to mimic the ability to
extend
a class - both data structure and behavior.
§Scaffolding Concept
- A class that
extends
the “Scaffolding class” should inherate all the “parent” data structure and behavior, as well as append the “child” specific data structure and behavior from the generic type being extended. - The developer should have the flexibility to adopt the default “parent” characteristics or overwrite them as desired.
- There are common class attributes that are required in order to manage it using CRUD
id
- The unique identifier of the object.created_dtm
- The unix epoch (UTC) representation of when the object was createdmodified_dtm
- The unix epoch (UTC) representation of when the object was last updatedinactive_dtm
- The unix epoch (UTC) representation of when the object was/will be considered obsoleteexpired_dtm
- The unix epoch (UTC) representation of when the object was/will be ready for deletionactivity
- The list of actions performed on the object
- There is common class behaviors that are required in order to manage it using CRUD
- The
id
is not optional. It must be either provided or automatically generated during instantiation. This can be done by calling theScaffolding
trait’sid()
method - The
created_dtm
is not optional. It must be either provided or automatically generated during instantiation. This can be done by calling one of theScaffolding
trait’s many datetime related methods, (e.g.:now()
) - The
modified_dtm
is not optional. It must be either provided or automatically generated during instantiation or updates to the object. This can be done by calling one of theScaffolding
trait’s many datetime related methods, (e.g.:now()
) - The
inactive_dtm
is not optional. It must be either provided or automatically generated during instantiation or updates to the object. This can be done by calling one of theScaffolding
trait’s many datetime related methods, (e.g.:add_months()
in conjuctions withnow()
) - The
expire_dtm
is not optional. It must be either provided or automatically generated during instantiation or updates to the object. This can be done by calling one of theScaffolding
trait’s many datetime related methods, (e.g.:never()
) - The
activity
is required and by default is an empty list of activity
§Example
Add Scaffolding to a struct
and impl
::new()
using macros and defaults
extern crate scaffolding_core;
use scaffolding_core::*;
// (1) Define the structure - Required
#[scaffolding_struct]
#[derive(Debug, Clone, Deserialize, Serialize, Scaffolding)]
struct MyEntity {
a: bool,
b: String,
}
impl MyEntity {
// (2) Define the constructor - Optional
// Note: Any of the Scaffodling attributes that are set here
// will not be overwritten when generated. For example
// the `id` attribute, if uncommented, would be ignored.
#[scaffolding_fn]
fn new(arg: bool) -> Self {
let msg = format!("You said it is {}", arg);
Self {
// id: "my unique identitifer".to_string(),
a: arg,
b: msg
}
}
fn my_func(&self) -> String {
"my function".to_string()
}
}
let mut entity = MyEntity::new(true);
/* scaffolding attributes */
assert_eq!(entity.id.len(), "54324f57-9e6b-4142-b68d-1d4c86572d0a".len());
assert_eq!(entity.created_dtm, defaults::now());
assert_eq!(entity.modified_dtm, defaults::now());
// becomes inactive in 90 days
assert_eq!(entity.inactive_dtm, defaults::add_days(defaults::now(), 90));
// expires in 3 years
assert_eq!(entity.expired_dtm, defaults::add_years(defaults::now(), 3));
/* serialization */
let json_string = entity.serialize();
println!("{}", json_string);
/* use the activity log functionality */
// (1) Log an activity
entity.log_activity("cancelled".to_string(), "The customer has cancelled their service".to_string());
// (2) Get activities
assert_eq!(entity.get_activity("cancelled".to_string()).len(), 1);
// extended attributes
assert_eq!(entity.a, true);
assert_eq!(entity.b, "You said it is true");
// extended behavior
assert_eq!(entity.my_func(), "my function");
Modules§
- defaults
- The defaults module provides the methods for creating deafult values for the Scaffolding common attributes
- errors
Structs§
- Activity
Item - Supporting Classes
- Address
- BTree
Map - An ordered map based on a B-Tree.
- Countries
- Country
- Email
Address - Note
- Phone
Number
Traits§
- Scaffolding
- The core behavior of a Scaffolding object
- Scaffolding
Addresses - The addresses behavior of a Scaffolding object
- Scaffolding
Email Addresses - The email address behavior of a Scaffolding object
- Scaffolding
Notes - The notes behavior of a Scaffolding object
- Scaffolding
Phone Numbers - The phone number behavior of a Scaffolding object
- Scaffolding
Tags - The tagging behavior of a Scaffolding object
Attribute Macros§
- scaffolding_
fn - Modifies the following functions
- scaffolding_
struct - Modifying a struct