Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
open-runtime-module-library
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Noxim
open-runtime-module-library
Commits
bacd7b80
Unverified
Commit
bacd7b80
authored
5 years ago
by
Shaopeng Wang
Committed by
GitHub
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Update fixedu128 serde impl. (#87)
parent
2f70d290
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
utilities/src/fixed128.rs
+9
-59
9 additions, 59 deletions
utilities/src/fixed128.rs
with
9 additions
and
59 deletions
utilities/src/fixed128.rs
+
9
−
59
View file @
bacd7b80
...
...
@@ -212,30 +212,12 @@ impl_perthing_into_fixed_u128!(Perquintill);
#[cfg(feature
=
"std"
)]
impl
FixedU128
{
fn
str_with_precision
(
&
self
)
->
String
{
format!
(
"{}.{}"
,
&
self
.0
/
DIV
,
&
self
.0
%
DIV
)
}
fn
from_str_with_precision
(
s
:
&
str
)
->
Result
<
Self
,
&
'static
str
>
{
let
err
=
"invalid string input"
;
let
vec_str
:
Vec
<&
str
>
=
s
.split
(
"."
)
.collect
();
// parsing to decimal and fractional parts
let
(
decimal_str
,
fractional_str
)
=
match
vec_str
.as_slice
()
{
&
[
d
]
=>
(
d
,
"0"
),
&
[
d
,
f
]
=>
(
d
,
f
),
_
=>
return
Err
(
err
),
};
let
decimal
:
u128
=
decimal_str
.parse
()
.map_err
(|
_
|
err
)
?
;
let
decimal_with_precision
=
decimal
.checked_mul
(
DIV
)
.ok_or
(
err
)
?
;
// width = 18; precision = 18
let
padded_fractional_string
=
format!
(
"{:0<18.18}"
,
fractional_str
);
let
fractional_with_precision
:
u128
=
padded_fractional_string
.parse
()
.map_err
(|
_
|
err
)
?
;
let
parts
=
decimal_with_precision
.checked_add
(
fractional_with_precision
)
.ok_or
(
err
)
?
;
fn
u128_str
(
&
self
)
->
String
{
format!
(
"{}"
,
&
self
.0
)
}
fn
try_from_u128_str
(
s
:
&
str
)
->
Result
<
Self
,
&
'static
str
>
{
let
parts
:
u128
=
s
.parse
()
.map_err
(|
_
|
"invalid string input"
)
?
;
Ok
(
Self
::
from_parts
(
parts
))
}
}
...
...
@@ -248,7 +230,7 @@ impl Serialize for FixedU128 {
where
S
:
Serializer
,
{
serializer
.serialize_str
(
&
self
.
str_with_precision
())
serializer
.serialize_str
(
&
self
.
u128_str
())
}
}
...
...
@@ -261,7 +243,7 @@ impl<'de> Deserialize<'de> for FixedU128 {
D
:
Deserializer
<
'de
>
,
{
let
s
=
String
::
deserialize
(
deserializer
)
?
;
FixedU128
::
from_
str_with_precision
(
&
s
)
.map_err
(|
err_str
|
de
::
Error
::
custom
(
err_str
))
FixedU128
::
try_
from_
u128_str
(
&
s
)
.map_err
(|
err_str
|
de
::
Error
::
custom
(
err_str
))
}
}
...
...
@@ -416,43 +398,11 @@ mod tests {
assert_eq!
(
a
.recip
(),
None
);
}
#[test]
fn
from_str_with_precision_should_work
()
{
assert_eq!
(
FixedU128
::
from_str_with_precision
(
"1"
)
.unwrap
(),
FixedU128
::
from_natural
(
1
)
);
assert_eq!
(
FixedU128
::
from_str_with_precision
(
"1.0"
)
.unwrap
(),
FixedU128
::
from_natural
(
1
)
);
assert_eq!
(
FixedU128
::
from_str_with_precision
(
"0.1"
)
.unwrap
(),
FixedU128
::
from_rational
(
1
,
10
)
);
assert_eq!
(
FixedU128
::
from_str_with_precision
(
"2.5"
)
.unwrap
(),
FixedU128
::
from_rational
(
5
,
2
)
);
assert_eq!
(
FixedU128
::
from_str_with_precision
(
"0.1000000000000000111"
)
.unwrap
(),
FixedU128
::
from_rational
(
100000000000000011u128
,
1000000000000000000u128
)
);
assert!
(
FixedU128
::
from_str_with_precision
(
"."
)
.is_err
());
assert!
(
FixedU128
::
from_str_with_precision
(
""
)
.is_err
());
assert!
(
FixedU128
::
from_str_with_precision
(
"1.1.1"
)
.is_err
());
assert!
(
FixedU128
::
from_str_with_precision
(
"a.1"
)
.is_err
());
assert!
(
FixedU128
::
from_str_with_precision
(
"1.a"
)
.is_err
());
// 340282366920938463464 == u128::max_value() / DIV + 1; overflows
assert!
(
FixedU128
::
from_str_with_precision
(
"340282366920938463464"
)
.is_err
());
}
#[test]
fn
serialize_deserialize_should_work
()
{
let
two_point_five
=
FixedU128
::
from_rational
(
5
,
2
);
let
serialized
=
serde_json
::
to_string
(
&
two_point_five
)
.unwrap
();
assert_eq!
(
serialized
,
"
\"
2
.
500000000000000000
\"
"
);
assert_eq!
(
serialized
,
"
\"
2500000000000000000
\"
"
);
let
deserialized
:
FixedU128
=
serde_json
::
from_str
(
&
serialized
)
.unwrap
();
assert_eq!
(
deserialized
,
two_point_five
);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment