Trait scaffolding_core::Scaffolding

source ·
pub trait Scaffolding {
    // Required methods
    fn log_activity(&mut self, name: String, descr: String);
    fn get_activity(&self, name: String) -> Vec<ActivityItem>;

    // Provided methods
    fn deserialized<T: DeserializeOwned>(
        serialized: &[u8],
    ) -> Result<T, DeserializeError> { ... }
    fn serialize(&mut self) -> String
       where Self: Serialize { ... }
}
Expand description

The core behavior of a Scaffolding object

Required Methods§

source

fn log_activity(&mut self, name: String, descr: String)

This function adds a ActivityItem to the activity log

#Example

#[macro_use]
     
use scaffolding_core::{defaults, ActivityItem, Scaffolding};
use scaffolding_macros::*;

#[scaffolding_struct]
#[derive(Clone, Debug, Scaffolding)]
struct MyEntity {}

impl MyEntity {
    #[scaffolding_fn]
    fn new() -> Self {
        Self {}
    }
}

let mut entity = MyEntity::new();

entity.log_activity("cancelled".to_string(), "The customer has cancelled their service".to_string());
assert_eq!(entity.activity.len(), 1);
source

fn get_activity(&self, name: String) -> Vec<ActivityItem>

This function retrieves all the ActivityItems that have the specified action (name)

#Example

#[macro_use]
     
use scaffolding_core::{defaults, ActivityItem, Scaffolding};
use scaffolding_macros::*;

#[scaffolding_struct]
#[derive(Clone, Debug, Scaffolding)]
struct MyEntity {}

impl MyEntity {
    #[scaffolding_fn]
    fn new() -> Self {
        Self {}
    }
}

let mut entity = MyEntity::new();

entity.log_activity("ordered".to_string(), "The customer has place the order".to_string());
entity.log_activity("cancelled".to_string(), "The customer has cancelled their service".to_string());
assert_eq!(entity.get_activity("cancelled".to_string()).len(), 1);

Provided Methods§

source

fn deserialized<T: DeserializeOwned>( serialized: &[u8], ) -> Result<T, DeserializeError>

This function instantiates an entity from a JSON string.

#Example

#[macro_use]
     
use scaffolding_core::{defaults, ActivityItem, Scaffolding};
use scaffolding_macros::*;
use serde_derive::Deserialize;

#[scaffolding_struct]
#[derive(Clone, Debug, Deserialize, Scaffolding)]
struct MyEntity {}

impl MyEntity {
    #[scaffolding_fn]
    fn new() -> Self {
        Self {}
    }
}

let json = r#"{
    "id":"b4d6c6db-7468-400a-8536-a5e83b1f2bdc",
    "created_dtm":1711802687,
    "modified_dtm":1711802687,
    "inactive_dtm":1719578687,
    "expired_dtm":1806410687,
    "activity":[
        {
            "created_dtm":1711802687,
            "action":"updated",
            "description":"The object has been updated"
        },
        {
            "created_dtm":1711802687,
            "action":"updated",
            "description":"The object has been updated"
        },
        {
            "created_dtm":1711802687,
            "action":"cancelled",
            "description":"The object has been cancelled"
        }
        ]
    }"#;
let deserialized = MyEntity::deserialized::<MyEntity>(json.as_bytes()).unwrap();

assert_eq!(deserialized.id, "b4d6c6db-7468-400a-8536-a5e83b1f2bdc");
assert_eq!(deserialized.activity.len(), 3);  
source

fn serialize(&mut self) -> String
where Self: Serialize,

This function converts the entity to a serialize JSON string.

#Example

#[macro_use]
     
use scaffolding_core::{defaults, ActivityItem, Scaffolding};
use scaffolding_macros::*;
use serde_derive::Serialize;

#[scaffolding_struct]
#[derive(Clone, Debug, Serialize, Scaffolding)]
struct MyEntity {}

impl MyEntity {
    #[scaffolding_fn]
    fn new() -> Self {
        Self {}
    }
}

let mut entity = MyEntity::new();
let json_string = entity.serialize();

println!("{}", json_string);

Object Safety§

This trait is not object safe.

Implementors§