use std::{collections::BTreeSet, fmt};
use schemars::JsonSchema;
use serde::Serialize;
use crate::item::flag::Flag;
#[derive(Clone, Debug, Serialize, JsonSchema)]
#[serde(
tag = "kind",
rename_all = "kebab-case",
rename_all_fields = "camelCase"
)]
#[allow(dead_code)]
pub enum CollectionHunk {
Create {
side: String,
collection: String,
},
Delete {
side: String,
collection: String,
},
Scan {
side: String,
collection: String,
},
}
impl fmt::Display for CollectionHunk {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Create { side, collection } => {
write!(f, "create collection {collection} on {side}")
}
Self::Delete { side, collection } => {
write!(f, "delete collection {collection} on {side}")
}
Self::Scan { side, collection } => {
write!(f, "scan collection {collection} on {side}")
}
}
}
}
#[derive(Clone, Debug, Serialize, JsonSchema)]
#[serde(
tag = "kind",
rename_all = "kebab-case",
rename_all_fields = "camelCase"
)]
pub enum ItemHunk {
Add {
side: String,
collection: String,
id: String,
flags: BTreeSet<Flag>,
#[serde(skip)]
content_key: u64,
},
Copy {
source_side: String,
target_side: String,
collection: String,
source_id: String,
flags: BTreeSet<Flag>,
#[serde(default, skip_serializing_if = "Option::is_none")]
origin: Option<String>,
#[serde(skip)]
content_key: u64,
},
AddFlags {
side: String,
collection: String,
id: String,
flags: BTreeSet<Flag>,
#[serde(skip)]
content_key: u64,
},
RemoveFlags {
side: String,
collection: String,
id: String,
flags: BTreeSet<Flag>,
#[serde(skip)]
content_key: u64,
},
Delete {
side: String,
collection: String,
id: String,
#[serde(skip)]
content_key: u64,
},
Fetch {
side: String,
collection: String,
id: String,
#[serde(skip)]
content_key: u64,
},
Update {
side: String,
collection: String,
id: String,
#[serde(skip)]
content_key: u64,
},
}
impl fmt::Display for ItemHunk {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Add {
side,
collection,
id,
..
} => write!(f, "add item {id} in {collection} on {side}"),
Self::Copy {
target_side,
collection,
source_id,
origin: Some(origin),
..
} => write!(
f,
"copy item {source_id} from {origin} to {collection} on {target_side}"
),
Self::Copy {
source_side,
target_side,
collection,
source_id,
..
} => write!(
f,
"copy item {source_id} in {collection} from {source_side} to {target_side}"
),
Self::AddFlags {
side,
collection,
id,
flags,
..
} => write!(
f,
"add {flags} to item {id} in {collection} on {side}",
flags = format_flag_list(flags),
),
Self::RemoveFlags {
side,
collection,
id,
flags,
..
} => write!(
f,
"remove {flags} from item {id} in {collection} on {side}",
flags = format_flag_list(flags),
),
Self::Delete {
side,
collection,
id,
..
} => {
write!(f, "delete item {id} in {collection} on {side}")
}
Self::Fetch {
side,
collection,
id,
..
} => {
write!(f, "fetch item {id} in {collection} from {side}")
}
Self::Update {
side,
collection,
id,
..
} => {
write!(f, "update item {id} in {collection} on {side}")
}
}
}
}
fn format_flag_list(flags: &BTreeSet<Flag>) -> String {
let mut out = String::from("[");
let mut first = true;
for flag in flags {
if !first {
out.push_str(", ");
}
first = false;
for ch in flag.raw().chars() {
for lower in ch.to_lowercase() {
out.push(lower);
}
}
}
out.push(']');
out
}