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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use crate::minecraft::models::{version_summary::VersionSummary, version_type::VersionType};
use gio::subclass::prelude::*;
use glib::{ObjectExt, ParamFlags, ParamSpec, ParamSpecInt64, ParamSpecString, ToValue};
use once_cell::sync::{Lazy, OnceCell};
use std::cell::Cell;
use time::OffsetDateTime;

pub const ID: &str = "id";
pub const TYPE: &str = "type";
pub const URL: &str = "url";
pub const TIME: &str = "time";
pub const RELEASE_TIME: &str = "release-time";

mod imp {
    use super::*;

    #[derive(Debug, Default)]
    pub struct GVersionSummary {
        pub id: OnceCell<String>,
        pub _type: OnceCell<String>,
        pub url: OnceCell<String>,
        pub time: Cell<i64>,
        pub release_time: Cell<i64>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for GVersionSummary {
        const NAME: &'static str = "GVersionSummary";
        type Type = super::GVersionSummary;
        type ParentType = glib::Object;
    }

    impl ObjectImpl for GVersionSummary {
        fn properties() -> &'static [glib::ParamSpec] {
            static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
                vec![
                    ParamSpecString::new(
                        ID,
                        "ID",
                        "ID",
                        None,
                        ParamFlags::READWRITE | ParamFlags::CONSTRUCT_ONLY,
                    ),
                    ParamSpecString::new(
                        TYPE,
                        "Type",
                        "Type",
                        None,
                        ParamFlags::READWRITE | ParamFlags::CONSTRUCT_ONLY,
                    ),
                    ParamSpecString::new(
                        URL,
                        "URL",
                        "URL",
                        None,
                        ParamFlags::READWRITE | ParamFlags::CONSTRUCT_ONLY,
                    ),
                    ParamSpecInt64::new(
                        TIME,
                        "Time",
                        "Time",
                        i64::MIN,
                        i64::MAX,
                        0,
                        ParamFlags::READWRITE | ParamFlags::CONSTRUCT_ONLY,
                    ),
                    ParamSpecInt64::new(
                        RELEASE_TIME,
                        "Release Time",
                        "Release Time",
                        i64::MIN,
                        i64::MAX,
                        0,
                        ParamFlags::READWRITE | ParamFlags::CONSTRUCT_ONLY,
                    ),
                ]
            });

            PROPERTIES.as_ref()
        }

        fn set_property(
            &self,
            _obj: &Self::Type,
            _id: usize,
            value: &glib::Value,
            pspec: &glib::ParamSpec,
        ) {
            match pspec.name() {
                ID => self.id.set(value.get().unwrap()).unwrap(),
                TYPE => self._type.set(value.get().unwrap()).unwrap(),
                URL => self.url.set(value.get().unwrap()).unwrap(),
                TIME => self.time.set(value.get().unwrap()),
                RELEASE_TIME => self.release_time.set(value.get().unwrap()),
                prop => unimplemented!("Property {prop} not a member of GVersionSummary"),
            }
        }

        fn property(&self, _obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
            match pspec.name() {
                ID => self.id.get().to_value(),
                TYPE => self._type.get().to_value(),
                URL => self.url.get().to_value(),
                TIME => self.time.get().to_value(),
                RELEASE_TIME => self.release_time.get().to_value(),
                prop => unimplemented!("Property {prop} not a member of GVersionSummary"),
            }
        }
    }
}

glib::wrapper! {
    /// Glib object for [`VersionSummary`](crate::minecraft::models::version_summary::VersionSummary).
    pub struct GVersionSummary(ObjectSubclass<imp::GVersionSummary>);
}

impl GVersionSummary {
    /// Get `id`.
    pub fn id(&self) -> String {
        self.property(ID)
    }

    /// Get `type`.
    pub fn _type(&self) -> VersionType {
        let _type: String = self.property(TYPE);

        match _type.as_str() {
            "Release" => VersionType::Release,
            "Snapshot" => VersionType::Snapshot,
            "Alpha" => VersionType::OldAlpha,
            "Beta" => VersionType::OldBeta,
            _ => panic!("Invalid release type found in GObject"),
        }
    }

    /// Get `url`.
    pub fn url(&self) -> String {
        self.property(URL)
    }

    /// Get `time`.
    pub fn time(&self) -> OffsetDateTime {
        let time: i64 = self.property(TIME);
        OffsetDateTime::from_unix_timestamp(time).unwrap()
    }

    /// Get `release_time`.
    pub fn release_time(&self) -> OffsetDateTime {
        let release_time: i64 = self.property(RELEASE_TIME);
        OffsetDateTime::from_unix_timestamp(release_time).unwrap()
    }
}

impl From<VersionSummary> for GVersionSummary {
    fn from(summary: VersionSummary) -> Self {
        glib::Object::new(&[
            (ID, &summary.id),
            (TYPE, &summary._type.to_string()),
            (URL, &summary.url),
            (TIME, &summary.time.unix_timestamp()),
            (RELEASE_TIME, &summary.release_time.unix_timestamp()),
        ])
        .unwrap()
    }
}