Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(model): Add associated types to CommandOption(Value). #2415

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion twilight-model/src/application/command/option.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use crate::channel::ChannelType;
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::{cmp::Eq, collections::HashMap};
use std::{
cmp::Eq,
collections::HashMap,
ops::{Range, RangeInclusive},
};

/// Option for a [`Command`].
///
Expand Down Expand Up @@ -151,6 +155,11 @@ pub struct CommandOption {
pub required: Option<bool>,
}

impl CommandOption {
/// This range is the length a string may be.
pub const STRING_LENGTH_RANGE: RangeInclusive<u16> = 0..=6000;
}

/// A predetermined choice users can select.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct CommandOptionChoice {
Expand Down Expand Up @@ -199,6 +208,27 @@ pub enum CommandOptionValue {
Number(f64),
}

impl CommandOptionValue {
/// This range contains integer values that safely can be
/// represented as a 64-bit floating point value.
///
/// Values outside of this range will result in a `400 Bad
/// Request`.
pub const INTEGER_RANGE: Range<i64> =
-(2_i64.pow(f64::MANTISSA_DIGITS) - 1)..(2_i64.pow(f64::MANTISSA_DIGITS));
/// This range contains all floating point values that can be
/// safely used as Discord Number values.
///
/// Values outside of this range will result in a `400 Bad
/// Request`.
// As we can see above we are within 52 bits on the left, but uses
// 53 bits on the right. We ensure to be within 52 bits on the
// right below by subtracting 1 first and using RangeInclusive.
#[allow(clippy::cast_precision_loss)]
pub const NUMBER_RANGE: RangeInclusive<f64> =
(Self::INTEGER_RANGE.start as f64)..=((Self::INTEGER_RANGE.end - 1) as f64);
}

/// Type of a [`CommandOption`].
#[derive(Clone, Copy, Debug, Deserialize_repr, Eq, Hash, PartialEq, Serialize_repr)]
#[non_exhaustive]
Expand Down