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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#![crate_name = "syunit"]
#![doc = include_str!("../README.md")]
#![no_std]
#![deny(missing_docs)]

// Units
use core::ops::{Add, AddAssign, Div, Sub, SubAssign};
use core::time::Duration;

#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize}; 

// Submodules
    mod funcs;
    pub use funcs::*;

    mod macros;

    mod specials;
    pub use specials::*;
// 

// Helper import for local macro definitions
use crate as syunit;

/// General marker trait for all units
pub trait Unit : Into<f32> { 
    /// Creates a new value of this unit using a `f32` value
    fn new(v : f32) -> Self
    where 
        Self : Sized;
}

/// Represents a time
/// 
/// # Unit
/// 
/// - In seconds
/// 
/// ```rust
/// use syunit::*;
/// 
/// // Comparisions
/// assert!(Time(1.0) > Time(-1.0));
/// ```
#[derive(Clone, Copy, Default, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Time(pub f32);
basic_unit!(Time);
additive_unit!(Time);

impl Into<Duration> for Time {
    #[inline(always)]
    fn into(self) -> Duration {
        // Negative time fallback
        // if self.0.is_sign_negative() {
        //     self.0 = self.0.abs();
        // }

        Duration::from_secs_f32(self.0)
    }
}

impl Div<Time> for f32 {
    type Output = Velocity;

    #[inline(always)]
    fn div(self, rhs: Time) -> Self::Output {
        Velocity(self / rhs.0)
    }
}

/// Represents a certain factor between 0 and 1
/// 
/// # Unit
/// 
/// - Unitless
/// 
#[derive(Clone, Copy, Default, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Factor(f32);

impl Factor {
    // Constants
        /// Maximum
        pub const MAX : Self = Self(1.0);

        /// Half
        pub const HALF : Self = Self(0.5);

        /// Minium
        pub const MIN : Self = Self(0.0);
    // 

    /// Creates a new `Factor`
    /// 
    /// # Panics
    /// 
    /// Panics if the given 'val' is out of bounds (0 <= val <= 1)
    pub fn new(val : f32) -> Self {
        if (val >= 0.0) & (val <= 1.0) {
            Self(val)
        } else {
            panic!("The given value is out of bounds for a Factor! ({})", val);
        }
    }

    /// Tries to create a new factor, will be `None` if `val` is not between or equal to 0 and 1
    pub fn try_new(val : f32) -> Option<Self> {
        if (val >= 0.0) & (val <= 1.0) {
            Some(Self(val))
        } else {
            None
        }
    }

    /// Creates a new factor without checking bounds
    /// 
    /// # Unsafe
    /// 
    /// An out of bounds factor might throw up important logic
    /// 
    /// Should only be used for creating literals
    pub const unsafe fn new_unchecked(val : f32) -> Self {
        Self(val)
    }

    /// embedded_hal helper function
    pub fn get_duty(self) -> u16 {
        ((u16::MAX as f32) * self.0) as u16 
    }

    /// embedded_hal helper function
    pub fn get_duty_for(self, max_duty : u16) -> u16 {
        ((max_duty as f32) * self.0) as u16 
    }
}

impl core::ops::Mul<Factor> for Factor {
    type Output = Factor;

    fn mul(self, rhs: Factor) -> Self::Output {
        Self::new(self.0 * rhs.0)
    }
}

impl core::ops::Mul<Factor> for Velocity {
    type Output = Velocity;

    fn mul(self, rhs: Factor) -> Self::Output {
        Self::new(self.0 * rhs.0)
    }
}

impl core::fmt::Display for Factor {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        self.0.fmt(f)
    }
}

impl core::fmt::Debug for Factor {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_fmt(format_args!("Factor({})", self.0))
    }
}

/// The gamma distance represents the actual distance traveled by the component
/// 
/// # Unit
/// 
/// - Can be either radians or millimeters
/// 
/// # Operations
/// 
/// ```rust
/// use syunit::{Gamma, Delta};
/// 
/// // Subtract two absolute distances to get once relative
/// assert_eq!(Gamma(2.0) - Gamma(1.0), Delta(1.0));
/// 
/// // Add relative distance to an absolute one
/// assert_eq!(Gamma(2.0) + Delta(1.0), Gamma(3.0));
/// assert_eq!(Gamma(2.0) - Delta(1.0), Gamma(1.0));
/// ```
#[derive(Clone, Copy, Default, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Gamma(pub f32);
basic_unit!(Gamma);

impl Gamma {
    /// Does a force conversion of gamma-distance (absolute distance of component) to a phi-distance 
    /// (absolute distance for mathematical calculations)
    pub fn force_to_phi(self) -> Phi {
        Phi(self.0)
    }
}

impl Sub<Gamma> for Gamma {
    type Output = Delta;
    
    #[inline(always)]
    fn sub(self, rhs: Gamma) -> Self::Output {
        Delta(self.0 - rhs.0)
    }
}

impl Add<Delta> for Gamma {
    type Output = Gamma;

    #[inline(always)]
    fn add(self, rhs: Delta) -> Self::Output {
        Self(self.0 + rhs.0)
    }
}

impl Add<Gamma> for Delta {
    type Output = Delta;

    #[inline(always)]
    fn add(self, rhs: Gamma) -> Self::Output {
        Self(self.0 + rhs.0)
    }
}

impl AddAssign<Delta> for Gamma {
    fn add_assign(&mut self, rhs: Delta) {
        self.0 += rhs.0;
    }
}

impl Sub<Delta> for Gamma {
    type Output = Gamma;

    #[inline]
    fn sub(self, rhs: Delta) -> Self::Output {
        Self(self.0 - rhs.0)
    }
}

impl SubAssign for Gamma {
    fn sub_assign(&mut self, rhs: Self) {
        self.0 -= rhs.0;
    }
}


/// Helper functions to force convert an array of gammas to phis
#[inline]
pub fn force_phis_from_gammas<const N : usize>(gammas : [Gamma; N]) -> [Phi; N] {
    let mut phis = [Phi::ZERO; N];
    for i in 0 .. N {
        phis[i] = gammas[i].force_to_phi();
    }
    phis
}

/// Helper functions to foce convert an array of phis to gammas
#[inline]
pub fn force_gammas_from_phis<const N : usize>(phis : [Phi; N]) -> [Gamma; N] {
    let mut gammas = [Gamma::ZERO; N];
    for i in 0 .. N {
        gammas[i] = phis[i].force_to_gamma();
    }
    gammas
}

/// The phi distance represents the mathematical distance used for calculations
/// 
/// # Unit
/// 
/// - Can be either radians or millimeters
#[derive(Clone, Copy, Default, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Phi(pub f32);
basic_unit!(Phi);

impl Phi {
    /// Does a force conversion of this phi-distance (absolute distance for mathematical calculations) to a gamma-distance 
    /// (absolute distance for components)
    pub fn force_to_gamma(self) -> Gamma {
        Gamma(self.0)
    }
}

impl Sub<Phi> for Phi {
    type Output = Delta;

    fn sub(self, rhs: Phi) -> Self::Output {
        Delta(self.0 - rhs.0)
    }
}

impl Add<Delta> for Phi {
    type Output = Phi;

    #[inline(always)]
    fn add(self, rhs : Delta) -> Self::Output {
        Phi(self.0 + rhs.0)
    }
}

impl Add<Phi> for Delta {
    type Output = Phi;

    #[inline(always)]
    fn add(self, rhs: Phi) -> Self::Output {
        Phi(self.0 + rhs.0)
    }
}

impl AddAssign<Delta> for Phi {
    fn add_assign(&mut self, rhs: Delta) {
        self.0 += rhs.0;
    }
}

impl Sub<Delta> for Phi {
    type Output = Phi;

    #[inline(always)]
    fn sub(self, rhs: Delta) -> Self::Output {
        Self(self.0 - rhs.0)
    }
}

impl SubAssign<Delta> for Phi {
    fn sub_assign(&mut self, rhs: Delta) {
        self.0 += rhs.0;
    }
}

/// The delta distance represents a relative distance traveled by the 
/// 
/// # Unit
/// 
/// - Can be either radians or millimeters
/// 
/// ```rust
/// use syunit::*;
/// 
/// assert_eq!(Delta(2.0), Delta(1.0) + Delta(1.0));
/// assert_eq!(Delta(5.0), Delta(2.5) * 2.0);
/// assert_eq!(Delta(2.0), Gamma(4.0) - Gamma(2.0));
/// ```
#[derive(Clone, Copy, Default, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Delta(pub f32);
basic_unit!(Delta);
additive_unit!(Delta);

impl Delta {
    /// Creates a new delta distance from a starting point `start` and an endpoint `end`
    #[inline(always)]
    pub fn diff(start : Gamma, end : Gamma) -> Self {
        end - start
    }
}

/// Represents a change in distance over time
/// 
/// # Unit
/// 
/// - Can be either radians per second or millimeters per second
#[derive(Clone, Copy, Default, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Velocity(pub f32);
basic_unit!(Velocity);
additive_unit!(Velocity);
derive_units!(Delta, Velocity, Time);

impl Div<Velocity> for f32 {
    type Output = Time;

    #[inline(always)]
    fn div(self, rhs: Velocity) -> Self::Output {
        Time(self / rhs.0)
    }
}

/// Represents a change in distance over time
/// 
/// # Unit
/// 
/// - Hertz (1 / seconds)
#[derive(Clone, Copy, Default, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Frequency(pub f32);
basic_unit!(Frequency);
additive_unit!(Frequency);

impl Div<Frequency> for f32 {
    type Output = Time;

    #[inline(always)]
    fn div(self, rhs: Frequency) -> Self::Output {
        Time(self / rhs.0)
    }
}

/// Represents a change in velocity over time
/// 
/// # Unit
/// 
/// - Can be either radians per second^2 or millimeters per second^2
/// 
/// ```
/// use syunit::*;
/// 
/// assert_eq!(Velocity(5.0), Acceleration(2.5) * Time(2.0));
/// assert_eq!(Acceleration(2.5), Velocity(5.0) / Time(2.0));
#[derive(Clone, Copy, Default, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Acceleration(pub f32); 
basic_unit!(Acceleration);
additive_unit!(Acceleration);
derive_units!(Velocity, Acceleration, Time);
derive_units!(Force, Acceleration, Inertia);

/// Represents a change in acceleration over time
/// 
/// # Unit
/// 
/// - Can be either radians per second^3 or millimeters per second^3
#[derive(Clone, Copy, Default, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Jolt(pub f32); 
basic_unit!(Jolt);
additive_unit!(Jolt);
derive_units!(Acceleration, Jolt, Time);

/// Represents an inertia, slowing down movement processes
/// 
/// # Unit
/// 
/// - Can be either kilogramm or kilogramm times meter^2
#[derive(Clone, Copy, Default, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Inertia(pub f32);
basic_unit!(Inertia);
additive_unit!(Inertia);

/// Represents a force, slowing down movement processes, eventually even overloading the component
/// 
/// # Unit
/// 
/// - Can be either Newton or Newtonmeter
#[derive(Clone, Copy, Default, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Force(pub f32);
basic_unit!(Force);
additive_unit!(Force);