Skip to main content

openpit/param/
adjustment_amount.rs

1// Copyright The Pit Project Owners. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// Please see https://github.com/openpitkit and the OWNERS file for details.
17
18use crate::param::PositionSize;
19
20/// Signed balance/position adjustment payload; delta applies change, absolute sets target value.
21#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22#[non_exhaustive]
23pub enum AdjustmentAmount {
24    /// Apply signed difference.
25    Delta(PositionSize),
26    /// Set resulting value.
27    Absolute(PositionSize),
28}
29
30impl std::fmt::Display for AdjustmentAmount {
31    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        match self {
33            Self::Delta(delta) => write!(formatter, "delta: {delta}"),
34            Self::Absolute(sz) => write!(formatter, "sz: {sz}"),
35        }
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::AdjustmentAmount;
42    use crate::param::PositionSize;
43
44    #[test]
45    fn delta_holds_signed_difference() {
46        let value = PositionSize::from_str("-3.5").expect("must be valid");
47        assert_eq!(
48            AdjustmentAmount::Delta(value),
49            AdjustmentAmount::Delta(value)
50        );
51    }
52
53    #[test]
54    fn absolute_holds_resulting_value() {
55        let value = PositionSize::from_str("12").expect("must be valid");
56        assert_eq!(
57            AdjustmentAmount::Absolute(value),
58            AdjustmentAmount::Absolute(value)
59        );
60    }
61
62    #[test]
63    fn display_delta_variant() {
64        let value = PositionSize::from_str("-3.5").expect("must be valid");
65        assert_eq!(AdjustmentAmount::Delta(value).to_string(), "delta: -3.5");
66    }
67
68    #[test]
69    fn display_absolute_variant() {
70        let value = PositionSize::from_str("12").expect("must be valid");
71        assert_eq!(AdjustmentAmount::Absolute(value).to_string(), "sz: 12");
72    }
73}