about summary refs log tree commit diff stats
path: root/pkgs/by-name/ts/tskm/src/interface/project/mod.rs
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-04-04 11:48:44 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-04-04 11:48:44 +0200
commit135d09bfb305d54cac1ba1fb9861d5b9309a7b3a (patch)
tree459109a40320530993ae560f55a730a72df31416 /pkgs/by-name/ts/tskm/src/interface/project/mod.rs
parentrefactor(modules/legacy/firefox): Move to by-name (diff)
downloadnixos-config-135d09bfb305d54cac1ba1fb9861d5b9309a7b3a.zip
feat(pkgs/neorg): Rewrite in rust
This improves upon neorg by integrating it better into the system
context.
Diffstat (limited to 'pkgs/by-name/ts/tskm/src/interface/project/mod.rs')
-rw-r--r--pkgs/by-name/ts/tskm/src/interface/project/mod.rs73
1 files changed, 73 insertions, 0 deletions
diff --git a/pkgs/by-name/ts/tskm/src/interface/project/mod.rs b/pkgs/by-name/ts/tskm/src/interface/project/mod.rs
new file mode 100644
index 00000000..62069746
--- /dev/null
+++ b/pkgs/by-name/ts/tskm/src/interface/project/mod.rs
@@ -0,0 +1,73 @@
+use std::collections::HashMap;
+
+use anyhow::Result;
+use serde::{Deserialize, Serialize};
+
+pub mod handle;
+pub use handle::handle;
+
+#[derive(Deserialize, Serialize)]
+struct ProjectList(HashMap<String, ProjectDefinition>);
+
+#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Default)]
+struct ProjectDefinition {
+    #[serde(default)]
+    #[serde(skip_serializing_if = "is_default")]
+    name: String,
+
+    #[serde(default)]
+    #[serde(skip_serializing_if = "is_default")]
+    prefix: String,
+
+    #[serde(default)]
+    #[serde(skip_serializing_if = "is_default")]
+    subprojects: HashMap<String, ProjectDefinition>,
+}
+
+fn is_default<T: Default + PartialEq>(input: &T) -> bool {
+    input == &T::default()
+}
+
+#[derive(Debug, Clone)]
+pub struct ProjectName {
+    project_segments: Vec<String>,
+}
+
+impl ProjectName {
+    #[must_use]
+    pub fn segments(&self) -> &[String] {
+        &self.project_segments
+    }
+
+    /// # Errors
+    /// Never.
+    pub fn try_from_project(s: &str) -> Result<Self> {
+        Ok(Self::from_project(s))
+    }
+    pub fn from_project(s: &str) -> Self {
+        let me = Self {
+            project_segments: s.split('.').map(ToOwned::to_owned).collect(),
+        };
+        me
+    }
+    pub fn from_context(s: &str) -> Self {
+        let me = Self {
+            project_segments: s.split('_').map(ToOwned::to_owned).collect(),
+        };
+        me
+    }
+}
+
+// Source: https://stackoverflow.com/a/67792465
+fn sort_alphabetically<T: Serialize, S: serde::Serializer>(
+    value: &T,
+    serializer: S,
+) -> Result<S::Ok, S::Error> {
+    let value = serde_json::to_value(value).map_err(serde::ser::Error::custom)?;
+    value.serialize(serializer)
+}
+
+#[derive(Serialize)]
+pub(super) struct SortAlphabetically<T: Serialize>(
+    #[serde(serialize_with = "sort_alphabetically")] T,
+);