about summary refs log tree commit diff stats
path: root/pkgs/by-name/ts/tskm/src/interface/project/mod.rs
blob: 8a7fa1b05ef595fde208a298d4be8e392934f009 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// nixos-config - My current NixOS configuration
//
// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of my nixos-config.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.

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