Enum rsonpath_syntax::num::JsonNumber
source · pub enum JsonNumber {
Int(JsonInt),
Float(JsonFloat),
}Expand description
JSONPath numeric type - either a JsonInt or a JsonFloat.
Note that this type is not normalized and an integer in the range
[-253+1, (253)-1] can be represented both as
a JsonNumber::Int and as a JsonNumber::Float.
Which type is produced when is a parser implementation detail.
If you need to rely on integers always being represented as JsonNumber::Int
you can use JsonNumber::normalize, or manually inspect the underlying
JsonFloat using JsonFloat::is_int and its TryInto<JsonInt> conversion.
§Examples
let int = JsonInt::from(42);
let float = JsonFloat::try_from(42.01).unwrap();
let num_int = JsonNumber::from(int);
let num_float = JsonNumber::from(float);
assert_eq!(num_int, JsonNumber::Int(int));
assert_eq!(num_float, JsonNumber::Float(float));
assert_eq!("42", num_int.to_string());
assert_eq!("42.01", num_float.to_string());Variants§
Implementations§
source§impl JsonNumber
impl JsonNumber
sourcepub fn normalize(&self) -> Self
pub fn normalize(&self) -> Self
Normalize a JsonNumber so that valid JsonInt value is represented
by JsonNumber::Int.
The parser is allowed to represent a normal JSON integer (e.g. 17) as an
equivalent JSON float (17.0). Calling normalize ensures all values
representable by a JsonInt are indeed represented as such.
§Examples
// Creating a JsonNumber from a JsonFloat always gives JsonNumber::Float.
let f1 = JsonFloat::try_from(17.0).unwrap();
let nf1 = JsonNumber::from(f1);
assert_eq!(nf1, JsonNumber::Float(f1));
// Normalizing will give us an integer representation, when possible.
assert_eq!(nf1.normalize(), JsonNumber::Int(17.into()));
// If the float is an integer within range normalization will succeed.
let f2 = JsonFloat::try_from(1e15).unwrap();
let nf2 = JsonNumber::from(f2);
assert_eq!(nf2, JsonNumber::Float(f2));
assert_eq!(nf2.normalize(), JsonNumber::Int(1_000_000_000_000_000_i64.try_into().unwrap()));
// An int is an int, and remains so under normalization.
let i1 = JsonInt::from(42);
let ni1 = JsonNumber::from(i1);
assert_eq!(ni1, JsonNumber::Int(i1));
assert_eq!(ni1.normalize(), JsonNumber::Int(i1));
// A float that is not an int remains the same when normalized.
let f3 = JsonFloat::try_from(3.14).unwrap();
let nf3 = JsonNumber::from(f3);
assert_eq!(nf3, JsonNumber::Float(f3));
assert_eq!(nf3.normalize(), JsonNumber::Float(f3));
// A float that is an int, but outside of the interoperable JsonInt range,
// is not normalized.
let f4 = JsonFloat::try_from(1e120).unwrap();
let nf4 = JsonNumber::from(f4);
assert_eq!(nf4, JsonNumber::Float(f4));
assert_eq!(nf4.normalize(), JsonNumber::Float(f4));Trait Implementations§
source§impl<'arbitrary> Arbitrary<'arbitrary> for JsonNumber
impl<'arbitrary> Arbitrary<'arbitrary> for JsonNumber
source§fn arbitrary(u: &mut Unstructured<'arbitrary>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'arbitrary>) -> Result<Self>
Self from the given unstructured data. Read moresource§fn arbitrary_take_rest(u: Unstructured<'arbitrary>) -> Result<Self>
fn arbitrary_take_rest(u: Unstructured<'arbitrary>) -> Result<Self>
Self from the entirety of the given
unstructured data. Read moresource§impl Clone for JsonNumber
impl Clone for JsonNumber
source§fn clone(&self) -> JsonNumber
fn clone(&self) -> JsonNumber
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl Debug for JsonNumber
impl Debug for JsonNumber
source§impl Display for JsonNumber
impl Display for JsonNumber
source§impl From<JsonFloat> for JsonNumber
impl From<JsonFloat> for JsonNumber
source§impl From<JsonInt> for JsonNumber
impl From<JsonInt> for JsonNumber
source§impl From<JsonNumber> for Literal
impl From<JsonNumber> for Literal
source§fn from(value: JsonNumber) -> Self
fn from(value: JsonNumber) -> Self
source§impl Hash for JsonNumber
impl Hash for JsonNumber
source§impl Ord for JsonNumber
impl Ord for JsonNumber
source§impl PartialEq for JsonNumber
impl PartialEq for JsonNumber
source§impl PartialOrd for JsonNumber
impl PartialOrd for JsonNumber
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moreimpl Copy for JsonNumber
impl Eq for JsonNumber
Auto Trait Implementations§
impl Freeze for JsonNumber
impl RefUnwindSafe for JsonNumber
impl Send for JsonNumber
impl Sync for JsonNumber
impl Unpin for JsonNumber
impl UnwindSafe for JsonNumber
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moresource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more