use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::drive::{DriveAuthorization, File};
use crate::termite::versioning::FileOrFiles;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Stimuli {
Start,
File,
Text,
Website,
X,
Linkedin,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GraphNodeWire {
pub id: String,
pub kind: Stimuli,
pub x: f64,
pub y: f64,
#[serde(default)]
pub label: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub w: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub h: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub text: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub mime: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub file_path: Option<String>,
}
fn wire(
id: &str,
kind: Stimuli,
x: f64,
y: f64,
label: String,
w: Option<f64>,
h: Option<f64>,
) -> GraphNodeWire {
GraphNodeWire {
id: id.to_string(),
kind,
x,
y,
label,
w,
h,
text: None,
url: None,
mime: None,
file_path: None,
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ExtraPath {
pub path: String,
pub mime: String,
pub name: String,
}
#[derive(Default, Deserialize)]
#[serde(rename_all = "camelCase")]
struct XEnvelope {
body: Option<String>,
#[serde(default, deserialize_with = "present")]
quoted_id: Option<Option<String>>,
extra: Option<Vec<ExtraPath>>,
}
fn present<'de, D: serde::Deserializer<'de>>(
deserializer: D,
) -> std::result::Result<Option<Option<String>>, D::Error> {
Ok(Some(Option::<String>::deserialize(deserializer)?))
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct XEnvelopeOut<'a> {
body: &'a str,
quoted_id: Option<&'a str>,
#[serde(skip_serializing_if = "Vec::is_empty")]
extra: Vec<ExtraPath>,
}
fn parse_x_envelope(text: Option<&str>) -> XEnvelope {
let raw = text.unwrap_or_default().trim();
if raw.is_empty() {
return XEnvelope::default();
}
if raw.starts_with('{')
&& let Ok(Value::Object(fields)) = serde_json::from_str::<Value>(raw)
&& let Ok(envelope) = serde_json::from_value(Value::Object(fields))
{
return envelope;
}
XEnvelope {
body: Some(raw.to_string()),
..XEnvelope::default()
}
}
fn file_path_of(file: Option<&File>, fallback: Option<&String>) -> Option<String> {
match file.map(|file| &file.authorization) {
Some(DriveAuthorization::Path { path, .. })
| Some(DriveAuthorization::Repository { path, .. }) => Some(path.clone()),
_ => fallback.cloned(),
}
}
fn file_list(files: Option<FileOrFiles>) -> Vec<File> {
match files {
None => Vec::new(),
Some(FileOrFiles::File(file)) => vec![file],
Some(FileOrFiles::Files(files)) => files,
}
}
#[derive(Debug, Clone)]
#[allow(clippy::large_enum_variant)]
pub enum GraphNode {
Start(StartNode),
Text(TextNode),
Website(WebsiteNode),
File(FileNode),
X(XNode),
LinkedIn(LinkedInNode),
}
impl GraphNode {
pub fn kind(&self) -> Stimuli {
match self {
GraphNode::Start(_) => Stimuli::Start,
GraphNode::Text(_) => Stimuli::Text,
GraphNode::Website(_) => Stimuli::Website,
GraphNode::File(_) => Stimuli::File,
GraphNode::X(_) => Stimuli::X,
GraphNode::LinkedIn(_) => Stimuli::Linkedin,
}
}
pub fn id(&self) -> &str {
match self {
GraphNode::Start(node) => &node.id,
GraphNode::Text(node) => &node.id,
GraphNode::Website(node) => &node.id,
GraphNode::File(node) => &node.id,
GraphNode::X(node) => &node.id,
GraphNode::LinkedIn(node) => &node.id,
}
}
#[allow(clippy::should_implement_trait)]
pub fn clone(&self) -> GraphNode {
Clone::clone(self)
}
pub fn to_json(&self) -> GraphNodeWire {
match self {
GraphNode::Start(node) => node.to_json(),
GraphNode::Text(node) => node.to_json(),
GraphNode::Website(node) => node.to_json(),
GraphNode::File(node) => node.to_json(),
GraphNode::X(node) => node.to_json(),
GraphNode::LinkedIn(node) => node.to_json(),
}
}
pub fn from_json(json: &GraphNodeWire, files: Option<FileOrFiles>) -> GraphNode {
match json.kind {
Stimuli::Start => {
GraphNode::Start(StartNode::new(&json.id, json.x, json.y, json.w, json.h))
}
Stimuli::Text => GraphNode::Text(TextNode::new(
&json.id,
json.x,
json.y,
&json.label,
json.text.as_deref().unwrap_or_default(),
json.w,
json.h,
)),
Stimuli::Website => GraphNode::Website(WebsiteNode::new(
&json.id,
json.x,
json.y,
json.url.as_deref().unwrap_or_default(),
json.w,
json.h,
)),
Stimuli::File => {
let mut node = FileNode::new(
&json.id,
json.x,
json.y,
file_list(files).into_iter().next(),
);
node.w = json.w;
node.h = json.h;
node.wire_path = json.file_path.clone();
node.wire_mime = json.mime.clone();
node.wire_name = Some(json.label.clone());
GraphNode::File(node)
}
Stimuli::X => GraphNode::X(XNode::from_json(json, files)),
Stimuli::Linkedin => GraphNode::LinkedIn(LinkedInNode::from_json(json, files)),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct StartNode {
pub id: String,
pub x: f64,
pub y: f64,
pub w: Option<f64>,
pub h: Option<f64>,
}
impl StartNode {
pub fn new(id: &str, x: f64, y: f64, w: Option<f64>, h: Option<f64>) -> StartNode {
StartNode {
id: id.to_string(),
x,
y,
w,
h,
}
}
#[allow(clippy::should_implement_trait)]
pub fn clone(&self) -> StartNode {
Clone::clone(self)
}
pub fn to_json(&self) -> GraphNodeWire {
wire(
&self.id,
Stimuli::Start,
self.x,
self.y,
"Start".to_string(),
self.w,
self.h,
)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct TextNode {
pub id: String,
pub x: f64,
pub y: f64,
pub w: Option<f64>,
pub h: Option<f64>,
pub label: String,
pub text: String,
}
impl TextNode {
pub fn new(
id: &str,
x: f64,
y: f64,
label: &str,
text: &str,
w: Option<f64>,
h: Option<f64>,
) -> TextNode {
TextNode {
id: id.to_string(),
x,
y,
w,
h,
label: label.to_string(),
text: text.to_string(),
}
}
#[allow(clippy::should_implement_trait)]
pub fn clone(&self) -> TextNode {
Clone::clone(self)
}
pub fn to_json(&self) -> GraphNodeWire {
GraphNodeWire {
text: Some(self.text.clone()),
..wire(
&self.id,
Stimuli::Text,
self.x,
self.y,
self.label.clone(),
self.w,
self.h,
)
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct WebsiteNode {
pub id: String,
pub x: f64,
pub y: f64,
pub w: Option<f64>,
pub h: Option<f64>,
pub url: String,
}
impl WebsiteNode {
pub fn new(id: &str, x: f64, y: f64, url: &str, w: Option<f64>, h: Option<f64>) -> WebsiteNode {
WebsiteNode {
id: id.to_string(),
x,
y,
w,
h,
url: url.to_string(),
}
}
#[allow(clippy::should_implement_trait)]
pub fn clone(&self) -> WebsiteNode {
Clone::clone(self)
}
pub fn to_json(&self) -> GraphNodeWire {
let label = if self.url.is_empty() {
"Website".to_string()
} else {
self.url.clone()
};
GraphNodeWire {
url: Some(self.url.clone()),
..wire(
&self.id,
Stimuli::Website,
self.x,
self.y,
label,
self.w,
self.h,
)
}
}
}
#[derive(Debug, Clone)]
pub struct FileNode {
pub id: String,
pub x: f64,
pub y: f64,
pub w: Option<f64>,
pub h: Option<f64>,
pub file: Option<File>,
pub wire_path: Option<String>,
pub wire_mime: Option<String>,
pub wire_name: Option<String>,
}
impl FileNode {
pub fn new(id: &str, x: f64, y: f64, file: Option<File>) -> FileNode {
FileNode {
id: id.to_string(),
x,
y,
w: None,
h: None,
file,
wire_path: None,
wire_mime: None,
wire_name: None,
}
}
#[allow(clippy::should_implement_trait)]
pub fn clone(&self) -> FileNode {
Clone::clone(self)
}
pub fn to_json(&self) -> GraphNodeWire {
let label = self
.file
.as_ref()
.map(|file| file.name().to_string())
.or_else(|| self.wire_name.clone())
.unwrap_or_else(|| "File".to_string());
GraphNodeWire {
mime: self
.file
.as_ref()
.map(|file| file.content_type.clone())
.or_else(|| self.wire_mime.clone()),
file_path: file_path_of(self.file.as_ref(), self.wire_path.as_ref()),
..wire(
&self.id,
Stimuli::File,
self.x,
self.y,
label,
self.w,
self.h,
)
}
}
}
#[derive(Debug, Clone)]
pub struct XNode {
pub id: String,
pub x: f64,
pub y: f64,
pub w: Option<f64>,
pub h: Option<f64>,
pub handle: String,
pub body: String,
pub media: Vec<File>,
pub quoted_id: Option<String>,
pub extra_paths: Vec<ExtraPath>,
pub wire_path: Option<String>,
pub wire_mime: Option<String>,
}
impl XNode {
pub fn new(id: &str, x: f64, y: f64, handle: &str, body: &str) -> XNode {
XNode {
id: id.to_string(),
x,
y,
w: None,
h: None,
handle: handle.to_string(),
body: body.to_string(),
media: Vec::new(),
quoted_id: None,
extra_paths: Vec::new(),
wire_path: None,
wire_mime: None,
}
}
pub fn is_post(&self) -> bool {
self.quoted_id.is_none()
}
pub fn is_repost(&self) -> bool {
self.quoted_id.is_some() && self.body.trim().is_empty()
}
pub fn is_quote(&self) -> bool {
self.quoted_id.is_some() && !self.body.trim().is_empty()
}
#[allow(clippy::should_implement_trait)]
pub fn clone(&self) -> XNode {
Clone::clone(self)
}
pub fn to_json(&self) -> GraphNodeWire {
let first = self.media.first();
let mut extra: Vec<ExtraPath> = self
.media
.iter()
.skip(1)
.map(|file| ExtraPath {
path: file_path_of(Some(file), None).unwrap_or_default(),
mime: file.content_type.clone(),
name: file.name().to_string(),
})
.filter(|row| !row.path.is_empty())
.collect();
extra.extend(self.extra_paths.iter().cloned());
let text = serde_json::to_string(&XEnvelopeOut {
body: &self.body,
quoted_id: self.quoted_id.as_deref(),
extra,
})
.unwrap_or_default();
GraphNodeWire {
text: Some(text),
mime: first
.map(|file| file.content_type.clone())
.or_else(|| self.wire_mime.clone()),
file_path: file_path_of(first, self.wire_path.as_ref()),
..wire(
&self.id,
Stimuli::X,
self.x,
self.y,
self.handle.clone(),
self.w,
self.h,
)
}
}
pub fn from_json(json: &GraphNodeWire, files: Option<FileOrFiles>) -> XNode {
let envelope = parse_x_envelope(json.text.as_deref());
let handle = if json.label.is_empty() {
json.url.clone().unwrap_or_default()
} else {
json.label.clone()
};
XNode {
w: json.w,
h: json.h,
media: file_list(files),
quoted_id: envelope.quoted_id.flatten(),
extra_paths: envelope.extra.unwrap_or_default(),
wire_path: json.file_path.clone(),
wire_mime: json.mime.clone(),
..XNode::new(
&json.id,
json.x,
json.y,
&handle,
&envelope.body.unwrap_or_default(),
)
}
}
}
#[derive(Debug, Clone)]
pub struct LinkedInNode {
pub id: String,
pub x: f64,
pub y: f64,
pub w: Option<f64>,
pub h: Option<f64>,
pub author: String,
pub body: String,
pub media: Vec<File>,
pub wire_path: Option<String>,
pub wire_mime: Option<String>,
}
impl LinkedInNode {
pub fn new(id: &str, x: f64, y: f64, author: &str, body: &str) -> LinkedInNode {
LinkedInNode {
id: id.to_string(),
x,
y,
w: None,
h: None,
author: author.to_string(),
body: body.to_string(),
media: Vec::new(),
wire_path: None,
wire_mime: None,
}
}
#[allow(clippy::should_implement_trait)]
pub fn clone(&self) -> LinkedInNode {
Clone::clone(self)
}
pub fn to_json(&self) -> GraphNodeWire {
let first = self.media.first();
GraphNodeWire {
text: Some(self.body.clone()),
mime: first
.map(|file| file.content_type.clone())
.or_else(|| self.wire_mime.clone()),
file_path: file_path_of(first, self.wire_path.as_ref()),
..wire(
&self.id,
Stimuli::Linkedin,
self.x,
self.y,
self.author.clone(),
self.w,
self.h,
)
}
}
pub fn from_json(json: &GraphNodeWire, files: Option<FileOrFiles>) -> LinkedInNode {
LinkedInNode {
w: json.w,
h: json.h,
media: file_list(files),
wire_path: json.file_path.clone(),
wire_mime: json.mime.clone(),
..LinkedInNode::new(
&json.id,
json.x,
json.y,
&json.label,
json.text.as_deref().unwrap_or_default(),
)
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GraphEdge {
pub id: String,
pub from_node_id: String,
pub to_node_id: String,
}
impl GraphEdge {
pub fn new(id: &str, from_node_id: &str, to_node_id: &str) -> GraphEdge {
GraphEdge {
id: id.to_string(),
from_node_id: from_node_id.to_string(),
to_node_id: to_node_id.to_string(),
}
}
#[allow(clippy::should_implement_trait)]
pub fn clone(&self) -> GraphEdge {
Clone::clone(self)
}
pub fn to_json(&self) -> Value {
serde_json::to_value(self).unwrap_or_default()
}
pub fn from_json(json: &Value) -> GraphEdge {
serde_json::from_value(json.clone()).unwrap_or_else(|_| GraphEdge::new("", "", ""))
}
}