#面向对象 # #设计 #SDK #derive-debug #开发

框架核心

一个软件开发套件,提供使用面向对象(OOP)构建应用程序和服务的框架。

13 个版本 (2 个稳定版)

2.0.0 2024 年 6 月 30 日
1.0.0 2024 年 6 月 21 日
0.8.0 2024 年 6 月 19 日
0.4.0 2024 年 3 月 31 日

Rust 模式 中排名 #246

Download history 6/week @ 2024-04-07 1/week @ 2024-05-26 130/week @ 2024-06-02 313/week @ 2024-06-09 329/week @ 2024-06-16 29/week @ 2024-06-23 188/week @ 2024-06-30 32/week @ 2024-07-07 53/week @ 2024-07-14 6/week @ 2024-07-21

每月下载量 285

Apache-2.0

76KB
503 代码行

License Docs.rs Build/Test Discussions

框架核心

对于欣赏面向对象开发启动器的软件开发团队,这个框架库是一个轻量级的模块,它提供了所有类实例对象的构建块的基本结构和行为。与使用各种方法编写具有常见功能类的实践不同,这个开源库帮助您继承这些跨类通用性,以便您可以专注于定义您的类的不同之处。


目录


新增功能

我们使 crate 更易于实现,并更新了文档和 crate 元数据。

2.0.0

用法

(1) 导入必要的模块

extern crate scaffolding_core;

use scaffolding_core::*;

(2) 向结构体添加 Scaffolding 属性并将特性和属性应用于结构体

#[scaffolding_struct]
#[derive(Debug, Clone, Deserialize, Serialize, Scaffolding)]
struct MyEntity {
    a: bool,
    b: String,
}

(3) 将 Scaffodling 属性的默认值作为 ::new() 构造函数的一部分动态添加 注意: 如果您希望在其他地方使用它,则可用的 scaffolding_core::defaults 模块

impl MyEntity {
    // Define the constructor - Optional
    // Note: Any of the Scaffolding 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()
    }
}

(4) 使用 Scaffolding 属性和行为

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));

/* 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);

/* custom attributes */
assert_eq!(entity.a, true);
assert_eq!(entity.b, "You said it is true");

/* custom behavior */
assert_eq!(entity.my_func(), "my function");

序列化

let json_string = entity.serialize();
println!("{}", json_string);

反序列化

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 entity = MyEntity::deserialized(json.as_bytes()).unwrap();

assert_eq!(entity.get_activity("cancelled".to_string()).len(), 1);

还有其他可以应用的 Scaffolding 功能。

地址

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

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

let mut entity = MyEntity::new();

/* use the addresses functionality */
// (1) Add an address
let id = entity.add_address(
    "shipping".to_string(),
    "acmes company".to_string(),
    "14 Main Street".to_string(),
    "Big City, NY 038845".to_string(),
    "USA".to_string(),
    "USA".to_string(),
);
// (2) Find addresses based on the category
let shipping_addresses = entity.addresses_by_category("shipping".to_string());
// (3) Remove an address
entity.remove_address(id);

电子邮件地址

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

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

let mut entity = MyEntity::new();

/* use the email addresses functionality */
// (1) Add an email address
let id = entity.insert_email_address(
    "home".to_string(),
    "[email protected]".to_string(),
);
// (2) Find email addresses based on the category
let home_email_addresses = entity.search_email_addresses_by_category("home".to_string());
// (3) Remove an address
entity.remove_address(id);

元数据

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

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

let mut entity = MyEntity::new();

/* use the metadata functionality
   Note: `memtadata` is a BTreeMap<String, String>
          https://doc.rust-lang.net.cn/std/collections/struct.BTreeMap.html
*/
entity.metadata.insert("field_1".to_string(), "myvalue".to_string());
assert_eq!(entity.metadata.len(), 1);

注释

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

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

let mut entity = MyEntity::new();

// (1) Insert a note
let id = entity.insert_note(
  "fsmith".to_string(),
  "This was updated".as_bytes().to_vec(),
  None,
);
// (2) Modify the note
entity.modify_note(
  id.clone(),
  "fsmith".to_string(),
  "This was updated again".as_bytes().to_vec(),
  Some("private".to_string()),
);
// (3) Read the note's content
let read_note = entity.get_note(id.clone()).unwrap().content_as_string().unwrap();
println!("{}", read_note);
// (4) Search for notes that contain the word `updated`
let search_results = entity.search_notes("updated".to_string());
assert_eq!(search_results.len(), 1);
// (5) Delete the note
entity.remove_note(id);

电话号码

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

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

let mut entity = MyEntity::new();

/* use the phone number functionality */
// (1) Add a phone number
let _ = entity.add_phone_number(
    "home".to_string(),
    "8482493561".to_string(),
    "USA".to_string(),
);
let id = entity.add_phone_number(
    "work".to_string(),
    "2223330000".to_string(),
    "USA".to_string(),
);
// (2) Find phone number based on the category
let home_phone = entity.phone_numbers_by_category("home".to_string());
// (3) Remove an address
entity.remove_phone_number(id);

标签

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

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

let mut entity = MyEntity::new();

// manage tags
entity.add_tag("tag_1".to_string());
entity.add_tag("tag_2".to_string());
entity.add_tag("tag_3".to_string());
assert!(entity.has_tag("tag_1".to_string()));
entity.remove_tag("tag_2".to_string());
assert_eq!(entity.tags.len(), 2);

如何贡献

有关如何贡献的详细信息,请参阅 CONTRIBUTING 文件。

许可

《scaffolding-core》项目主要遵循Apache许可证(版本2.0)的条款进行分发。

有关详细信息,请参阅"Apache许可证"

依赖项

~4–6MB
~111K SLoC