Skip to content

Commit

Permalink
Rename custom Result type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
printfn committed Jan 13, 2024
1 parent 58136e4 commit bb43194
Show file tree
Hide file tree
Showing 31 changed files with 368 additions and 405 deletions.
22 changes: 11 additions & 11 deletions core/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::eval::evaluate_to_value;
use crate::ident::Ident;
use crate::interrupt::test_int;
use crate::num::{Base, FormattingStyle, Number};
use crate::result::FendCoreResult;
use crate::result::FResult;
use crate::scope::Scope;
use crate::serialize::{Deserialize, Serialize};
use crate::value::{built_in_function::BuiltInFunction, ApplyMulHandling, Value};
Expand Down Expand Up @@ -35,7 +35,7 @@ pub(crate) enum Bop {
}

impl Bop {
pub(crate) fn serialize(self, write: &mut impl io::Write) -> FendCoreResult<()> {
pub(crate) fn serialize(self, write: &mut impl io::Write) -> FResult<()> {
let n: u8 = match self {
Self::Plus => 0,
Self::ImplicitPlus => 1,
Expand All @@ -56,7 +56,7 @@ impl Bop {
Ok(())
}

pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult<Self> {
pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult<Self> {
Ok(match u8::deserialize(read)? {
0 => Self::Plus,
1 => Self::ImplicitPlus,
Expand Down Expand Up @@ -126,7 +126,7 @@ pub(crate) enum Expr {
}

impl Expr {
pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FendCoreResult<()> {
pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FResult<()> {
match self {
Self::Literal(x) => {
0u8.serialize(write)?;
Expand Down Expand Up @@ -206,7 +206,7 @@ impl Expr {
Ok(())
}

pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult<Self> {
pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult<Self> {
Ok(match u8::deserialize(read)? {
0 => Self::Literal(Value::deserialize(read)?),
1 => Self::Ident(Ident::deserialize(read)?),
Expand Down Expand Up @@ -261,7 +261,7 @@ impl Expr {
attrs: Attrs,
ctx: &mut crate::Context,
int: &I,
) -> FendCoreResult<String> {
) -> FResult<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 @@ -314,7 +314,7 @@ impl Expr {
}

/// returns true if rhs is '-1' or '(-1)'
fn should_compute_inverse<I: Interrupt>(rhs: &Expr, int: &I) -> FendCoreResult<bool> {
fn should_compute_inverse<I: Interrupt>(rhs: &Expr, int: &I) -> FResult<bool> {
if let Expr::UnaryMinus(inner) = rhs {
if let Expr::Literal(Value::Num(n)) = &**inner {
if n.is_unitless_one(int)? {
Expand All @@ -340,7 +340,7 @@ pub(crate) fn evaluate<I: Interrupt>(
attrs: Attrs,
context: &mut crate::Context,
int: &I,
) -> FendCoreResult<Value> {
) -> FResult<Value> {
macro_rules! eval {
($e:expr) => {
evaluate($e, scope.clone(), attrs, context, int)
Expand Down Expand Up @@ -460,7 +460,7 @@ fn evaluate_add<I: Interrupt>(
b: Value,
scope: Option<Arc<Scope>>,
int: &I,
) -> FendCoreResult<Value> {
) -> FResult<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 @@ -504,7 +504,7 @@ fn evaluate_as<I: Interrupt>(
attrs: Attrs,
context: &mut crate::Context,
int: &I,
) -> FendCoreResult<Value> {
) -> FResult<Value> {
if let Expr::Ident(ident) = &b {
match ident.as_str() {
"bool" | "boolean" => {
Expand Down Expand Up @@ -581,7 +581,7 @@ pub(crate) fn resolve_identifier<I: Interrupt>(
attrs: Attrs,
context: &mut crate::Context,
int: &I,
) -> FendCoreResult<Value> {
) -> FResult<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 day_of_week::DayOfWeek;
pub(crate) use month::Month;
use year::Year;

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

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

impl Date {
pub(crate) fn today(context: &mut crate::Context) -> FendCoreResult<Self> {
pub(crate) fn today(context: &mut crate::Context) -> FResult<Self> {
let Some(current_time_info) = &context.current_time else {
return Err(FendError::UnableToGetCurrentDate);
};
Expand Down Expand Up @@ -120,7 +120,7 @@ impl Date {
}
}

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

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

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

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

pub(crate) fn get_object_member(self, key: &Ident) -> FendCoreResult<crate::value::Value> {
pub(crate) fn get_object_member(self, key: &Ident) -> FResult<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) -> FendCoreResult<Value> {
pub(crate) fn add(self, rhs: Value) -> FResult<Value> {
let rhs = rhs.expect_num()?;
let int = &crate::interrupt::Never;
if rhs.unit_equal_to("day") {
Expand All @@ -212,7 +212,7 @@ impl Date {
}
}

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

Expand Down
6 changes: 3 additions & 3 deletions core/src/date/day.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::result::FendCoreResult;
use crate::result::FResult;
use crate::FendError;
use crate::{Deserialize, Serialize};
use std::fmt;
Expand All @@ -17,12 +17,12 @@ impl Day {
Self(day)
}

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

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

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

pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult<Self> {
pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult<Self> {
Ok(match u8::deserialize(read)? {
0 => Self::Sunday,
1 => Self::Monday,
Expand Down
6 changes: 3 additions & 3 deletions core/src/date/month.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::result::FendCoreResult;
use crate::result::FResult;
use crate::FendError;
use crate::{
date::Year,
Expand Down Expand Up @@ -88,12 +88,12 @@ impl Month {
}
}

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

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

Expand Down
4 changes: 2 additions & 2 deletions core/src/date/parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
date::{Date, Day, Month, Year},
error::FendError,
result::FendCoreResult,
result::FResult,
};
use std::convert;

Expand Down Expand Up @@ -60,7 +60,7 @@ fn parse_yyyymmdd(s: &str) -> Result<(Date, &str), ()> {
Ok((Date { year, month, day }, s))
}

pub(crate) fn parse_date(s: &str) -> FendCoreResult<Date> {
pub(crate) fn parse_date(s: &str) -> FResult<Date> {
let trimmed = s.trim();
if let Ok((date, remaining)) = parse_yyyymmdd(trimmed) {
if remaining.is_empty() {
Expand Down
6 changes: 3 additions & 3 deletions core/src/date/year.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{convert, fmt, io};

use crate::{
error::FendError,
result::FendCoreResult,
result::FResult,
serialize::{Deserialize, Serialize},
};

Expand Down Expand Up @@ -54,12 +54,12 @@ impl Year {
}
}

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

pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult<Self> {
pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult<Self> {
Self::try_from(i32::deserialize(read)?).map_err(|_| FendError::DeserializationError)
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/eval.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use crate::{
ast, error::Interrupt, lexer, parser, result::FendCoreResult, scope::Scope, value::Value, Span,
ast, error::Interrupt, lexer, parser, result::FResult, scope::Scope, value::Value, Span,
};

pub(crate) fn evaluate_to_value<I: Interrupt>(
Expand All @@ -10,7 +10,7 @@ pub(crate) fn evaluate_to_value<I: Interrupt>(
attrs: Attrs,
context: &mut crate::Context,
int: &I,
) -> FendCoreResult<Value> {
) -> FResult<Value> {
let lex = lexer::lex(input, int);
let mut tokens = vec![];
let mut missing_open_parens: i32 = 0;
Expand Down Expand Up @@ -77,7 +77,7 @@ pub(crate) fn evaluate_to_spans<I: Interrupt>(
scope: Option<Arc<Scope>>,
context: &mut crate::Context,
int: &I,
) -> FendCoreResult<(Vec<Span>, bool, Attrs)> {
) -> FResult<(Vec<Span>, bool, Attrs)> {
let (attrs, input) = parse_attrs(input);
let value = evaluate_to_value(input, scope, attrs, context, int)?;
context.variables.insert("_".to_string(), value.clone());
Expand Down
10 changes: 3 additions & 7 deletions core/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
use crate::error::Interrupt;
use crate::num::Exact;
use crate::result::FendCoreResult;
use crate::result::FResult;
use std::fmt;

pub(crate) trait Format {
type Params: Default;
type Out: fmt::Display + fmt::Debug;

fn format<I: Interrupt>(
&self,
params: &Self::Params,
int: &I,
) -> FendCoreResult<Exact<Self::Out>>;
fn format<I: Interrupt>(&self, params: &Self::Params, int: &I) -> FResult<Exact<Self::Out>>;

/// Simpler alternative to calling format
fn fm<I: Interrupt>(&self, int: &I) -> FendCoreResult<Self::Out> {
fn fm<I: Interrupt>(&self, int: &I) -> FResult<Self::Out> {
Ok(self.format(&Default::default(), int)?.value)
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/ident.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{borrow::Cow, fmt, io};

use crate::{
result::FendCoreResult,
result::FResult,
serialize::{Deserialize, Serialize},
};

Expand All @@ -27,11 +27,11 @@ impl Ident {
self.0 == "$" || self.0 == "\u{a3}"
}

pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FendCoreResult<()> {
pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FResult<()> {
self.0.as_ref().serialize(write)
}

pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult<Self> {
pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult<Self> {
Ok(Self(Cow::Owned(String::deserialize(read)?)))
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/interrupt.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{error::FendError, result::FendCoreResult};
use crate::{error::FendError, result::FResult};

pub trait Interrupt {
fn should_interrupt(&self) -> bool;
}

pub(crate) fn test_int<I: crate::error::Interrupt>(int: &I) -> FendCoreResult<()> {
pub(crate) fn test_int<I: crate::error::Interrupt>(int: &I) -> FResult<()> {
if int.should_interrupt() {
Err(FendError::Interrupted)
} else {
Expand Down
Loading

0 comments on commit bb43194

Please sign in to comment.