Skip to content

Commit

Permalink
refactor: inline map
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 14, 2022
1 parent 39e5df0 commit 927ce06
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
4 changes: 2 additions & 2 deletions examples/entries-define.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ entries:
actions: ~
- type: graph
display: "Knowledge Graph"
target: "quake-board"
component: "quake-board"
properties:
- title: Title
- file: File
- source: Source
- content: String
- created_date: Date
- updated_date: Date
26 changes: 13 additions & 13 deletions quake_core/src/entry/entry_define.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::entry::PropMap;
use indexmap::IndexMap;
use serde_derive::{Deserialize, Serialize};

Expand All @@ -7,11 +8,13 @@ use crate::meta::{EntryDefineProperties, MetaProperty};
/// Define a new entry:
/// - `entry_type`: the entry_type for operation in system, should be in letter or `_` use in `dir`, `storage` such as
/// - `display`: the name for display
/// - `custom_path`: custom entry path
/// - `properties`: in yaml is a key-value list, need to be convert to HashMap
/// - `processors`: define processors for files
/// - `actions`: custom behavior action, can be use as API #TBD
/// - `flows`: for a simple workflow like **kanban**
/// - `states`: such as **filterable** condition
/// - `generate_rules`: for auto generate content rule
/// - `component`: display from source
///
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Default)]
pub struct EntryDefine {
Expand All @@ -21,7 +24,7 @@ pub struct EntryDefine {
/// custom path for entries
#[serde(skip_serializing_if = "Option::is_none")]
pub custom_path: Option<String>,
pub properties: Vec<IndexMap<String, String>>,
pub properties: Vec<PropMap>,
#[serde(skip_serializing_if = "Option::is_none")]
pub actions: Option<Vec<String>>,
/// file processors
Expand All @@ -32,7 +35,7 @@ pub struct EntryDefine {
#[serde(skip_serializing_if = "Option::is_none")]
pub states: Option<Vec<EntryState>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub target: Option<String>,
pub component: Option<String>,
}

/// process file for file content
Expand All @@ -59,7 +62,7 @@ pub struct EntryState {
impl EntryDefine {
// todo: directly Deserialize to meta field
pub fn to_field_type(&self) -> IndexMap<String, MetaProperty> {
let mut properties: IndexMap<String, String> = IndexMap::new();
let mut properties: PropMap = IndexMap::new();
for map in &self.properties {
for (key, value) in map {
properties.insert(key.to_string(), value.to_string());
Expand All @@ -70,8 +73,8 @@ impl EntryDefine {
}

/// set default flow value from first values
pub fn create_flows_and_states(&self) -> IndexMap<String, String> {
let mut map: IndexMap<String, String> = IndexMap::new();
pub fn create_flows_and_states(&self) -> PropMap {
let mut map: PropMap = IndexMap::new();

if let Some(list) = &self.flows {
for flow in list {
Expand All @@ -89,7 +92,7 @@ impl EntryDefine {
}

/// add `title`, `created_date`, `updated_date` value to system
pub fn create_title_and_date(&self, title: &str) -> IndexMap<String, String> {
pub fn create_title_and_date(&self, title: &str) -> PropMap {
let date = quake_time::date_now();

let mut map = IndexMap::new();
Expand All @@ -101,7 +104,7 @@ impl EntryDefine {
}

/// create default properties with title, date, flows, dates
pub fn create_default_properties(&self, title: &str) -> IndexMap<String, String> {
pub fn create_default_properties(&self, title: &str) -> PropMap {
let basic_map = self.create_title_and_date(title);
let mut properties = self.convert_to_properties(basic_map);
let flows = self.create_flows_and_states();
Expand All @@ -111,11 +114,8 @@ impl EntryDefine {
}

/// merge custom yaml map list to Indexmap
pub fn convert_to_properties(
&self,
values: IndexMap<String, String>,
) -> IndexMap<String, String> {
let mut result: IndexMap<String, String> = IndexMap::new();
pub fn convert_to_properties(&self, values: PropMap) -> PropMap {
let mut result: PropMap = IndexMap::new();

for field_def in &self.properties {
for (key, _field_type) in field_def {
Expand Down
7 changes: 4 additions & 3 deletions quake_core/src/entry/entry_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize, Serializer};
use serde_yaml::{Sequence, Value};

use crate::entry::slug::slugify;
use crate::entry::PropMap;
use crate::errors::QuakeError;
use crate::helper::date_now;
use crate::meta::quake_change::QuakeChange;
Expand All @@ -23,7 +24,7 @@ pub struct EntryFile {
#[serde(skip_serializing_if = "Option::is_none")]
pub path: Option<PathBuf>,
pub name: String,
pub properties: IndexMap<String, String>,
pub properties: PropMap,
pub content: String,
pub changes: Vec<QuakeChange>,
}
Expand Down Expand Up @@ -134,7 +135,7 @@ impl EntryFile {

let (front_matter, content) = Self::split_markdown(text);

let mut fields: IndexMap<String, String> = IndexMap::new();
let mut fields: PropMap = IndexMap::new();
let mut changes = vec![];
for document in serde_yaml::Deserializer::from_str(&front_matter) {
let value = match Value::deserialize(document) {
Expand Down Expand Up @@ -228,7 +229,7 @@ impl EntryFile {
self.properties.insert(key.to_string(), value);
}

pub fn set_properties(&mut self, fields: IndexMap<String, String>) {
pub fn set_properties(&mut self, fields: PropMap) {
self.properties = fields;
}

Expand Down
4 changes: 4 additions & 0 deletions quake_core/src/entry/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use indexmap::IndexMap;

pub use entry_define::EntryDefine;
pub use entry_defines::EntryDefines;
pub use entry_node_info::EntryNodeInfo;
Expand All @@ -9,3 +11,5 @@ pub mod entry_file;
pub mod entry_node_info;
pub mod entry_paths;
pub mod slug;

pub type PropMap = IndexMap<String, String>;

0 comments on commit 927ce06

Please sign in to comment.