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

[Refact] Add Ser/De Trait #256

Merged
merged 4 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
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
63 changes: 32 additions & 31 deletions core/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
use crate::ident::Ident;
use crate::interrupt::test_int;
use crate::num::{Base, FormattingStyle, Number};
use crate::result::FendCoreResult;
use crate::scope::Scope;
use crate::serialize::{deserialize_u8, serialize_u8};
use crate::serialize::{Deserialize, Serialize};
use crate::value::{built_in_function::BuiltInFunction, ApplyMulHandling, Value};
use crate::Attrs;
use std::sync::Arc;
Expand Down Expand Up @@ -34,8 +35,8 @@
}

impl Bop {
pub(crate) fn serialize(self, write: &mut impl io::Write) -> Result<(), FendError> {
let n = match self {
pub(crate) fn serialize(self, write: &mut impl io::Write) -> FendCoreResult<()> {
let n: u8 = match self {
Self::Plus => 0,
Self::ImplicitPlus => 1,
Self::Minus => 2,
Expand All @@ -51,12 +52,12 @@
Self::Combination => 12,
Self::Permutation => 13,
};
serialize_u8(n, write)?;
n.serialize(write)?;
Ok(())
}

pub(crate) fn deserialize(read: &mut impl io::Read) -> Result<Self, FendError> {
Ok(match deserialize_u8(read)? {
pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult<Self> {
Ok(match u8::deserialize(read)? {
0 => Self::Plus,
1 => Self::ImplicitPlus,
2 => Self::Minus,
Expand Down Expand Up @@ -125,88 +126,88 @@
}

impl Expr {
pub(crate) fn serialize(&self, write: &mut impl io::Write) -> Result<(), FendError> {
pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FendCoreResult<()> {
match self {
Self::Literal(x) => {
serialize_u8(0, write)?;
0u8.serialize(write)?;
x.serialize(write)?;
}
Self::Ident(i) => {
serialize_u8(1, write)?;
1u8.serialize(write)?;
i.serialize(write)?;
}
Self::Parens(e) => {
serialize_u8(2, write)?;
2u8.serialize(write)?;

Check warning on line 140 in core/src/ast.rs

View check run for this annotation

Codecov / codecov/patch

core/src/ast.rs#L140

Added line #L140 was not covered by tests
e.serialize(write)?;
}
Self::UnaryMinus(e) => {
serialize_u8(3, write)?;
3u8.serialize(write)?;

Check warning on line 144 in core/src/ast.rs

View check run for this annotation

Codecov / codecov/patch

core/src/ast.rs#L144

Added line #L144 was not covered by tests
e.serialize(write)?;
}
Self::UnaryPlus(e) => {
serialize_u8(4, write)?;
4u8.serialize(write)?;

Check warning on line 148 in core/src/ast.rs

View check run for this annotation

Codecov / codecov/patch

core/src/ast.rs#L148

Added line #L148 was not covered by tests
e.serialize(write)?;
}
Self::UnaryDiv(e) => {
serialize_u8(5, write)?;
5u8.serialize(write)?;

Check warning on line 152 in core/src/ast.rs

View check run for this annotation

Codecov / codecov/patch

core/src/ast.rs#L152

Added line #L152 was not covered by tests
e.serialize(write)?;
}
Self::Factorial(e) => {
serialize_u8(6, write)?;
6u8.serialize(write)?;

Check warning on line 156 in core/src/ast.rs

View check run for this annotation

Codecov / codecov/patch

core/src/ast.rs#L156

Added line #L156 was not covered by tests
e.serialize(write)?;
}
Self::Bop(op, a, b) => {
serialize_u8(7, write)?;
7u8.serialize(write)?;
op.serialize(write)?;
a.serialize(write)?;
b.serialize(write)?;
}
Self::Apply(a, b) => {
serialize_u8(8, write)?;
8u8.serialize(write)?;

Check warning on line 166 in core/src/ast.rs

View check run for this annotation

Codecov / codecov/patch

core/src/ast.rs#L166

Added line #L166 was not covered by tests
a.serialize(write)?;
b.serialize(write)?;
}
Self::ApplyFunctionCall(a, b) => {
serialize_u8(9, write)?;
9u8.serialize(write)?;
a.serialize(write)?;
b.serialize(write)?;
}
Self::ApplyMul(a, b) => {
serialize_u8(10, write)?;
10u8.serialize(write)?;

Check warning on line 176 in core/src/ast.rs

View check run for this annotation

Codecov / codecov/patch

core/src/ast.rs#L176

Added line #L176 was not covered by tests
a.serialize(write)?;
b.serialize(write)?;
}
Self::As(a, b) => {
serialize_u8(11, write)?;
11u8.serialize(write)?;

Check warning on line 181 in core/src/ast.rs

View check run for this annotation

Codecov / codecov/patch

core/src/ast.rs#L181

Added line #L181 was not covered by tests
a.serialize(write)?;
b.serialize(write)?;
}
Self::Fn(a, b) => {
serialize_u8(12, write)?;
12u8.serialize(write)?;

Check warning on line 186 in core/src/ast.rs

View check run for this annotation

Codecov / codecov/patch

core/src/ast.rs#L186

Added line #L186 was not covered by tests
a.serialize(write)?;
b.serialize(write)?;
}
Self::Of(a, b) => {
serialize_u8(13, write)?;
13u8.serialize(write)?;

Check warning on line 191 in core/src/ast.rs

View check run for this annotation

Codecov / codecov/patch

core/src/ast.rs#L191

Added line #L191 was not covered by tests
a.serialize(write)?;
b.serialize(write)?;
}
Self::Assign(a, b) => {
serialize_u8(14, write)?;
14u8.serialize(write)?;

Check warning on line 196 in core/src/ast.rs

View check run for this annotation

Codecov / codecov/patch

core/src/ast.rs#L196

Added line #L196 was not covered by tests
a.serialize(write)?;
b.serialize(write)?;
}
Self::Statements(a, b) => {
serialize_u8(15, write)?;
15u8.serialize(write)?;

Check warning on line 201 in core/src/ast.rs

View check run for this annotation

Codecov / codecov/patch

core/src/ast.rs#L201

Added line #L201 was not covered by tests
a.serialize(write)?;
b.serialize(write)?;
}
}
Ok(())
}

pub(crate) fn deserialize(read: &mut impl io::Read) -> Result<Self, FendError> {
Ok(match deserialize_u8(read)? {
pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult<Self> {
Ok(match u8::deserialize(read)? {
0 => Self::Literal(Value::deserialize(read)?),
1 => Self::Ident(Ident::deserialize(read)?),
2 => Self::Parens(Box::new(Self::deserialize(read)?)),
Expand Down Expand Up @@ -260,7 +261,7 @@
attrs: Attrs,
ctx: &mut crate::Context,
int: &I,
) -> Result<String, FendError> {
) -> FendCoreResult<String> {
Ok(match self {
Self::Literal(Value::String(s)) => format!(r#""{}""#, s.as_ref()),
Self::Literal(v) => v.format_to_plain_string(0, attrs, ctx, int)?,
Expand Down Expand Up @@ -313,7 +314,7 @@
}

/// returns true if rhs is '-1' or '(-1)'
fn should_compute_inverse<I: Interrupt>(rhs: &Expr, int: &I) -> Result<bool, FendError> {
fn should_compute_inverse<I: Interrupt>(rhs: &Expr, int: &I) -> FendCoreResult<bool> {
if let Expr::UnaryMinus(inner) = rhs {
if let Expr::Literal(Value::Num(n)) = &**inner {
if n.is_unitless_one(int)? {
Expand All @@ -339,7 +340,7 @@
attrs: Attrs,
context: &mut crate::Context,
int: &I,
) -> Result<Value, FendError> {
) -> FendCoreResult<Value> {
macro_rules! eval {
($e:expr) => {
evaluate($e, scope.clone(), attrs, context, int)
Expand Down Expand Up @@ -459,7 +460,7 @@
b: Value,
scope: Option<Arc<Scope>>,
int: &I,
) -> Result<Value, FendError> {
) -> FendCoreResult<Value> {
Ok(match (a, b) {
(Value::Num(a), Value::Num(b)) => Value::Num(Box::new(a.add(*b, int)?)),
(Value::String(a), Value::String(b)) => {
Expand Down Expand Up @@ -503,7 +504,7 @@
attrs: Attrs,
context: &mut crate::Context,
int: &I,
) -> Result<Value, FendError> {
) -> FendCoreResult<Value> {
if let Expr::Ident(ident) = &b {
match ident.as_str() {
"bool" | "boolean" => {
Expand Down Expand Up @@ -580,7 +581,7 @@
attrs: Attrs,
context: &mut crate::Context,
int: &I,
) -> Result<Value, FendError> {
) -> FendCoreResult<Value> {
macro_rules! eval_box {
($input:expr) => {
Box::new(evaluate_to_value(
Expand Down
18 changes: 9 additions & 9 deletions core/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
pub(crate) use month::Month;
use year::Year;

use crate::{error::FendError, ident::Ident, value::Value};
use crate::{error::FendError, ident::Ident, result::FendCoreResult, value::Value};

#[derive(Copy, Clone, Eq, PartialEq)]
pub(crate) struct Date {
Expand All @@ -21,7 +21,7 @@
}

impl Date {
pub(crate) fn today(context: &mut crate::Context) -> Result<Self, FendError> {
pub(crate) fn today(context: &mut crate::Context) -> FendCoreResult<Self> {

Check warning on line 24 in core/src/date.rs

View check run for this annotation

Codecov / codecov/patch

core/src/date.rs#L24

Added line #L24 was not covered by tests
let Some(current_time_info) = &context.current_time else {
return Err(FendError::UnableToGetCurrentDate);
};
Expand Down Expand Up @@ -120,7 +120,7 @@
}
}

pub(crate) fn diff_months(self, mut months: i64) -> Result<Self, FendError> {
pub(crate) fn diff_months(self, mut months: i64) -> FendCoreResult<Self> {
let mut result = self;
while months >= 12 {
result.year = result.year.next();
Expand Down Expand Up @@ -170,34 +170,34 @@
Ok(result)
}

pub(crate) fn parse(s: &str) -> Result<Self, FendError> {
pub(crate) fn parse(s: &str) -> FendCoreResult<Self> {
parser::parse_date(s)
}

pub(crate) fn serialize(self, write: &mut impl io::Write) -> Result<(), FendError> {
pub(crate) fn serialize(self, write: &mut impl io::Write) -> FendCoreResult<()> {
self.year.serialize(write)?;
self.month.serialize(write)?;
self.day.serialize(write)?;
Ok(())
}

pub(crate) fn deserialize(read: &mut impl io::Read) -> Result<Self, FendError> {
pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult<Self> {
Ok(Self {
year: Year::deserialize(read)?,
month: Month::deserialize(read)?,
day: Day::deserialize(read)?,
})
}

pub(crate) fn get_object_member(self, key: &Ident) -> Result<crate::value::Value, FendError> {
pub(crate) fn get_object_member(self, key: &Ident) -> FendCoreResult<crate::value::Value> {
Ok(match key.as_str() {
"month" => Value::Month(self.month),
"day_of_week" => Value::DayOfWeek(self.day_of_week()),
_ => return Err(FendError::CouldNotFindKey(key.to_string())),
})
}

pub(crate) fn add(self, rhs: Value) -> Result<Value, FendError> {
pub(crate) fn add(self, rhs: Value) -> FendCoreResult<Value> {
let rhs = rhs.expect_num()?;
let int = &crate::interrupt::Never;
if rhs.unit_equal_to("day") {
Expand All @@ -212,7 +212,7 @@
}
}

pub(crate) fn sub(self, rhs: Value) -> Result<Value, FendError> {
pub(crate) fn sub(self, rhs: Value) -> FendCoreResult<Value> {
let int = &crate::interrupt::Never;
let rhs = rhs.expect_num()?;

Expand Down
12 changes: 6 additions & 6 deletions core/src/date/day.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::serialize::deserialize_u8;
use crate::serialize::serialize_u8;
use crate::result::FendCoreResult;
use crate::FendError;
use crate::{Deserialize, Serialize};
use std::fmt;
use std::io;

Expand All @@ -17,13 +17,13 @@ impl Day {
Self(day)
}

pub(crate) fn serialize(self, write: &mut impl io::Write) -> Result<(), FendError> {
serialize_u8(self.value(), write)?;
pub(crate) fn serialize(self, write: &mut impl io::Write) -> FendCoreResult<()> {
self.value().serialize(write)?;
Ok(())
}

pub(crate) fn deserialize(read: &mut impl io::Read) -> Result<Self, FendError> {
let n = deserialize_u8(read)?;
pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult<Self> {
let n = u8::deserialize(read)?;
if n == 0 || n >= 32 {
return Err(FendError::DeserializationError);
}
Expand Down
11 changes: 6 additions & 5 deletions core/src/date/day_of_week.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
error::FendError,
serialize::{deserialize_u8, serialize_u8},
result::FendCoreResult,
serialize::{Deserialize, Serialize},
};
use std::{fmt, io};

Expand Down Expand Up @@ -43,13 +44,13 @@ impl DayOfWeek {
}
}

pub(crate) fn serialize(self, write: &mut impl io::Write) -> Result<(), FendError> {
serialize_u8(self.as_u8(), write)?;
pub(crate) fn serialize(self, write: &mut impl io::Write) -> FendCoreResult<()> {
self.as_u8().serialize(write)?;
Ok(())
}

pub(crate) fn deserialize(read: &mut impl io::Read) -> Result<Self, FendError> {
Ok(match deserialize_u8(read)? {
pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult<Self> {
Ok(match u8::deserialize(read)? {
0 => Self::Sunday,
1 => Self::Monday,
2 => Self::Tuesday,
Expand Down
39 changes: 21 additions & 18 deletions core/src/date/month.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
use crate::serialize::serialize_u8;
use crate::result::FendCoreResult;
use crate::FendError;
use crate::{date::Year, serialize::deserialize_u8};
use crate::{
date::Year,
serialize::{Deserialize, Serialize},
};
use std::{convert, fmt, io};

#[derive(Copy, Clone, Eq, PartialEq)]
pub(crate) enum Month {
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December,
January = 1,
February = 2,
March = 3,
April = 4,
May = 5,
June = 6,
July = 7,
August = 8,
September = 9,
October = 10,
November = 11,
December = 12,
}

impl Month {
Expand Down Expand Up @@ -85,13 +88,13 @@ impl Month {
}
}

pub(crate) fn serialize(self, write: &mut impl io::Write) -> Result<(), FendError> {
serialize_u8(self.as_u8(), write)?;
pub(crate) fn serialize(self, write: &mut impl io::Write) -> FendCoreResult<()> {
self.as_u8().serialize(write)?;
Ok(())
}

pub(crate) fn deserialize(read: &mut impl io::Read) -> Result<Self, FendError> {
Self::try_from(deserialize_u8(read)?).map_err(|_| FendError::DeserializationError)
pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult<Self> {
Self::try_from(u8::deserialize(read)?).map_err(|_| FendError::DeserializationError)
}

fn as_u8(self) -> u8 {
Expand Down
Loading