From bb431942dd6359b9c39607ce287312f105f5371c Mon Sep 17 00:00:00 2001 From: printfn <1643883+printfn@users.noreply.github.com> Date: Sat, 13 Jan 2024 09:28:19 +0000 Subject: [PATCH] Rename custom Result type alias --- core/src/ast.rs | 22 ++--- core/src/date.rs | 18 ++--- core/src/date/day.rs | 6 +- core/src/date/day_of_week.rs | 6 +- core/src/date/month.rs | 6 +- core/src/date/parser.rs | 4 +- core/src/date/year.rs | 6 +- core/src/eval.rs | 6 +- core/src/format.rs | 10 +-- core/src/ident.rs | 6 +- core/src/interrupt.rs | 4 +- core/src/lexer.rs | 53 ++++++------ core/src/lib.rs | 6 +- core/src/num/base.rs | 12 +-- core/src/num/bigrat.rs | 116 +++++++++++++-------------- core/src/num/biguint.rs | 50 +++++------- core/src/num/complex.rs | 76 +++++++++--------- core/src/num/continued_fraction.rs | 34 +++----- core/src/num/dist.rs | 30 +++---- core/src/num/formatting_style.rs | 6 +- core/src/num/real.rs | 68 ++++++++-------- core/src/num/unit.rs | 120 ++++++++++++++-------------- core/src/num/unit/base_unit.rs | 6 +- core/src/num/unit/named_unit.rs | 6 +- core/src/num/unit/unit_exponent.rs | 10 +-- core/src/result.rs | 2 +- core/src/scope.rs | 14 ++-- core/src/serialize.rs | 18 ++--- core/src/units.rs | 14 ++-- core/src/value.rs | 28 +++---- core/src/value/built_in_function.rs | 10 +-- 31 files changed, 368 insertions(+), 405 deletions(-) diff --git a/core/src/ast.rs b/core/src/ast.rs index 2c3ca407..72872922 100644 --- a/core/src/ast.rs +++ b/core/src/ast.rs @@ -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}; @@ -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, @@ -56,7 +56,7 @@ impl Bop { Ok(()) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Ok(match u8::deserialize(read)? { 0 => Self::Plus, 1 => Self::ImplicitPlus, @@ -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)?; @@ -206,7 +206,7 @@ impl Expr { Ok(()) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Ok(match u8::deserialize(read)? { 0 => Self::Literal(Value::deserialize(read)?), 1 => Self::Ident(Ident::deserialize(read)?), @@ -261,7 +261,7 @@ impl Expr { attrs: Attrs, ctx: &mut crate::Context, int: &I, - ) -> FendCoreResult { + ) -> FResult { 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)?, @@ -314,7 +314,7 @@ impl Expr { } /// returns true if rhs is '-1' or '(-1)' -fn should_compute_inverse(rhs: &Expr, int: &I) -> FendCoreResult { +fn should_compute_inverse(rhs: &Expr, int: &I) -> FResult { if let Expr::UnaryMinus(inner) = rhs { if let Expr::Literal(Value::Num(n)) = &**inner { if n.is_unitless_one(int)? { @@ -340,7 +340,7 @@ pub(crate) fn evaluate( attrs: Attrs, context: &mut crate::Context, int: &I, -) -> FendCoreResult { +) -> FResult { macro_rules! eval { ($e:expr) => { evaluate($e, scope.clone(), attrs, context, int) @@ -460,7 +460,7 @@ fn evaluate_add( b: Value, scope: Option>, int: &I, -) -> FendCoreResult { +) -> FResult { Ok(match (a, b) { (Value::Num(a), Value::Num(b)) => Value::Num(Box::new(a.add(*b, int)?)), (Value::String(a), Value::String(b)) => { @@ -504,7 +504,7 @@ fn evaluate_as( attrs: Attrs, context: &mut crate::Context, int: &I, -) -> FendCoreResult { +) -> FResult { if let Expr::Ident(ident) = &b { match ident.as_str() { "bool" | "boolean" => { @@ -581,7 +581,7 @@ pub(crate) fn resolve_identifier( attrs: Attrs, context: &mut crate::Context, int: &I, -) -> FendCoreResult { +) -> FResult { macro_rules! eval_box { ($input:expr) => { Box::new(evaluate_to_value( diff --git a/core/src/date.rs b/core/src/date.rs index 5ae6c1f0..e29d4a26 100644 --- a/core/src/date.rs +++ b/core/src/date.rs @@ -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 { @@ -21,7 +21,7 @@ pub(crate) struct Date { } impl Date { - pub(crate) fn today(context: &mut crate::Context) -> FendCoreResult { + pub(crate) fn today(context: &mut crate::Context) -> FResult { let Some(current_time_info) = &context.current_time else { return Err(FendError::UnableToGetCurrentDate); }; @@ -120,7 +120,7 @@ impl Date { } } - pub(crate) fn diff_months(self, mut months: i64) -> FendCoreResult { + pub(crate) fn diff_months(self, mut months: i64) -> FResult { let mut result = self; while months >= 12 { result.year = result.year.next(); @@ -170,18 +170,18 @@ impl Date { Ok(result) } - pub(crate) fn parse(s: &str) -> FendCoreResult { + pub(crate) fn parse(s: &str) -> FResult { 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 { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Ok(Self { year: Year::deserialize(read)?, month: Month::deserialize(read)?, @@ -189,7 +189,7 @@ impl Date { }) } - pub(crate) fn get_object_member(self, key: &Ident) -> FendCoreResult { + pub(crate) fn get_object_member(self, key: &Ident) -> FResult { Ok(match key.as_str() { "month" => Value::Month(self.month), "day_of_week" => Value::DayOfWeek(self.day_of_week()), @@ -197,7 +197,7 @@ impl Date { }) } - pub(crate) fn add(self, rhs: Value) -> FendCoreResult { + pub(crate) fn add(self, rhs: Value) -> FResult { let rhs = rhs.expect_num()?; let int = &crate::interrupt::Never; if rhs.unit_equal_to("day") { @@ -212,7 +212,7 @@ impl Date { } } - pub(crate) fn sub(self, rhs: Value) -> FendCoreResult { + pub(crate) fn sub(self, rhs: Value) -> FResult { let int = &crate::interrupt::Never; let rhs = rhs.expect_num()?; diff --git a/core/src/date/day.rs b/core/src/date/day.rs index e397813d..236fb67d 100644 --- a/core/src/date/day.rs +++ b/core/src/date/day.rs @@ -1,4 +1,4 @@ -use crate::result::FendCoreResult; +use crate::result::FResult; use crate::FendError; use crate::{Deserialize, Serialize}; use std::fmt; @@ -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 { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { let n = u8::deserialize(read)?; if n == 0 || n >= 32 { return Err(FendError::DeserializationError); diff --git a/core/src/date/day_of_week.rs b/core/src/date/day_of_week.rs index 68c0ba95..504d54df 100644 --- a/core/src/date/day_of_week.rs +++ b/core/src/date/day_of_week.rs @@ -1,6 +1,6 @@ use crate::{ error::FendError, - result::FendCoreResult, + result::FResult, serialize::{Deserialize, Serialize}, }; use std::{fmt, io}; @@ -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 { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Ok(match u8::deserialize(read)? { 0 => Self::Sunday, 1 => Self::Monday, diff --git a/core/src/date/month.rs b/core/src/date/month.rs index 98600532..aba1bc06 100644 --- a/core/src/date/month.rs +++ b/core/src/date/month.rs @@ -1,4 +1,4 @@ -use crate::result::FendCoreResult; +use crate::result::FResult; use crate::FendError; use crate::{ date::Year, @@ -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 { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Self::try_from(u8::deserialize(read)?).map_err(|_| FendError::DeserializationError) } diff --git a/core/src/date/parser.rs b/core/src/date/parser.rs index d1645868..60d431f7 100644 --- a/core/src/date/parser.rs +++ b/core/src/date/parser.rs @@ -1,7 +1,7 @@ use crate::{ date::{Date, Day, Month, Year}, error::FendError, - result::FendCoreResult, + result::FResult, }; use std::convert; @@ -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 { +pub(crate) fn parse_date(s: &str) -> FResult { let trimmed = s.trim(); if let Ok((date, remaining)) = parse_yyyymmdd(trimmed) { if remaining.is_empty() { diff --git a/core/src/date/year.rs b/core/src/date/year.rs index 7a776cb4..9ffd2164 100644 --- a/core/src/date/year.rs +++ b/core/src/date/year.rs @@ -2,7 +2,7 @@ use std::{convert, fmt, io}; use crate::{ error::FendError, - result::FendCoreResult, + result::FResult, serialize::{Deserialize, Serialize}, }; @@ -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 { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Self::try_from(i32::deserialize(read)?).map_err(|_| FendError::DeserializationError) } } diff --git a/core/src/eval.rs b/core/src/eval.rs index 8ad7fba0..80366835 100644 --- a/core/src/eval.rs +++ b/core/src/eval.rs @@ -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( @@ -10,7 +10,7 @@ pub(crate) fn evaluate_to_value( attrs: Attrs, context: &mut crate::Context, int: &I, -) -> FendCoreResult { +) -> FResult { let lex = lexer::lex(input, int); let mut tokens = vec![]; let mut missing_open_parens: i32 = 0; @@ -77,7 +77,7 @@ pub(crate) fn evaluate_to_spans( scope: Option>, context: &mut crate::Context, int: &I, -) -> FendCoreResult<(Vec, bool, Attrs)> { +) -> FResult<(Vec, 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()); diff --git a/core/src/format.rs b/core/src/format.rs index 2d9df91a..9bdfb761 100644 --- a/core/src/format.rs +++ b/core/src/format.rs @@ -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( - &self, - params: &Self::Params, - int: &I, - ) -> FendCoreResult>; + fn format(&self, params: &Self::Params, int: &I) -> FResult>; /// Simpler alternative to calling format - fn fm(&self, int: &I) -> FendCoreResult { + fn fm(&self, int: &I) -> FResult { Ok(self.format(&Default::default(), int)?.value) } } diff --git a/core/src/ident.rs b/core/src/ident.rs index b6245369..95eab276 100644 --- a/core/src/ident.rs +++ b/core/src/ident.rs @@ -1,7 +1,7 @@ use std::{borrow::Cow, fmt, io}; use crate::{ - result::FendCoreResult, + result::FResult, serialize::{Deserialize, Serialize}, }; @@ -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 { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Ok(Self(Cow::Owned(String::deserialize(read)?))) } } diff --git a/core/src/interrupt.rs b/core/src/interrupt.rs index f3feacfb..0349ad15 100644 --- a/core/src/interrupt.rs +++ b/core/src/interrupt.rs @@ -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(int: &I) -> FendCoreResult<()> { +pub(crate) fn test_int(int: &I) -> FResult<()> { if int.should_interrupt() { Err(FendError::Interrupted) } else { diff --git a/core/src/lexer.rs b/core/src/lexer.rs index 1882bc9a..a850549e 100644 --- a/core/src/lexer.rs +++ b/core/src/lexer.rs @@ -2,7 +2,7 @@ use crate::date::Date; use crate::error::{FendError, Interrupt}; use crate::ident::Ident; use crate::num::{Base, Number}; -use crate::result::FendCoreResult; +use crate::result::FResult; use std::{borrow, convert, fmt}; #[derive(Clone, Debug)] @@ -73,7 +73,7 @@ impl fmt::Display for Symbol { } } -fn parse_char(input: &str) -> FendCoreResult<(char, &str)> { +fn parse_char(input: &str) -> FResult<(char, &str)> { input .chars() .next() @@ -83,7 +83,7 @@ fn parse_char(input: &str) -> FendCoreResult<(char, &str)> { }) } -fn parse_ascii_digit(input: &str, base: Base) -> FendCoreResult<(u8, &str)> { +fn parse_ascii_digit(input: &str, base: Base) -> FResult<(u8, &str)> { let (ch, input) = parse_char(input)?; let possible_digit = ch.to_digit(base.base_as_u8().into()); possible_digit @@ -93,7 +93,7 @@ fn parse_ascii_digit(input: &str, base: Base) -> FendCoreResult<(u8, &str)> { }) } -fn parse_fixed_char(input: &str, ch: char) -> FendCoreResult<((), &str)> { +fn parse_fixed_char(input: &str, ch: char) -> FResult<((), &str)> { let (parsed_ch, input) = parse_char(input)?; if parsed_ch == ch { Ok(((), input)) @@ -102,7 +102,7 @@ fn parse_fixed_char(input: &str, ch: char) -> FendCoreResult<((), &str)> { } } -fn parse_digit_separator(input: &str) -> FendCoreResult<((), &str)> { +fn parse_digit_separator(input: &str) -> FResult<((), &str)> { let (parsed_ch, input) = parse_char(input)?; if parsed_ch == '_' || parsed_ch == ',' { Ok(((), input)) @@ -148,7 +148,7 @@ fn parse_integer<'a, E: From>( Ok(((), input)) } -fn parse_base_prefix(input: &str) -> FendCoreResult<(Base, &str)> { +fn parse_base_prefix(input: &str) -> FResult<(Base, &str)> { // 0x -> 16 // 0o -> 8 // 0b -> 2 @@ -192,7 +192,7 @@ fn parse_recurring_digits<'a, I: Interrupt>( num_nonrec_digits: usize, base: Base, int: &I, -) -> FendCoreResult<((), &'a str)> { +) -> FResult<((), &'a str)> { let original_input = input; // If there's no '(': return Ok but don't parse anything if parse_fixed_char(input, '(').is_err() { @@ -206,7 +206,7 @@ fn parse_recurring_digits<'a, I: Interrupt>( let mut recurring_number_num = Number::from(0); let mut recurring_number_den = Number::from(1); let base_as_u64 = u64::from(base.base_as_u8()); - let ((), input) = parse_integer(input, true, base, &mut |digit| -> FendCoreResult<()> { + let ((), input) = parse_integer(input, true, base, &mut |digit| -> FResult<()> { let digit_as_u64 = u64::from(digit); recurring_number_num = recurring_number_num .clone() @@ -232,7 +232,7 @@ fn parse_basic_number<'a, I: Interrupt>( mut input: &'a str, base: Base, int: &I, -) -> FendCoreResult<(Number, &'a str)> { +) -> FResult<(Number, &'a str)> { let mut is_dice_with_no_count = false; if input.starts_with('d') && base.base_as_u8() <= 10 { let mut chars = input.chars(); @@ -249,14 +249,13 @@ fn parse_basic_number<'a, I: Interrupt>( let mut is_integer = true; if parse_fixed_char(input, '.').is_err() && !is_dice_with_no_count { - let ((), remaining) = - parse_integer(input, true, base, &mut |digit| -> FendCoreResult<()> { - res = res - .clone() - .mul(base_as_u64.into(), int)? - .add(u64::from(digit).into(), int)?; - Ok(()) - })?; + let ((), remaining) = parse_integer(input, true, base, &mut |digit| -> FResult<()> { + res = res + .clone() + .mul(base_as_u64.into(), int)? + .add(u64::from(digit).into(), int)?; + Ok(()) + })?; input = remaining; } @@ -304,7 +303,7 @@ fn parse_basic_number<'a, I: Interrupt>( }; let mut face_count = 0_u32; let ((), remaining2) = - parse_integer(remaining, false, base, &mut |digit| -> FendCoreResult<()> { + parse_integer(remaining, false, base, &mut |digit| -> FResult<()> { face_count = face_count .checked_mul(base.base_as_u8().into()) .ok_or(FendError::InvalidDiceSyntax)? @@ -357,7 +356,7 @@ fn parse_basic_number<'a, I: Interrupt>( let mut exp = Number::zero_with_base(base); let base_num = Number::from(u64::from(base.base_as_u8())); let ((), remaining2) = - parse_integer(input, true, base, &mut |digit| -> FendCoreResult<()> { + parse_integer(input, true, base, &mut |digit| -> FResult<()> { exp = (exp.clone().mul(base_num.clone(), int)?) .add(u64::from(digit).into(), int)?; Ok(()) @@ -375,7 +374,7 @@ fn parse_basic_number<'a, I: Interrupt>( Ok((res, input)) } -fn parse_number<'a, I: Interrupt>(input: &'a str, int: &I) -> FendCoreResult<(Number, &'a str)> { +fn parse_number<'a, I: Interrupt>(input: &'a str, int: &I) -> FResult<(Number, &'a str)> { let (base, input) = parse_base_prefix(input).unwrap_or((Base::default(), input)); let (res, input) = parse_basic_number(input, base, int)?; Ok((res, input)) @@ -415,7 +414,7 @@ fn is_valid_in_ident(ch: char, prev: Option) -> bool { } } -fn parse_ident(input: &str, allow_dots: bool) -> FendCoreResult<(Token, &str)> { +fn parse_ident(input: &str, allow_dots: bool) -> FResult<(Token, &str)> { let (first_char, _) = parse_char(input)?; if !is_valid_in_ident(first_char, None) || first_char == '.' && !allow_dots { return Err(FendError::InvalidCharAtBeginningOfIdent(first_char)); @@ -449,7 +448,7 @@ fn parse_ident(input: &str, allow_dots: bool) -> FendCoreResult<(Token, &str)> { )) } -fn parse_symbol(ch: char, input: &mut &str) -> FendCoreResult { +fn parse_symbol(ch: char, input: &mut &str) -> FResult { let mut test_next = |next: char| { if input.starts_with(next) { let (_, remaining) = input.split_at(next.len_utf8()); @@ -506,7 +505,7 @@ fn parse_symbol(ch: char, input: &mut &str) -> FendCoreResult { })) } -fn parse_unicode_escape(chars_iter: &mut std::str::CharIndices<'_>) -> FendCoreResult { +fn parse_unicode_escape(chars_iter: &mut std::str::CharIndices<'_>) -> FResult { if chars_iter .next() .ok_or(FendError::UnterminatedStringLiteral)? @@ -545,7 +544,7 @@ fn parse_unicode_escape(chars_iter: &mut std::str::CharIndices<'_>) -> FendCoreR } } -fn parse_string_literal(input: &str, terminator: char) -> FendCoreResult<(Token, &str)> { +fn parse_string_literal(input: &str, terminator: char) -> FResult<(Token, &str)> { let (_, input) = input.split_at(1); let mut chars_iter = input.char_indices(); let mut literal_length = None; @@ -682,7 +681,7 @@ fn skip_whitespace_and_comments(input: &mut &str) { } } -fn parse_date(input: &str) -> FendCoreResult<(Date, &str)> { +fn parse_date(input: &str) -> FResult<(Date, &str)> { let (_, input) = input.split_at(1); // skip '@' symbol let mut input2 = input; let mut split_idx = 0; @@ -713,7 +712,7 @@ fn parse_date(input: &str) -> FendCoreResult<(Date, &str)> { } impl<'a, 'b, I: Interrupt> Lexer<'a, 'b, I> { - fn next_token(&mut self) -> FendCoreResult> { + fn next_token(&mut self) -> FResult> { skip_whitespace_and_comments(&mut self.input); let (ch, following) = { let mut chars = self.input.chars(); @@ -776,7 +775,7 @@ impl<'a, 'b, I: Interrupt> Lexer<'a, 'b, I> { } impl<'a, I: Interrupt> Iterator for Lexer<'a, '_, I> { - type Item = FendCoreResult; + type Item = FResult; fn next(&mut self) -> Option { let res = match self.next_token() { diff --git a/core/src/lib.rs b/core/src/lib.rs index f4edeed2..a1fb2d42 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -45,7 +45,7 @@ use std::{collections::HashMap, fmt, io}; use error::FendError; pub(crate) use eval::Attrs; pub use interrupt::Interrupt; -use result::FendCoreResult; +use result::FResult; use serialize::{Deserialize, Serialize}; /// This contains the result of a computation. @@ -282,7 +282,7 @@ impl Context { self.output_mode = OutputMode::TerminalFixedWidth; } - fn serialize_variables_internal(&self, write: &mut impl io::Write) -> FendCoreResult<()> { + fn serialize_variables_internal(&self, write: &mut impl io::Write) -> FResult<()> { self.variables.len().serialize(write)?; for (k, v) in &self.variables { k.as_str().serialize(write)?; @@ -305,7 +305,7 @@ impl Context { } } - fn deserialize_variables_internal(&mut self, read: &mut impl io::Read) -> FendCoreResult<()> { + fn deserialize_variables_internal(&mut self, read: &mut impl io::Read) -> FResult<()> { let len = usize::deserialize(read)?; self.variables.clear(); self.variables.reserve(len); diff --git a/core/src/num/base.rs b/core/src/num/base.rs index 98f2f573..13079e6a 100644 --- a/core/src/num/base.rs +++ b/core/src/num/base.rs @@ -2,7 +2,7 @@ use std::{fmt, io}; use crate::{ error::FendError, - result::FendCoreResult, + result::FResult, serialize::{Deserialize, Serialize}, }; @@ -35,7 +35,7 @@ impl Base { } } - pub(crate) const fn from_zero_based_prefix_char(ch: char) -> FendCoreResult { + pub(crate) const fn from_zero_based_prefix_char(ch: char) -> FResult { Ok(match ch { 'x' => Self(BaseEnum::Hex), 'o' => Self(BaseEnum::Octal), @@ -44,7 +44,7 @@ impl Base { }) } - pub(crate) const fn from_plain_base(base: u8) -> FendCoreResult { + pub(crate) const fn from_plain_base(base: u8) -> FResult { if base < 2 { return Err(FendError::BaseTooSmall); } else if base > 36 { @@ -53,7 +53,7 @@ impl Base { Ok(Self(BaseEnum::Plain(base))) } - pub(crate) const fn from_custom_base(base: u8) -> FendCoreResult { + pub(crate) const fn from_custom_base(base: u8) -> FResult { if base < 2 { return Err(FendError::BaseTooSmall); } else if base > 36 { @@ -119,7 +119,7 @@ impl Base { }) } - pub(crate) fn serialize(self, write: &mut impl io::Write) -> FendCoreResult<()> { + pub(crate) fn serialize(self, write: &mut impl io::Write) -> FResult<()> { match self.0 { BaseEnum::Binary => 1u8.serialize(write)?, BaseEnum::Octal => 2u8.serialize(write)?, @@ -136,7 +136,7 @@ impl Base { Ok(()) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Ok(Self(match u8::deserialize(read)? { 1 => BaseEnum::Binary, 2 => BaseEnum::Octal, diff --git a/core/src/num/bigrat.rs b/core/src/num/bigrat.rs index cc5d6ade..beb6424b 100644 --- a/core/src/num/bigrat.rs +++ b/core/src/num/bigrat.rs @@ -3,13 +3,13 @@ use crate::format::Format; use crate::interrupt::test_int; use crate::num::biguint::BigUint; use crate::num::{Base, Exact, FormattingStyle, Range, RangeBound}; -use crate::result::FendCoreResult; +use crate::result::FResult; use std::{cmp, fmt, hash, io, ops}; pub(crate) mod sign { use crate::{ error::FendError, - result::FendCoreResult, + result::FResult, serialize::{Deserialize, Serialize}, }; use std::io; @@ -39,11 +39,11 @@ pub(crate) mod sign { } } - 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) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Ok(match u8::deserialize(read)? { 1 => Self::Positive, 2 => Self::Negative, @@ -117,14 +117,14 @@ impl hash::Hash for BigRat { } impl BigRat { - pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FendCoreResult<()> { + pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FResult<()> { self.sign.serialize(write)?; self.num.serialize(write)?; self.den.serialize(write)?; Ok(()) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Ok(Self { sign: Sign::deserialize(read)?, num: BigUint::deserialize(read)?, @@ -136,7 +136,7 @@ impl BigRat { self.den == 1.into() } - pub(crate) fn try_as_usize(mut self, int: &I) -> FendCoreResult { + pub(crate) fn try_as_usize(mut self, int: &I) -> FResult { if self.sign == Sign::Negative && self.num != 0.into() { return Err(FendError::NegativeNumbersNotAllowed); } @@ -147,7 +147,7 @@ impl BigRat { self.num.try_as_usize(int) } - pub(crate) fn try_as_i64(mut self, int: &I) -> FendCoreResult { + pub(crate) fn try_as_i64(mut self, int: &I) -> FResult { self = self.simplify(int)?; if self.den != 1.into() { return Err(FendError::FractionToInteger); @@ -166,7 +166,7 @@ impl BigRat { }) } - pub(crate) fn into_f64(mut self, int: &I) -> FendCoreResult { + pub(crate) fn into_f64(mut self, int: &I) -> FResult { if self.is_definitely_zero() { return Ok(0.0); } @@ -184,7 +184,7 @@ impl BigRat { clippy::cast_sign_loss, clippy::cast_precision_loss )] - pub(crate) fn from_f64(mut f: f64, int: &I) -> FendCoreResult { + pub(crate) fn from_f64(mut f: f64, int: &I) -> FResult { let negative = f < 0.0; if negative { f = -f; @@ -205,7 +205,7 @@ impl BigRat { } // sin works for all real numbers - pub(crate) fn sin(self, int: &I) -> FendCoreResult> { + pub(crate) fn sin(self, int: &I) -> FResult> { Ok(if self == 0.into() { Exact::new(Self::from(0), true) } else { @@ -214,7 +214,7 @@ impl BigRat { } // asin, acos and atan only work for values between -1 and 1 - pub(crate) fn asin(self, int: &I) -> FendCoreResult { + pub(crate) fn asin(self, int: &I) -> FResult { let one = Self::from(1); if self > one || self < -one { return Err(out_of_range(self.fm(int)?, Range::open(-1, 1))); @@ -222,7 +222,7 @@ impl BigRat { Self::from_f64(f64::asin(self.into_f64(int)?), int) } - pub(crate) fn acos(self, int: &I) -> FendCoreResult { + pub(crate) fn acos(self, int: &I) -> FResult { let one = Self::from(1); if self > one || self < -one { return Err(out_of_range(self.fm(int)?, Range::open(-1, 1))); @@ -231,32 +231,32 @@ impl BigRat { } // note that this works for any real number, unlike asin and acos - pub(crate) fn atan(self, int: &I) -> FendCoreResult { + pub(crate) fn atan(self, int: &I) -> FResult { Self::from_f64(f64::atan(self.into_f64(int)?), int) } - pub(crate) fn atan2(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn atan2(self, rhs: Self, int: &I) -> FResult { Self::from_f64(f64::atan2(self.into_f64(int)?, rhs.into_f64(int)?), int) } - pub(crate) fn sinh(self, int: &I) -> FendCoreResult { + pub(crate) fn sinh(self, int: &I) -> FResult { Self::from_f64(f64::sinh(self.into_f64(int)?), int) } - pub(crate) fn cosh(self, int: &I) -> FendCoreResult { + pub(crate) fn cosh(self, int: &I) -> FResult { Self::from_f64(f64::cosh(self.into_f64(int)?), int) } - pub(crate) fn tanh(self, int: &I) -> FendCoreResult { + pub(crate) fn tanh(self, int: &I) -> FResult { Self::from_f64(f64::tanh(self.into_f64(int)?), int) } - pub(crate) fn asinh(self, int: &I) -> FendCoreResult { + pub(crate) fn asinh(self, int: &I) -> FResult { Self::from_f64(f64::asinh(self.into_f64(int)?), int) } // value must not be less than 1 - pub(crate) fn acosh(self, int: &I) -> FendCoreResult { + pub(crate) fn acosh(self, int: &I) -> FResult { if self < 1.into() { return Err(out_of_range( self.fm(int)?, @@ -270,7 +270,7 @@ impl BigRat { } // value must be between -1 and 1. - pub(crate) fn atanh(self, int: &I) -> FendCoreResult { + pub(crate) fn atanh(self, int: &I) -> FResult { let one: Self = 1.into(); if self >= one || self <= -one { return Err(out_of_range(self.fm(int)?, Range::open(-1, 1))); @@ -279,7 +279,7 @@ impl BigRat { } // For all logs: value must be greater than 0 - pub(crate) fn ln(self, int: &I) -> FendCoreResult> { + pub(crate) fn ln(self, int: &I) -> FResult> { if self <= 0.into() { return Err(out_of_range( self.fm(int)?, @@ -298,7 +298,7 @@ impl BigRat { )) } - pub(crate) fn log2(self, int: &I) -> FendCoreResult { + pub(crate) fn log2(self, int: &I) -> FResult { if self <= 0.into() { return Err(out_of_range( self.fm(int)?, @@ -311,7 +311,7 @@ impl BigRat { Self::from_f64(f64::log2(self.into_f64(int)?), int) } - pub(crate) fn log10(self, int: &I) -> FendCoreResult { + pub(crate) fn log10(self, int: &I) -> FResult { if self <= 0.into() { return Err(out_of_range( self.fm(int)?, @@ -326,9 +326,9 @@ impl BigRat { fn apply_uint_op( mut self, - f: impl FnOnce(BigUint, &I) -> FendCoreResult, + f: impl FnOnce(BigUint, &I) -> FResult, int: &I, - ) -> FendCoreResult { + ) -> FResult { self = self.simplify(int)?; if self.den != 1.into() { let n = self.fm(int)?; @@ -340,7 +340,7 @@ impl BigRat { f(self.num, int) } - pub(crate) fn factorial(self, int: &I) -> FendCoreResult { + pub(crate) fn factorial(self, int: &I) -> FResult { Ok(self.apply_uint_op(BigUint::factorial, int)?.into()) } @@ -349,7 +349,7 @@ impl BigRat { rhs: Self, op: crate::ast::BitwiseBop, int: &I, - ) -> FendCoreResult { + ) -> FResult { use crate::ast::BitwiseBop; Ok(self @@ -371,7 +371,7 @@ impl BigRat { } /// compute a + b - fn add_internal(self, rhs: Self, int: &I) -> FendCoreResult { + fn add_internal(self, rhs: Self, int: &I) -> FResult { // a + b == -((-a) + (-b)) if self.sign == Sign::Negative { return Ok(-((-self).add_internal(-rhs, int)?)); @@ -423,7 +423,7 @@ impl BigRat { }) } - fn simplify(mut self, int: &I) -> FendCoreResult { + fn simplify(mut self, int: &I) -> FResult { if self.den == 1.into() { return Ok(self); } @@ -433,7 +433,7 @@ impl BigRat { Ok(self) } - pub(crate) fn div(self, rhs: &Self, int: &I) -> FendCoreResult { + pub(crate) fn div(self, rhs: &Self, int: &I) -> FResult { if rhs.num == 0.into() { return Err(FendError::DivideByZero); } @@ -444,7 +444,7 @@ impl BigRat { }) } - pub(crate) fn modulo(mut self, mut rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn modulo(mut self, mut rhs: Self, int: &I) -> FResult { if rhs.num == 0.into() { return Err(FendError::ModuloByZero); } @@ -466,7 +466,7 @@ impl BigRat { // test if this fraction has a terminating representation // e.g. in base 10: 1/4 = 0.25, but not 1/3 - fn terminates_in_base(&self, base: Base, int: &I) -> FendCoreResult { + fn terminates_in_base(&self, base: Base, int: &I) -> FResult { let mut x = self.clone(); let base_as_u64: u64 = base.base_as_u8().into(); let base = Self { @@ -493,7 +493,7 @@ impl BigRat { use_parens_if_product: bool, sf_limit: Option, int: &I, - ) -> FendCoreResult> { + ) -> FResult> { let (ty, exact) = if !term.is_empty() && !base.has_prefix() && num == &1.into() { (FormattedBigRatType::Integer(None, false, term, false), true) } else { @@ -527,7 +527,7 @@ impl BigRat { mixed: bool, use_parens: bool, int: &I, - ) -> FendCoreResult> { + ) -> FResult> { let format_options = biguint::FormatOptions { base, write_base_prefix: true, @@ -595,9 +595,9 @@ impl BigRat { base: Base, sign: Sign, term: &'static str, - mut terminating: impl FnMut() -> FendCoreResult, + mut terminating: impl FnMut() -> FResult, int: &I, - ) -> FendCoreResult> { + ) -> FResult> { let integer_part = self.clone().num.div(&self.den, int)?; let sf_limit = if let FormattingStyle::SignificantFigures(sf) = style { Some(sf) @@ -689,10 +689,10 @@ impl BigRat { numerator: &BigUint, denominator: &BigUint, max_digits: MaxDigitsToPrint, - mut terminating: impl FnMut() -> FendCoreResult, - print_integer_part: impl Fn(bool) -> FendCoreResult<(Sign, String)>, + mut terminating: impl FnMut() -> FResult, + print_integer_part: impl Fn(bool) -> FResult<(Sign, String)>, int: &I, - ) -> FendCoreResult<(Sign, Exact)> { + ) -> FResult<(Sign, Exact)> { let base_as_u64: u64 = base.base_as_u8().into(); let b: BigUint = base_as_u64.into(); let next_digit = @@ -711,7 +711,7 @@ impl BigRat { let next_num = bnum.sub(&digit.clone().mul(denominator, int)?); Ok((next_num, digit)) }; - let fold_digits = |mut s: String, digit: BigUint| -> FendCoreResult { + let fold_digits = |mut s: String, digit: BigUint| -> FResult { let digit_str = digit .format( &biguint::FormatOptions { @@ -771,9 +771,9 @@ impl BigRat { base: Base, ignore_number_of_leading_zeroes: bool, mut next_digit: impl FnMut(usize, BigUint, &BigUint) -> Result<(BigUint, BigUint), NextDigitErr>, - print_integer_part: impl Fn(bool) -> FendCoreResult<(Sign, String)>, + print_integer_part: impl Fn(bool) -> FResult<(Sign, String)>, int: &I, - ) -> FendCoreResult<(Sign, Exact)> { + ) -> FResult<(Sign, Exact)> { let mut current_numerator = numerator.clone(); let mut i = 0; let mut trailing_zeroes = 0; @@ -896,11 +896,7 @@ impl BigRat { Ok((lam, mu, collected_res)) } - pub(crate) fn pow( - mut self, - mut rhs: Self, - int: &I, - ) -> FendCoreResult> { + pub(crate) fn pow(mut self, mut rhs: Self, int: &I) -> FResult> { self = self.simplify(int)?; rhs = rhs.simplify(int)?; if self.num != 0.into() && self.sign == Sign::Negative && rhs.den != 1.into() { @@ -945,7 +941,7 @@ impl BigRat { val: &Self, n: &Self, int: &I, - ) -> FendCoreResult { + ) -> FResult { let mut high_bound = low_bound.clone().add(1.into(), int)?; for _ in 0..30 { let guess = low_bound @@ -961,7 +957,7 @@ impl BigRat { low_bound.add(high_bound, int)?.div(&2.into(), int) } - pub(crate) fn exp(self, int: &I) -> FendCoreResult> { + pub(crate) fn exp(self, int: &I) -> FResult> { if self.num == 0.into() { return Ok(Exact::new(Self::from(1), true)); } @@ -973,7 +969,7 @@ impl BigRat { // the boolean indicates whether or not the result is exact // n must be an integer - pub(crate) fn root_n(self, n: &Self, int: &I) -> FendCoreResult> { + pub(crate) fn root_n(self, n: &Self, int: &I) -> FResult> { if self.num != 0.into() && self.sign == Sign::Negative { return Err(FendError::RootsOfNegativeNumbers); } @@ -1021,7 +1017,7 @@ impl BigRat { Ok(Exact::new(num_rat.div(&den_rat, int)?, false)) } - pub(crate) fn mul(self, rhs: &Self, int: &I) -> FendCoreResult { + pub(crate) fn mul(self, rhs: &Self, int: &I) -> FResult { Ok(Self { sign: Sign::sign_of_product(self.sign, rhs.sign), num: self.num.mul(&rhs.num, int)?, @@ -1029,7 +1025,7 @@ impl BigRat { }) } - pub(crate) fn add(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn add(self, rhs: Self, int: &I) -> FResult { self.add_internal(rhs, int) } @@ -1041,7 +1037,7 @@ impl BigRat { self.sign == Sign::Positive && self.num.is_definitely_one() && self.den.is_definitely_one() } - pub(crate) fn combination(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn combination(self, rhs: Self, int: &I) -> FResult { let n_factorial = self.clone().factorial(int)?; let r_factorial = rhs.clone().factorial(int)?; let n_minus_r_factorial = self.add(-rhs, int)?.factorial(int)?; @@ -1049,7 +1045,7 @@ impl BigRat { n_factorial.div(&denominator, int) } - pub(crate) fn permutation(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn permutation(self, rhs: Self, int: &I) -> FResult { let n_factorial = self.clone().factorial(int)?; let n_minus_r_factorial = self.add(-rhs, int)?.factorial(int)?; n_factorial.div(&n_minus_r_factorial, int) @@ -1122,11 +1118,7 @@ impl Format for BigRat { // Formats as an integer if possible, or a terminating float, otherwise as // either a fraction or a potentially approximated floating-point number. // The result 'exact' field indicates whether the number was exact or not. - fn format( - &self, - params: &Self::Params, - int: &I, - ) -> FendCoreResult> { + fn format(&self, params: &Self::Params, int: &I) -> FResult> { let base = params.base; let style = params.style; let term = params.term; @@ -1279,7 +1271,7 @@ mod tests { use super::BigRat; use crate::num::biguint::BigUint; - use crate::result::FendCoreResult; + use crate::result::FResult; use std::mem; #[test] @@ -1291,7 +1283,7 @@ mod tests { } #[test] - fn test_addition() -> FendCoreResult<()> { + fn test_addition() -> FResult<()> { let int = &crate::interrupt::Never; let two = BigRat::from(2); assert_eq!(two.clone().add(two, int)?, BigRat::from(4)); diff --git a/core/src/num/biguint.rs b/core/src/num/biguint.rs index 6a21658c..4c84340b 100644 --- a/core/src/num/biguint.rs +++ b/core/src/num/biguint.rs @@ -2,7 +2,7 @@ use crate::error::{FendError, Interrupt}; use crate::format::Format; use crate::interrupt::test_int; use crate::num::{out_of_range, Base, Exact, Range, RangeBound}; -use crate::result::FendCoreResult; +use crate::result::FResult; use crate::serialize::{Deserialize, Serialize}; use std::cmp::{max, Ordering}; use std::{fmt, hash, io}; @@ -68,8 +68,8 @@ impl BigUint { } } - pub(crate) fn try_as_usize(&self, int: &I) -> FendCoreResult { - let error = || -> FendCoreResult<_> { + pub(crate) fn try_as_usize(&self, int: &I) -> FResult { + let error = || -> FResult<_> { Ok(out_of_range( self.fm(int)?, Range { @@ -165,7 +165,7 @@ impl BigUint { } } - pub(crate) fn gcd(mut a: Self, mut b: Self, int: &I) -> FendCoreResult { + pub(crate) fn gcd(mut a: Self, mut b: Self, int: &I) -> FResult { while b >= 1.into() { let r = a.rem(&b, int)?; a = b; @@ -175,7 +175,7 @@ impl BigUint { Ok(a) } - pub(crate) fn pow(a: &Self, b: &Self, int: &I) -> FendCoreResult { + pub(crate) fn pow(a: &Self, b: &Self, int: &I) -> FResult { if a.is_zero() && b.is_zero() { return Err(FendError::ZeroToThePowerOfZero); } @@ -190,7 +190,7 @@ impl BigUint { // computes the exact `n`-th root if possible, otherwise the next lower integer #[allow(clippy::redundant_clone)] - pub(crate) fn root_n(self, n: &Self, int: &I) -> FendCoreResult> { + pub(crate) fn root_n(self, n: &Self, int: &I) -> FResult> { if self == 0.into() || self == 1.into() || n == &Self::from(1) { return Ok(Exact::new(self, true)); } @@ -211,7 +211,7 @@ impl BigUint { Ok(Exact::new(low_guess, false)) } - fn pow_internal(&self, mut exponent: u64, int: &I) -> FendCoreResult { + fn pow_internal(&self, mut exponent: u64, int: &I) -> FResult { let mut result = Self::from(1); let mut base = self.clone(); while exponent > 0 { @@ -225,7 +225,7 @@ impl BigUint { Ok(result) } - fn lshift(&mut self, int: &I) -> FendCoreResult<()> { + fn lshift(&mut self, int: &I) -> FResult<()> { match self { Small(n) => { if *n & 0xc000_0000_0000_0000 == 0 { @@ -250,7 +250,7 @@ impl BigUint { Ok(()) } - fn rshift(&mut self, int: &I) -> FendCoreResult<()> { + fn rshift(&mut self, int: &I) -> FResult<()> { match self { Small(n) => *n >>= 1, Large(value) => { @@ -269,11 +269,7 @@ impl BigUint { Ok(()) } - pub(crate) fn divmod( - &self, - other: &Self, - int: &I, - ) -> FendCoreResult<(Self, Self)> { + pub(crate) fn divmod(&self, other: &Self, int: &I) -> FResult<(Self, Self)> { if let (Small(a), Small(b)) = (self, other) { if let (Some(div_res), Some(mod_res)) = (a.checked_div(*b), a.checked_rem(*b)) { return Ok((Small(div_res), Small(mod_res))); @@ -320,7 +316,7 @@ impl BigUint { } /// computes self *= other - fn mul_internal(&mut self, other: &Self, int: &I) -> FendCoreResult<()> { + fn mul_internal(&mut self, other: &Self, int: &I) -> FResult<()> { if self.is_zero() || other.is_zero() { *self = Self::from(0); return Ok(()); @@ -357,7 +353,7 @@ impl BigUint { } // Note: 0! = 1, 1! = 1 - pub(crate) fn factorial(mut self, int: &I) -> FendCoreResult { + pub(crate) fn factorial(mut self, int: &I) -> FResult { let mut res = Self::from(1); while self > 1.into() { test_int(int)?; @@ -367,7 +363,7 @@ impl BigUint { Ok(res) } - pub(crate) fn mul(mut self, other: &Self, int: &I) -> FendCoreResult { + pub(crate) fn mul(mut self, other: &Self, int: &I) -> FResult { if let (Small(a), Small(b)) = (&self, &other) { if let Some(res) = a.checked_mul(*b) { return Ok(Self::from(res)); @@ -377,15 +373,15 @@ impl BigUint { Ok(self) } - fn rem(&self, other: &Self, int: &I) -> FendCoreResult { + fn rem(&self, other: &Self, int: &I) -> FResult { Ok(self.divmod(other, int)?.1) } - pub(crate) fn is_even(&self, int: &I) -> FendCoreResult { + pub(crate) fn is_even(&self, int: &I) -> FResult { Ok(self.divmod(&Self::from(2), int)?.1 == 0.into()) } - pub(crate) fn div(self, other: &Self, int: &I) -> FendCoreResult { + pub(crate) fn div(self, other: &Self, int: &I) -> FResult { Ok(self.divmod(other, int)?.0) } @@ -444,7 +440,7 @@ impl BigUint { } } - pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FendCoreResult<()> { + pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FResult<()> { match self { Small(x) => { 1u8.serialize(write)?; @@ -461,7 +457,7 @@ impl BigUint { Ok(()) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { let kind = u8::deserialize(read)?; Ok(match kind { 1 => Self::Small(u64::deserialize(read)?), @@ -540,7 +536,7 @@ impl BigUint { } } - pub(crate) fn lshift_n(mut self, rhs: &Self, int: &I) -> FendCoreResult { + pub(crate) fn lshift_n(mut self, rhs: &Self, int: &I) -> FResult { let mut rhs = rhs.try_as_usize(int)?; if rhs > 64 { self.make_large(); @@ -560,7 +556,7 @@ impl BigUint { Ok(self) } - pub(crate) fn rshift_n(mut self, rhs: &Self, int: &I) -> FendCoreResult { + pub(crate) fn rshift_n(mut self, rhs: &Self, int: &I) -> FResult { let rhs = rhs.try_as_usize(int)?; for _ in 0..rhs { if self.is_zero() { @@ -645,11 +641,7 @@ impl Format for BigUint { type Params = FormatOptions; type Out = FormattedBigUint; - fn format( - &self, - params: &Self::Params, - int: &I, - ) -> FendCoreResult> { + fn format(&self, params: &Self::Params, int: &I) -> FResult> { let base_prefix = if params.write_base_prefix { Some(params.base) } else { diff --git a/core/src/num/complex.rs b/core/src/num/complex.rs index 8bbd8580..f91d3953 100644 --- a/core/src/num/complex.rs +++ b/core/src/num/complex.rs @@ -2,7 +2,7 @@ use crate::error::{FendError, Interrupt}; use crate::num::real::{self, Real}; use crate::num::Exact; use crate::num::{Base, FormattingStyle}; -use crate::result::FendCoreResult; +use crate::result::FResult; use std::cmp::Ordering; use std::ops::Neg; use std::{fmt, io}; @@ -31,27 +31,27 @@ pub(crate) enum UseParentheses { } impl Complex { - pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FendCoreResult<()> { + pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FResult<()> { self.real.serialize(write)?; self.imag.serialize(write)?; Ok(()) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Ok(Self { real: Real::deserialize(read)?, imag: Real::deserialize(read)?, }) } - pub(crate) fn try_as_usize(self, int: &I) -> FendCoreResult { + pub(crate) fn try_as_usize(self, int: &I) -> FResult { if self.imag != 0.into() { return Err(FendError::ComplexToInteger); } self.real.try_as_usize(int) } - pub(crate) fn try_as_i64(self, int: &I) -> FendCoreResult { + pub(crate) fn try_as_i64(self, int: &I) -> FResult { if self.imag != 0.into() { return Err(FendError::ComplexToInteger); } @@ -75,7 +75,7 @@ impl Complex { } } - pub(crate) fn factorial(self, int: &I) -> FendCoreResult { + pub(crate) fn factorial(self, int: &I) -> FResult { if self.imag != 0.into() { return Err(FendError::FactorialComplex); } @@ -85,7 +85,7 @@ impl Complex { }) } - pub(crate) fn exp(self, int: &I) -> FendCoreResult> { + pub(crate) fn exp(self, int: &I) -> FResult> { // e^(a + bi) = e^a * e^(bi) = e^a * (cos(b) + i * sin(b)) let r = self.real.exp(int)?; let exact = r.exact; @@ -99,7 +99,7 @@ impl Complex { )) } - fn pow_n(self, n: usize, int: &I) -> FendCoreResult> { + fn pow_n(self, n: usize, int: &I) -> FResult> { if n == 0 { return Ok(Exact::new(Self::from(1), true)); } @@ -118,7 +118,7 @@ impl Complex { Ok(result) } - pub(crate) fn pow(self, rhs: Self, int: &I) -> FendCoreResult> { + pub(crate) fn pow(self, rhs: Self, int: &I) -> FResult> { if !rhs.real.is_integer() || !rhs.imag.is_integer() { return self.frac_pow(rhs, int); } @@ -191,7 +191,7 @@ impl Complex { } } - pub(crate) fn abs(self, int: &I) -> FendCoreResult> { + pub(crate) fn abs(self, int: &I) -> FResult> { Ok(if self.imag.is_zero() { if self.real < 0.into() { Exact::new(-self.real, true) @@ -213,7 +213,7 @@ impl Complex { }) } - pub(crate) fn arg(self, int: &I) -> FendCoreResult> { + pub(crate) fn arg(self, int: &I) -> FResult> { Ok(Exact::new(self.imag.atan2(self.real, int)?, false)) } @@ -224,7 +224,7 @@ impl Complex { base: Base, use_parentheses: UseParentheses, int: &I, - ) -> FendCoreResult> { + ) -> FResult> { let style = if !exact && style == FormattingStyle::Auto { FormattingStyle::DecimalPlaces(10) } else if self.imag != 0.into() && style == FormattingStyle::Auto { @@ -285,7 +285,7 @@ impl Complex { ) }) } - pub(crate) fn frac_pow(self, n: Self, int: &I) -> FendCoreResult> { + pub(crate) fn frac_pow(self, n: Self, int: &I) -> FResult> { if self.imag.is_zero() && n.imag.is_zero() && self.real >= 0.into() { Ok(self.real.pow(n.real, int)?.apply(Self::from)) } else { @@ -294,7 +294,7 @@ impl Complex { } } - fn expect_real(self) -> FendCoreResult { + fn expect_real(self) -> FResult { if self.imag.is_zero() { Ok(self.real) } else { @@ -302,7 +302,7 @@ impl Complex { } } - pub(crate) fn sin(self, int: &I) -> FendCoreResult> { + pub(crate) fn sin(self, int: &I) -> FResult> { // sin(a + bi) = sin(a) * cosh(b) + i * cos(a) * sinh(b) if self.imag.is_zero() { Ok(self.real.sin(int)?.apply(Self::from)) @@ -323,7 +323,7 @@ impl Complex { } } - pub(crate) fn cos(self, int: &I) -> FendCoreResult> { + pub(crate) fn cos(self, int: &I) -> FResult> { // cos(a + bi) = cos(a) * cosh(b) - i * sin(a) * sinh(b) if self.imag.is_zero() { Ok(self.real.cos(int)?.apply(Self::from)) @@ -344,7 +344,7 @@ impl Complex { } } - pub(crate) fn tan(self, int: &I) -> FendCoreResult> { + pub(crate) fn tan(self, int: &I) -> FResult> { let num = self.clone().sin(int)?; let den = self.cos(int)?; num.div(den, int) @@ -352,7 +352,7 @@ impl Complex { /// Calculates ln(i * z + sqrt(1 - z^2)) /// This is used to implement asin and acos for all complex numbers - fn asin_ln(self, int: &I) -> FendCoreResult> { + fn asin_ln(self, int: &I) -> FResult> { let half = Exact::new(Self::from(1), true).div(Exact::new(Self::from(2), true), int)?; let exact = Exact::new(self, true); let i = Exact::new(Self::i(), true); @@ -366,7 +366,7 @@ impl Complex { .try_and_then(|x| x.ln(int)) } - pub(crate) fn asin(self, int: &I) -> FendCoreResult { + pub(crate) fn asin(self, int: &I) -> FResult { // Real asin is defined for -1 <= x <= 1 if self.imag.is_zero() && Real::from(1).neg() < self.real && self.real < 1.into() { Ok(Self::from(self.real.asin(int)?)) @@ -380,7 +380,7 @@ impl Complex { } } - pub(crate) fn acos(self, int: &I) -> FendCoreResult { + pub(crate) fn acos(self, int: &I) -> FResult { // Real acos is defined for -1 <= x <= 1 if self.imag.is_zero() && Real::from(1).neg() < self.real && self.real < 1.into() { Ok(Self::from(self.real.acos(int)?)) @@ -396,7 +396,7 @@ impl Complex { } } - pub(crate) fn atan(self, int: &I) -> FendCoreResult { + pub(crate) fn atan(self, int: &I) -> FResult { // Real atan is defined for all real numbers if self.imag.is_zero() { Ok(Self::from(self.real.atan(int)?)) @@ -420,7 +420,7 @@ impl Complex { } } - pub(crate) fn sinh(self, int: &I) -> FendCoreResult { + pub(crate) fn sinh(self, int: &I) -> FResult { if self.imag.is_zero() { Ok(Self::from(self.real.sinh(int)?)) } else { @@ -437,7 +437,7 @@ impl Complex { } } - pub(crate) fn cosh(self, int: &I) -> FendCoreResult { + pub(crate) fn cosh(self, int: &I) -> FResult { if self.imag.is_zero() { Ok(Self::from(self.real.cosh(int)?)) } else { @@ -454,7 +454,7 @@ impl Complex { } } - pub(crate) fn tanh(self, int: &I) -> FendCoreResult { + pub(crate) fn tanh(self, int: &I) -> FResult { if self.imag.is_zero() { Ok(Self::from(self.real.tanh(int)?)) } else { @@ -465,7 +465,7 @@ impl Complex { } } - pub(crate) fn asinh(self, int: &I) -> FendCoreResult { + pub(crate) fn asinh(self, int: &I) -> FResult { // Real asinh is defined for all real numbers if self.imag.is_zero() { Ok(Self::from(self.real.asinh(int)?)) @@ -484,7 +484,7 @@ impl Complex { } } - pub(crate) fn acosh(self, int: &I) -> FendCoreResult { + pub(crate) fn acosh(self, int: &I) -> FResult { // Real acosh is defined for x >= 1 if self.imag.is_zero() && self.real >= 1.into() { Ok(Self::from(self.real.acosh(int)?)) @@ -503,7 +503,7 @@ impl Complex { } } - pub(crate) fn atanh(self, int: &I) -> FendCoreResult { + pub(crate) fn atanh(self, int: &I) -> FResult { // Real atanh is defined for -1 < x < 1 // Undefined for x = 1, -1 if self.imag.is_zero() && Real::from(1).neg() <= self.real && self.real <= 1.into() { @@ -525,7 +525,7 @@ impl Complex { } } - pub(crate) fn ln(self, int: &I) -> FendCoreResult> { + pub(crate) fn ln(self, int: &I) -> FResult> { if self.imag.is_zero() && self.real > 0.into() { Ok(self.real.ln(int)?.apply(Self::from)) } else { @@ -544,21 +544,21 @@ impl Complex { } } - pub(crate) fn log(self, base: Self, int: &I) -> FendCoreResult { + pub(crate) fn log(self, base: Self, int: &I) -> FResult { // log_n(z) = ln(z) / ln(n) let ln = self.ln(int)?; let ln2 = base.ln(int)?; Ok(ln.div(ln2, int)?.value) } - pub(crate) fn log2(self, int: &I) -> FendCoreResult { + pub(crate) fn log2(self, int: &I) -> FResult { if self.imag.is_zero() && self.real > 0.into() { Ok(Self::from(self.real.log2(int)?)) } else { self.log(Self::from(2), int) } } - pub(crate) fn log10(self, int: &I) -> FendCoreResult { + pub(crate) fn log10(self, int: &I) -> FResult { if self.imag.is_zero() && self.real > 0.into() { Ok(Self::from(self.real.log10(int)?)) } else { @@ -570,7 +570,7 @@ impl Complex { self.real.is_definitely_one() && self.imag.is_definitely_zero() } - pub(crate) fn modulo(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn modulo(self, rhs: Self, int: &I) -> FResult { Ok(Self::from( self.expect_real()?.modulo(rhs.expect_real()?, int)?, )) @@ -581,7 +581,7 @@ impl Complex { rhs: Self, op: crate::ast::BitwiseBop, int: &I, - ) -> FendCoreResult { + ) -> FResult { Ok(Self::from(self.expect_real()?.bitwise( rhs.expect_real()?, op, @@ -589,13 +589,13 @@ impl Complex { )?)) } - pub(crate) fn combination(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn combination(self, rhs: Self, int: &I) -> FResult { Ok(Self::from( self.expect_real()?.combination(rhs.expect_real()?, int)?, )) } - pub(crate) fn permutation(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn permutation(self, rhs: Self, int: &I) -> FResult { Ok(Self::from( self.expect_real()?.permutation(rhs.expect_real()?, int)?, )) @@ -603,7 +603,7 @@ impl Complex { } impl Exact { - pub(crate) fn add(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn add(self, rhs: Self, int: &I) -> FResult { let (self_real, self_imag) = self.apply(|x| (x.real, x.imag)).pair(); let (rhs_real, rhs_imag) = rhs.apply(|x| (x.real, x.imag)).pair(); let real = self_real.add(rhs_real, int)?; @@ -617,7 +617,7 @@ impl Exact { )) } - pub(crate) fn mul(self, rhs: &Self, int: &I) -> FendCoreResult { + pub(crate) fn mul(self, rhs: &Self, int: &I) -> FResult { // (a + bi) * (c + di) // => ac + bci + adi - bd // => (ac - bd) + (bc + ad)i @@ -639,7 +639,7 @@ impl Exact { )) } - pub(crate) fn div(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn div(self, rhs: Self, int: &I) -> FResult { // (u + vi) / (x + yi) = (1/(x^2 + y^2)) * ((ux + vy) + (vx - uy)i) let (u, v) = self.apply(|x| (x.real, x.imag)).pair(); let (x, y) = rhs.apply(|x| (x.real, x.imag)).pair(); diff --git a/core/src/num/continued_fraction.rs b/core/src/num/continued_fraction.rs index b981a04c..59d8586b 100644 --- a/core/src/num/continued_fraction.rs +++ b/core/src/num/continued_fraction.rs @@ -10,7 +10,7 @@ use crate::format::Format; use crate::interrupt::Never; use crate::num::bigrat::sign::Sign; use crate::num::biguint::BigUint; -use crate::result::FendCoreResult; +use crate::result::FResult; use crate::Interrupt; use std::hash::Hash; use std::rc::Rc; @@ -43,7 +43,7 @@ impl ContinuedFraction { } } - pub(crate) fn try_as_usize(&self, int: &I) -> FendCoreResult { + pub(crate) fn try_as_usize(&self, int: &I) -> FResult { if self.actual_integer_sign() == Sign::Negative { return Err(FendError::NegativeNumbersNotAllowed); } @@ -97,7 +97,7 @@ impl ContinuedFraction { self.integer == 0.into() && (self.fraction)().next().is_none() } - pub(crate) fn invert(self) -> FendCoreResult { + pub(crate) fn invert(self) -> FResult { if self.actual_integer_sign() == Sign::Negative { return Err(FendError::NegativeNumbersNotAllowed); } @@ -127,7 +127,7 @@ impl ContinuedFraction { args: [impl Into; 4], f: fn() -> Option, int: &I, - ) -> FendCoreResult { + ) -> FResult { if self.actual_integer_sign() == Sign::Negative { return Err(FendError::NegativeNumbersNotAllowed); } @@ -162,11 +162,7 @@ impl ContinuedFraction { } // (axy + bx + cy + d) / (exy + fx + gy + h) - pub(crate) fn bihomographic( - x: Self, - y: Self, - args: [impl Into; 8], - ) -> FendCoreResult { + pub(crate) fn bihomographic(x: Self, y: Self, args: [impl Into; 8]) -> FResult { if x.actual_integer_sign() == Sign::Negative || y.actual_integer_sign() == Sign::Negative { return Err(FendError::NegativeNumbersNotAllowed); } @@ -209,22 +205,22 @@ impl ContinuedFraction { }) } - pub(crate) fn add(&self, other: &Self, int: &I) -> FendCoreResult { + pub(crate) fn add(&self, other: &Self, int: &I) -> FResult { Self::bihomographic(self.clone(), other.clone(), [0, 1, 1, 0, 0, 0, 0, 1]) } - pub(crate) fn mul(&self, other: &Self, int: &I) -> FendCoreResult { + pub(crate) fn mul(&self, other: &Self, int: &I) -> FResult { Self::bihomographic(self.clone(), other.clone(), [1, 0, 0, 0, 0, 0, 0, 1]) } - pub(crate) fn div(&self, other: &Self, int: &I) -> FendCoreResult { + pub(crate) fn div(&self, other: &Self, int: &I) -> FResult { if other.is_zero() { return Err(FendError::DivideByZero); } self.clone().invert()?.mul(&other.clone().invert()?, int) } - pub(crate) fn modulo(&self, other: &Self, int: &I) -> FendCoreResult { + pub(crate) fn modulo(&self, other: &Self, int: &I) -> FResult { if other.is_zero() { return Err(FendError::ModuloByZero); } @@ -238,14 +234,14 @@ impl ContinuedFraction { Ok(Self::from(self.integer.divmod(&other.integer, int)?.1)) } - pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FendCoreResult<()> { + pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FResult<()> { self.integer_sign.serialize(write)?; self.integer.serialize(write)?; // TODO serialize fraction Ok(()) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Ok(Self { integer_sign: Sign::deserialize(read)?, integer: BigUint::deserialize(read)?, @@ -662,7 +658,7 @@ fn format_as_integer( use_parens_if_product: bool, sf_limit: Option, int: &I, -) -> FendCoreResult> { +) -> FResult> { let (ty, exact) = if !term.is_empty() && !base.has_prefix() && num == &1.into() { ( FormattedContinuedFractionType::Integer(None, false, term, false), @@ -695,11 +691,7 @@ impl Format for ContinuedFraction { type Params = FormatOptions; type Out = FormattedContinuedFraction; - fn format( - &self, - params: &Self::Params, - int: &I, - ) -> FendCoreResult> { + fn format(&self, params: &Self::Params, int: &I) -> FResult> { let sign = self.actual_integer_sign(); // try as integer if possible diff --git a/core/src/num/dist.rs b/core/src/num/dist.rs index 0e91de32..902e44d9 100644 --- a/core/src/num/dist.rs +++ b/core/src/num/dist.rs @@ -2,7 +2,7 @@ use crate::error::{FendError, Interrupt}; use crate::interrupt::test_int; use crate::num::bigrat::BigRat; use crate::num::complex::{self, Complex}; -use crate::result::FendCoreResult; +use crate::result::FResult; use crate::serialize::{Deserialize, Serialize}; use std::cmp::Ordering; use std::collections::HashMap; @@ -20,7 +20,7 @@ pub(crate) struct Dist { } impl Dist { - pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FendCoreResult<()> { + pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FResult<()> { self.parts.len().serialize(write)?; for (a, b) in &self.parts { a.serialize(write)?; @@ -29,7 +29,7 @@ impl Dist { Ok(()) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { let len = usize::deserialize(read)?; let mut hashmap = HashMap::with_capacity(len); for _ in 0..len { @@ -40,7 +40,7 @@ impl Dist { Ok(Self { parts: hashmap }) } - pub(crate) fn one_point(self) -> FendCoreResult { + pub(crate) fn one_point(self) -> FResult { if self.parts.len() == 1 { Ok(self.parts.into_iter().next().unwrap().0) } else { @@ -48,7 +48,7 @@ impl Dist { } } - pub(crate) fn one_point_ref(&self) -> FendCoreResult<&Complex> { + pub(crate) fn one_point_ref(&self) -> FResult<&Complex> { if self.parts.len() == 1 { Ok(self.parts.iter().next().unwrap().0) } else { @@ -56,7 +56,7 @@ impl Dist { } } - pub(crate) fn new_die(count: u32, faces: u32, int: &I) -> FendCoreResult { + pub(crate) fn new_die(count: u32, faces: u32, int: &I) -> FResult { assert!(count != 0); assert!(faces != 0); if count > 1 { @@ -83,11 +83,7 @@ impl Dist { } #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] - pub(crate) fn sample( - self, - ctx: &crate::Context, - int: &I, - ) -> FendCoreResult { + pub(crate) fn sample(self, ctx: &crate::Context, int: &I) -> FResult { if self.parts.len() == 1 { return Ok(self); } @@ -117,7 +113,7 @@ impl Dist { out: &mut String, ctx: &crate::Context, int: &I, - ) -> FendCoreResult> { + ) -> FResult> { if self.parts.len() == 1 { let res = self.parts.iter().next().unwrap().0.format( exact, @@ -181,9 +177,9 @@ impl Dist { fn bop( self, rhs: &Self, - mut f: impl FnMut(&Complex, &Complex, &I) -> FendCoreResult, + mut f: impl FnMut(&Complex, &Complex, &I) -> FResult, int: &I, - ) -> FendCoreResult { + ) -> FResult { let mut result = HashMap::::new(); for (n1, p1) in &self.parts { for (n2, p2) in &rhs.parts { @@ -201,7 +197,7 @@ impl Dist { } impl Exact { - pub(crate) fn add(self, rhs: &Self, int: &I) -> FendCoreResult { + pub(crate) fn add(self, rhs: &Self, int: &I) -> FResult { let self_exact = self.exact; let rhs_exact = rhs.exact; let mut exact = true; @@ -220,7 +216,7 @@ impl Exact { )) } - pub(crate) fn mul(self, rhs: &Self, int: &I) -> FendCoreResult { + pub(crate) fn mul(self, rhs: &Self, int: &I) -> FResult { let self_exact = self.exact; let rhs_exact = rhs.exact; let mut exact = true; @@ -239,7 +235,7 @@ impl Exact { )) } - pub(crate) fn div(self, rhs: &Self, int: &I) -> FendCoreResult { + pub(crate) fn div(self, rhs: &Self, int: &I) -> FResult { let self_exact = self.exact; let rhs_exact = rhs.exact; let mut exact = true; diff --git a/core/src/num/formatting_style.rs b/core/src/num/formatting_style.rs index 45638c2f..4b530359 100644 --- a/core/src/num/formatting_style.rs +++ b/core/src/num/formatting_style.rs @@ -2,7 +2,7 @@ use std::{fmt, io}; use crate::{ error::FendError, - result::FendCoreResult, + result::FResult, serialize::{Deserialize, Serialize}, }; @@ -58,7 +58,7 @@ impl fmt::Debug for FormattingStyle { } impl FormattingStyle { - 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::ImproperFraction => 1u8.serialize(write)?, Self::MixedFraction => 2u8.serialize(write)?, @@ -77,7 +77,7 @@ impl FormattingStyle { Ok(()) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Ok(match u8::deserialize(read)? { 1 => Self::ImproperFraction, 2 => Self::MixedFraction, diff --git a/core/src/num/real.rs b/core/src/num/real.rs index 45c64640..7c0afdf8 100644 --- a/core/src/num/real.rs +++ b/core/src/num/real.rs @@ -3,7 +3,7 @@ use crate::format::Format; use crate::num::bigrat::{BigRat, FormattedBigRat}; use crate::num::Exact; use crate::num::{Base, FormattingStyle}; -use crate::result::FendCoreResult; +use crate::result::FResult; use crate::serialize::{Deserialize, Serialize}; use std::cmp::Ordering; use std::ops::Neg; @@ -76,7 +76,7 @@ impl hash::Hash for Real { } impl Real { - pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FendCoreResult<()> { + pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FResult<()> { match &self.pattern { Pattern::Simple(s) => { 1u8.serialize(write)?; @@ -90,7 +90,7 @@ impl Real { Ok(()) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Ok(Self { pattern: match u8::deserialize(read)? { 1 => Pattern::Simple(BigRat::deserialize(read)?), @@ -107,7 +107,7 @@ impl Real { } } - fn approximate(self, int: &I) -> FendCoreResult { + fn approximate(self, int: &I) -> FResult { match self.pattern { Pattern::Simple(s) => Ok(s), Pattern::Pi(n) => { @@ -119,7 +119,7 @@ impl Real { } } - pub(crate) fn try_as_i64(self, int: &I) -> FendCoreResult { + pub(crate) fn try_as_i64(self, int: &I) -> FResult { match self.pattern { Pattern::Simple(s) => s.try_as_i64(int), Pattern::Pi(n) => { @@ -132,7 +132,7 @@ impl Real { } } - pub(crate) fn try_as_usize(self, int: &I) -> FendCoreResult { + pub(crate) fn try_as_usize(self, int: &I) -> FResult { match self.pattern { Pattern::Simple(s) => s.try_as_usize(int), Pattern::Pi(n) => { @@ -146,7 +146,7 @@ impl Real { } // sin works for all real numbers - pub(crate) fn sin(self, int: &I) -> FendCoreResult> { + pub(crate) fn sin(self, int: &I) -> FResult> { Ok(match self.pattern { Pattern::Simple(s) => s.sin(int)?.apply(Self::from), Pattern::Pi(n) => { @@ -181,68 +181,68 @@ impl Real { }) } - pub(crate) fn cos(self, int: &I) -> FendCoreResult> { + pub(crate) fn cos(self, int: &I) -> FResult> { // cos(x) = sin(x + pi/2) let half_pi = Exact::new(Self::pi(), true).div(&Exact::new(Self::from(2), true), int)?; Exact::new(self, true).add(half_pi, int)?.value.sin(int) } - pub(crate) fn asin(self, int: &I) -> FendCoreResult { + pub(crate) fn asin(self, int: &I) -> FResult { Ok(Self::from(self.approximate(int)?.asin(int)?)) } - pub(crate) fn acos(self, int: &I) -> FendCoreResult { + pub(crate) fn acos(self, int: &I) -> FResult { Ok(Self::from(self.approximate(int)?.acos(int)?)) } - pub(crate) fn atan(self, int: &I) -> FendCoreResult { + pub(crate) fn atan(self, int: &I) -> FResult { Ok(Self::from(self.approximate(int)?.atan(int)?)) } - pub(crate) fn atan2(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn atan2(self, rhs: Self, int: &I) -> FResult { Ok(Self::from( self.approximate(int)?.atan2(rhs.approximate(int)?, int)?, )) } - pub(crate) fn sinh(self, int: &I) -> FendCoreResult { + pub(crate) fn sinh(self, int: &I) -> FResult { Ok(Self::from(self.approximate(int)?.sinh(int)?)) } - pub(crate) fn cosh(self, int: &I) -> FendCoreResult { + pub(crate) fn cosh(self, int: &I) -> FResult { Ok(Self::from(self.approximate(int)?.cosh(int)?)) } - pub(crate) fn tanh(self, int: &I) -> FendCoreResult { + pub(crate) fn tanh(self, int: &I) -> FResult { Ok(Self::from(self.approximate(int)?.tanh(int)?)) } - pub(crate) fn asinh(self, int: &I) -> FendCoreResult { + pub(crate) fn asinh(self, int: &I) -> FResult { Ok(Self::from(self.approximate(int)?.asinh(int)?)) } - pub(crate) fn acosh(self, int: &I) -> FendCoreResult { + pub(crate) fn acosh(self, int: &I) -> FResult { Ok(Self::from(self.approximate(int)?.acosh(int)?)) } - pub(crate) fn atanh(self, int: &I) -> FendCoreResult { + pub(crate) fn atanh(self, int: &I) -> FResult { Ok(Self::from(self.approximate(int)?.atanh(int)?)) } // For all logs: value must be greater than 0 - pub(crate) fn ln(self, int: &I) -> FendCoreResult> { + pub(crate) fn ln(self, int: &I) -> FResult> { Ok(self.approximate(int)?.ln(int)?.apply(Self::from)) } - pub(crate) fn log2(self, int: &I) -> FendCoreResult { + pub(crate) fn log2(self, int: &I) -> FResult { Ok(Self::from(self.approximate(int)?.log2(int)?)) } - pub(crate) fn log10(self, int: &I) -> FendCoreResult { + pub(crate) fn log10(self, int: &I) -> FResult { Ok(Self::from(self.approximate(int)?.log10(int)?)) } - pub(crate) fn factorial(self, int: &I) -> FendCoreResult { + pub(crate) fn factorial(self, int: &I) -> FResult { Ok(Self::from(self.approximate(int)?.factorial(int)?)) } @@ -253,7 +253,7 @@ impl Real { imag: bool, use_parens_if_fraction: bool, int: &I, - ) -> FendCoreResult> { + ) -> FResult> { let mut pi = false; if style == FormattingStyle::Exact && !self.is_zero() { if let Pattern::Pi(_) = self.pattern { @@ -303,11 +303,11 @@ impl Real { )) } - pub(crate) fn exp(self, int: &I) -> FendCoreResult> { + pub(crate) fn exp(self, int: &I) -> FResult> { Ok(self.approximate(int)?.exp(int)?.apply(Self::from)) } - pub(crate) fn pow(self, rhs: Self, int: &I) -> FendCoreResult> { + pub(crate) fn pow(self, rhs: Self, int: &I) -> FResult> { // x^1 == x if let Pattern::Simple(n) = &rhs.pattern { if n == &1.into() { @@ -335,7 +335,7 @@ impl Real { } } - pub(crate) fn root_n(self, n: &Self, int: &I) -> FendCoreResult> { + pub(crate) fn root_n(self, n: &Self, int: &I) -> FResult> { // TODO: Combining these match blocks is not currently possible because // 'binding by-move and by-ref in the same pattern is unstable' // https://github.com/rust-lang/rust/pull/76119 @@ -380,14 +380,14 @@ impl Real { } } - pub(crate) fn expect_rational(self) -> FendCoreResult { + pub(crate) fn expect_rational(self) -> FResult { match self.pattern { Pattern::Simple(a) => Ok(a), Pattern::Pi(_) => Err(FendError::ExpectedARationalNumber), } } - pub(crate) fn modulo(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn modulo(self, rhs: Self, int: &I) -> FResult { Ok(Self::from( self.expect_rational()? .modulo(rhs.expect_rational()?, int)?, @@ -399,7 +399,7 @@ impl Real { rhs: Self, op: crate::ast::BitwiseBop, int: &I, - ) -> FendCoreResult { + ) -> FResult { Ok(Self::from(self.expect_rational()?.bitwise( rhs.expect_rational()?, op, @@ -407,14 +407,14 @@ impl Real { )?)) } - pub(crate) fn combination(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn combination(self, rhs: Self, int: &I) -> FResult { Ok(Self::from( self.expect_rational()? .combination(rhs.expect_rational()?, int)?, )) } - pub(crate) fn permutation(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn permutation(self, rhs: Self, int: &I) -> FResult { Ok(Self::from( self.expect_rational()? .permutation(rhs.expect_rational()?, int)?, @@ -423,7 +423,7 @@ impl Real { } impl Exact { - pub(crate) fn add(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn add(self, rhs: Self, int: &I) -> FResult { if self.exact && self.value.is_zero() { return Ok(rhs); } else if rhs.exact && rhs.value.is_zero() { @@ -450,7 +450,7 @@ impl Exact { ) } - pub(crate) fn mul(self, rhs: Exact<&Real>, int: &I) -> FendCoreResult { + pub(crate) fn mul(self, rhs: Exact<&Real>, int: &I) -> FResult { if self.exact && self.value.is_zero() { return Ok(self); } else if rhs.exact && rhs.value.is_zero() { @@ -484,7 +484,7 @@ impl Exact { }) } - pub(crate) fn div(self, rhs: &Self, int: &I) -> FendCoreResult { + pub(crate) fn div(self, rhs: &Self, int: &I) -> FResult { if rhs.value.is_zero() { return Err(FendError::DivideByZero); } diff --git a/core/src/num/unit.rs b/core/src/num/unit.rs index d22c30cc..8a4313ab 100644 --- a/core/src/num/unit.rs +++ b/core/src/num/unit.rs @@ -3,7 +3,7 @@ use crate::error::{FendError, Interrupt}; use crate::num::complex::{Complex, UseParentheses}; use crate::num::dist::Dist; use crate::num::{Base, FormattingStyle}; -use crate::result::FendCoreResult; +use crate::result::FResult; use crate::scope::Scope; use crate::serialize::{Deserialize, Serialize}; use crate::units::{lookup_default_unit, query_unit_static}; @@ -38,7 +38,7 @@ pub(crate) struct Value { } impl Value { - 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)?; self.unit.serialize(write)?; self.exact.serialize(write)?; @@ -48,7 +48,7 @@ impl Value { Ok(()) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Ok(Self { value: Dist::deserialize(read)?, unit: Unit::deserialize(read)?, @@ -59,14 +59,14 @@ impl Value { }) } - pub(crate) fn try_as_usize(self, int: &I) -> FendCoreResult { + pub(crate) fn try_as_usize(self, int: &I) -> FResult { if !self.is_unitless(int)? { return Err(FendError::NumberWithUnitToInt); } self.try_as_usize_unit(int) } - pub(crate) fn try_as_usize_unit(self, int: &I) -> FendCoreResult { + pub(crate) fn try_as_usize_unit(self, int: &I) -> FResult { if !self.exact { return Err(FendError::InexactNumberToInt); } @@ -80,7 +80,7 @@ impl Value { singular_name: Cow<'static, str>, plural_name: Cow<'static, str>, int: &I, - ) -> FendCoreResult { + ) -> FResult { let (hashmap, scale) = value.unit.to_hashmap_and_scale(int)?; let scale = scale.mul(&Exact::new(value.value.one_point_ref()?.clone(), true), int)?; let resulting_unit = NamedUnit::new( @@ -136,7 +136,7 @@ impl Value { } } - pub(crate) fn factorial(self, int: &I) -> FendCoreResult { + pub(crate) fn factorial(self, int: &I) -> FResult { if !self.is_unitless(int)? { return Err(FendError::FactorialUnitless); } @@ -163,7 +163,7 @@ impl Value { } } - pub(crate) fn add(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn add(self, rhs: Self, int: &I) -> FResult { let scale_factor = Unit::compute_scale_factor(&rhs.unit, &self.unit, int)?; let scaled = Exact::new(rhs.value, rhs.exact) .mul(&scale_factor.scale_1.apply(Dist::from), int)? @@ -189,7 +189,7 @@ impl Value { attrs: Attrs, context: &mut crate::Context, int: &I, - ) -> FendCoreResult { + ) -> FResult { for (lhs_unit, rhs_unit) in crate::units::IMPLICIT_UNIT_MAP { if self.unit.equal_to(lhs_unit) && rhs.is_unitless(int)? { let inches = @@ -201,7 +201,7 @@ impl Value { Ok(rhs) } - pub(crate) fn convert_to(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn convert_to(self, rhs: Self, int: &I) -> FResult { if rhs.value.one_point()? != 1.into() { return Err(FendError::ConversionRhsNumerical); } @@ -220,7 +220,7 @@ impl Value { }) } - pub(crate) fn sub(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn sub(self, rhs: Self, int: &I) -> FResult { let scale_factor = Unit::compute_scale_factor(&rhs.unit, &self.unit, int)?; let scaled = Exact::new(rhs.value, rhs.exact) .mul(&scale_factor.scale_1.apply(Dist::from), int)? @@ -236,7 +236,7 @@ impl Value { }) } - pub(crate) fn div(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn div(self, rhs: Self, int: &I) -> FResult { let mut components = self.unit.components.clone(); for rhs_component in rhs.unit.components { components.push(UnitExponent::new( @@ -256,7 +256,7 @@ impl Value { }) } - fn modulo(self, rhs: Self, int: &I) -> FendCoreResult { + fn modulo(self, rhs: Self, int: &I) -> FResult { if !self.is_unitless(int)? || !rhs.is_unitless(int)? { return Err(FendError::ModuloUnitless); } @@ -274,7 +274,7 @@ impl Value { }) } - fn bitwise(self, rhs: Self, op: BitwiseBop, int: &I) -> FendCoreResult { + fn bitwise(self, rhs: Self, op: BitwiseBop, int: &I) -> FResult { if !self.is_unitless(int)? || !rhs.is_unitless(int)? { return Err(FendError::ExpectedAUnitlessNumber); } @@ -292,7 +292,7 @@ impl Value { }) } - pub(crate) fn combination(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn combination(self, rhs: Self, int: &I) -> FResult { if !self.is_unitless(int)? || !rhs.is_unitless(int)? { return Err(FendError::ExpectedAUnitlessNumber); } @@ -310,7 +310,7 @@ impl Value { }) } - pub(crate) fn permutation(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn permutation(self, rhs: Self, int: &I) -> FResult { if !self.is_unitless(int)? || !rhs.is_unitless(int)? { return Err(FendError::ExpectedAUnitlessNumber); } @@ -335,7 +335,7 @@ impl Value { attrs: Attrs, context: &mut crate::Context, int: &I, - ) -> FendCoreResult { + ) -> FResult { match op { Bop::Plus => self.add(rhs, int), Bop::ImplicitPlus => { @@ -353,7 +353,7 @@ impl Value { } } - pub(crate) fn is_unitless(&self, int: &I) -> FendCoreResult { + pub(crate) fn is_unitless(&self, int: &I) -> FResult { // todo this is broken for unitless components if self.unit.components.is_empty() { return Ok(true); @@ -365,11 +365,11 @@ impl Value { Ok(false) } - pub(crate) fn is_unitless_one(&self, int: &I) -> FendCoreResult { + pub(crate) fn is_unitless_one(&self, int: &I) -> FResult { Ok(self.exact && self.value.equals_int(1) && self.is_unitless(int)?) } - pub(crate) fn pow(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn pow(self, rhs: Self, int: &I) -> FResult { if !rhs.is_unitless(int)? { return Err(FendError::ExpUnitless); } @@ -420,7 +420,7 @@ impl Value { } } - pub(crate) fn abs(self, int: &I) -> FendCoreResult { + pub(crate) fn abs(self, int: &I) -> FResult { let value = self.value.one_point()?.abs(int)?; Ok(Self { value: Complex::from(value.value).into(), @@ -458,20 +458,20 @@ impl Value { self.value.equals_int(0) } - pub(crate) fn new_die(count: u32, faces: u32, int: &I) -> FendCoreResult { + pub(crate) fn new_die(count: u32, faces: u32, int: &I) -> FResult { Ok(Self::new(Dist::new_die(count, faces, int)?, vec![])) } - fn remove_unit_scaling(self, int: &I) -> FendCoreResult { + fn remove_unit_scaling(self, int: &I) -> FResult { self.convert_to(Self::unitless(), int) } fn apply_fn_exact( mut self, - f: impl FnOnce(Complex, &I) -> FendCoreResult>, + f: impl FnOnce(Complex, &I) -> FResult>, require_unitless: bool, int: &I, - ) -> FendCoreResult { + ) -> FResult { self = self.remove_unit_scaling(int)?; if require_unitless && !self.is_unitless(int)? { return Err(FendError::ExpectedAUnitlessNumber); @@ -489,10 +489,10 @@ impl Value { fn apply_fn( mut self, - f: impl FnOnce(Complex, &I) -> FendCoreResult, + f: impl FnOnce(Complex, &I) -> FResult, require_unitless: bool, int: &I, - ) -> FendCoreResult { + ) -> FResult { self = self.remove_unit_scaling(int)?; if require_unitless && !self.is_unitless(int)? { return Err(FendError::ExpectedAUnitlessNumber); @@ -507,11 +507,7 @@ impl Value { }) } - pub(crate) fn sample( - self, - ctx: &crate::Context, - int: &I, - ) -> FendCoreResult { + pub(crate) fn sample(self, ctx: &crate::Context, int: &I) -> FResult { Ok(Self { value: self.value.sample(ctx, int)?, ..self @@ -524,7 +520,7 @@ impl Value { attrs: Attrs, context: &mut crate::Context, int: &I, - ) -> FendCoreResult { + ) -> FResult { let radians = ast::resolve_identifier(&Ident::new_str("radians"), scope, attrs, context, int)? .expect_num()?; @@ -542,21 +538,21 @@ impl Value { } } - pub(crate) fn real(self) -> FendCoreResult { + pub(crate) fn real(self) -> FResult { Ok(Self { value: Complex::from(self.value.one_point()?.real()).into(), ..self }) } - pub(crate) fn imag(self) -> FendCoreResult { + pub(crate) fn imag(self) -> FResult { Ok(Self { value: Complex::from(self.value.one_point()?.imag()).into(), ..self }) } - pub(crate) fn arg(self, int: &I) -> FendCoreResult { + pub(crate) fn arg(self, int: &I) -> FResult { self.apply_fn_exact( |c, int| c.arg(int).map(|c| c.apply(Complex::from)), false, @@ -564,7 +560,7 @@ impl Value { ) } - pub(crate) fn conjugate(self) -> FendCoreResult { + pub(crate) fn conjugate(self) -> FResult { Ok(Self { value: self.value.one_point()?.conjugate().into(), ..self @@ -577,7 +573,7 @@ impl Value { attrs: Attrs, context: &mut crate::Context, int: &I, - ) -> FendCoreResult { + ) -> FResult { if let Ok(rad) = self .clone() .convert_angle_to_rad(scope, attrs, context, int) @@ -596,7 +592,7 @@ impl Value { attrs: Attrs, context: &mut crate::Context, int: &I, - ) -> FendCoreResult { + ) -> FResult { if let Ok(rad) = self .clone() .convert_angle_to_rad(scope, attrs, context, int) @@ -614,7 +610,7 @@ impl Value { attrs: Attrs, context: &mut crate::Context, int: &I, - ) -> FendCoreResult { + ) -> FResult { if let Ok(rad) = self .clone() .convert_angle_to_rad(scope, attrs, context, int) @@ -626,51 +622,51 @@ impl Value { } } - pub(crate) fn asin(self, int: &I) -> FendCoreResult { + pub(crate) fn asin(self, int: &I) -> FResult { self.apply_fn(Complex::asin, false, int) } - pub(crate) fn acos(self, int: &I) -> FendCoreResult { + pub(crate) fn acos(self, int: &I) -> FResult { self.apply_fn(Complex::acos, false, int) } - pub(crate) fn atan(self, int: &I) -> FendCoreResult { + pub(crate) fn atan(self, int: &I) -> FResult { self.apply_fn(Complex::atan, false, int) } - pub(crate) fn sinh(self, int: &I) -> FendCoreResult { + pub(crate) fn sinh(self, int: &I) -> FResult { self.apply_fn(Complex::sinh, false, int) } - pub(crate) fn cosh(self, int: &I) -> FendCoreResult { + pub(crate) fn cosh(self, int: &I) -> FResult { self.apply_fn(Complex::cosh, false, int) } - pub(crate) fn tanh(self, int: &I) -> FendCoreResult { + pub(crate) fn tanh(self, int: &I) -> FResult { self.apply_fn(Complex::tanh, false, int) } - pub(crate) fn asinh(self, int: &I) -> FendCoreResult { + pub(crate) fn asinh(self, int: &I) -> FResult { self.apply_fn(Complex::asinh, false, int) } - pub(crate) fn acosh(self, int: &I) -> FendCoreResult { + pub(crate) fn acosh(self, int: &I) -> FResult { self.apply_fn(Complex::acosh, false, int) } - pub(crate) fn atanh(self, int: &I) -> FendCoreResult { + pub(crate) fn atanh(self, int: &I) -> FResult { self.apply_fn(Complex::atanh, false, int) } - pub(crate) fn ln(self, int: &I) -> FendCoreResult { + pub(crate) fn ln(self, int: &I) -> FResult { self.apply_fn_exact(Complex::ln, true, int) } - pub(crate) fn log2(self, int: &I) -> FendCoreResult { + pub(crate) fn log2(self, int: &I) -> FResult { self.apply_fn(Complex::log2, true, int) } - pub(crate) fn log10(self, int: &I) -> FendCoreResult { + pub(crate) fn log10(self, int: &I) -> FResult { self.apply_fn(Complex::log10, true, int) } @@ -678,7 +674,7 @@ impl Value { &self, ctx: &crate::Context, int: &I, - ) -> FendCoreResult { + ) -> FResult { let use_parentheses = if self.unit.components.is_empty() { UseParentheses::No } else { @@ -713,7 +709,7 @@ impl Value { }) } - pub(crate) fn mul(self, rhs: Self, int: &I) -> FendCoreResult { + pub(crate) fn mul(self, rhs: Self, int: &I) -> FResult { let components = [self.unit.components, rhs.unit.components].concat(); let value = Exact::new(self.value, self.exact).mul(&Exact::new(rhs.value, rhs.exact), int)?; @@ -733,7 +729,7 @@ impl Value { attrs: Attrs, ctx: &mut crate::Context, int: &I, - ) -> FendCoreResult { + ) -> FResult { if !self.simplifiable { return Ok(self); } @@ -965,7 +961,7 @@ struct ScaleFactor { } impl Unit { - pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FendCoreResult<()> { + pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FResult<()> { self.components.len().serialize(write)?; for c in &self.components { c.serialize(write)?; @@ -973,7 +969,7 @@ impl Unit { Ok(()) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { let len = usize::deserialize(read)?; let mut cs = Vec::with_capacity(len); for _ in 0..len { @@ -995,7 +991,7 @@ impl Unit { } /// base units with cancelled exponents do not appear in the hashmap - fn to_hashmap_and_scale(&self, int: &I) -> FendCoreResult { + fn to_hashmap_and_scale(&self, int: &I) -> FResult { let mut hashmap = HashMap::::new(); let mut scale = Complex::from(1); let mut exact = true; @@ -1008,7 +1004,7 @@ impl Unit { fn reduce_hashmap( hashmap: HashMap, int: &I, - ) -> FendCoreResult { + ) -> FResult { if hashmap.len() == 1 && hashmap.get(&BaseUnit::new(Cow::Borrowed("celsius"))) == Some(&1.into()) { @@ -1056,7 +1052,7 @@ impl Unit { fn print_base_units( hash: HashMap, int: &I, - ) -> FendCoreResult { + ) -> FResult { let from_base_units: Vec<_> = hash .into_iter() .map(|(base_unit, exponent)| { @@ -1082,7 +1078,7 @@ impl Unit { from: &Self, into: &Self, int: &I, - ) -> FendCoreResult { + ) -> FResult { let (hash_a, scale_a) = from.to_hashmap_and_scale(int)?; let (hash_b, scale_b) = into.to_hashmap_and_scale(int)?; let (hash_a, adj_a, offset_a) = Self::reduce_hashmap(hash_a, int)?; @@ -1135,7 +1131,7 @@ impl Unit { format: FormattingStyle, consider_printing_space: bool, int: &I, - ) -> FendCoreResult> { + ) -> FResult> { let mut unit_string = String::new(); if self.components.is_empty() { unit_string.push_str(unitless); diff --git a/core/src/num/unit/base_unit.rs b/core/src/num/unit/base_unit.rs index 320e62ba..82e8bb29 100644 --- a/core/src/num/unit/base_unit.rs +++ b/core/src/num/unit/base_unit.rs @@ -1,6 +1,6 @@ use std::{borrow::Cow, fmt, io}; -use crate::result::FendCoreResult; +use crate::result::FResult; use crate::serialize::{Deserialize, Serialize}; /// Represents a base unit, identified solely by its name. The name is not exposed to the user. @@ -30,12 +30,12 @@ impl BaseUnit { self.name.as_ref() } - pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FendCoreResult<()> { + pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FResult<()> { self.name.as_ref().serialize(write)?; Ok(()) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Ok(Self { name: Cow::Owned(String::deserialize(read)?), }) diff --git a/core/src/num/unit/named_unit.rs b/core/src/num/unit/named_unit.rs index 5e4e5564..b10d99fb 100644 --- a/core/src/num/unit/named_unit.rs +++ b/core/src/num/unit/named_unit.rs @@ -2,7 +2,7 @@ use std::{borrow::Cow, collections::HashMap, fmt, io}; use super::base_unit::BaseUnit; use crate::num::complex::Complex; -use crate::result::FendCoreResult; +use crate::result::FResult; use crate::serialize::{Deserialize, Serialize}; /// A named unit, like kilogram, megabyte or percent. @@ -35,7 +35,7 @@ impl NamedUnit { } } - pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FendCoreResult<()> { + pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FResult<()> { self.prefix.as_ref().serialize(write)?; self.singular_name.as_ref().serialize(write)?; self.plural_name.as_ref().serialize(write)?; @@ -51,7 +51,7 @@ impl NamedUnit { Ok(()) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { let prefix = String::deserialize(read)?; let singular_name = String::deserialize(read)?; let plural_name = String::deserialize(read)?; diff --git a/core/src/num/unit/unit_exponent.rs b/core/src/num/unit/unit_exponent.rs index c185f073..ee03447e 100644 --- a/core/src/num/unit/unit_exponent.rs +++ b/core/src/num/unit/unit_exponent.rs @@ -3,7 +3,7 @@ use std::{collections::HashMap, fmt, io}; use crate::interrupt::test_int; use crate::num::complex::{self, Complex, UseParentheses}; use crate::num::{Base, Exact, FormattingStyle}; -use crate::result::FendCoreResult; +use crate::result::FResult; use crate::Interrupt; use super::{base_unit::BaseUnit, named_unit::NamedUnit}; @@ -22,13 +22,13 @@ impl UnitExponent { } } - pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FendCoreResult<()> { + pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FResult<()> { self.unit.serialize(write)?; self.exponent.serialize(write)?; Ok(()) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Ok(Self { unit: NamedUnit::deserialize(read)?, exponent: Complex::deserialize(read)?, @@ -45,7 +45,7 @@ impl UnitExponent { scale: &mut Complex, exact: &mut bool, int: &I, - ) -> FendCoreResult<()> { + ) -> FResult<()> { test_int(int)?; let overall_exp = &Exact::new(self.exponent.clone(), true); for (base_unit, base_exp) in &self.unit.base_units { @@ -86,7 +86,7 @@ impl UnitExponent { plural: bool, invert_exp: bool, int: &I, - ) -> FendCoreResult>> { + ) -> FResult>> { let (prefix, name) = self.unit.prefix_and_name(plural); let exp = if invert_exp { -self.exponent.clone() diff --git a/core/src/result.rs b/core/src/result.rs index 23dacb68..783ca524 100644 --- a/core/src/result.rs +++ b/core/src/result.rs @@ -1,3 +1,3 @@ use crate::error::FendError; -pub(crate) type FendCoreResult = Result; +pub(crate) type FResult = Result; diff --git a/core/src/scope.rs b/core/src/scope.rs index a1dfa8be..aa5ccb7a 100644 --- a/core/src/scope.rs +++ b/core/src/scope.rs @@ -1,5 +1,5 @@ use crate::ident::Ident; -use crate::result::FendCoreResult; +use crate::result::FResult; use crate::serialize::{Deserialize, Serialize}; use crate::value::Value; use crate::Attrs; @@ -19,7 +19,7 @@ impl ScopeValue { attrs: Attrs, context: &mut crate::Context, int: &I, - ) -> FendCoreResult { + ) -> FResult { match self { Self::LazyVariable(expr, scope) => { let value = crate::ast::evaluate(expr.clone(), scope.clone(), attrs, context, int)?; @@ -28,7 +28,7 @@ impl ScopeValue { } } - 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::LazyVariable(e, s) => { e.serialize(write)?; @@ -44,7 +44,7 @@ impl ScopeValue { Ok(()) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Ok(Self::LazyVariable(Expr::deserialize(read)?, { if bool::deserialize(read)? { None @@ -63,7 +63,7 @@ pub(crate) struct Scope { } impl Scope { - pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FendCoreResult<()> { + pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FResult<()> { self.ident.serialize(write)?; self.value.serialize(write)?; match &self.inner { @@ -76,7 +76,7 @@ impl Scope { Ok(()) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Ok(Self { ident: Ident::deserialize(read)?, value: ScopeValue::deserialize(read)?, @@ -113,7 +113,7 @@ impl Scope { attrs: Attrs, context: &mut crate::Context, int: &I, - ) -> FendCoreResult> { + ) -> FResult> { if self.ident.as_str() == ident.as_str() { let value = self.value.eval(attrs, context, int)?; Ok(Some(value)) diff --git a/core/src/serialize.rs b/core/src/serialize.rs index f6e787f1..62d2eddf 100644 --- a/core/src/serialize.rs +++ b/core/src/serialize.rs @@ -1,30 +1,30 @@ -use crate::{error::FendError, result::FendCoreResult}; +use crate::{error::FendError, result::FResult}; use std::io; pub(crate) trait Serialize where Self: Sized, { - fn serialize(&self, write: &mut impl io::Write) -> FendCoreResult<()>; + fn serialize(&self, write: &mut impl io::Write) -> FResult<()>; } pub(crate) trait Deserialize where Self: Sized, { - fn deserialize(read: &mut impl io::Read) -> FendCoreResult; + fn deserialize(read: &mut impl io::Read) -> FResult; } macro_rules! impl_serde { ($($typ: ty)+) => { $( impl Serialize for $typ { - fn serialize(&self, write: &mut impl io::Write) -> FendCoreResult<()> { + fn serialize(&self, write: &mut impl io::Write) -> FResult<()> { Ok(write.write_all(&self.to_be_bytes())?) } } impl Deserialize for $typ { - fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + fn deserialize(read: &mut impl io::Read) -> FResult { let mut buf = [0; std::mem::size_of::<$typ>()]; read.read_exact(&mut buf[..])?; Ok(<$typ>::from_be_bytes(buf)) @@ -37,7 +37,7 @@ macro_rules! impl_serde { impl_serde!(u8 i32 u64 usize); impl Serialize for &str { - fn serialize(&self, write: &mut impl io::Write) -> FendCoreResult<()> { + fn serialize(&self, write: &mut impl io::Write) -> FResult<()> { self.len().serialize(write)?; self.as_bytes() .iter() @@ -47,7 +47,7 @@ impl Serialize for &str { } impl Deserialize for String { - fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + fn deserialize(read: &mut impl io::Read) -> FResult { let len = usize::deserialize(read)?; let mut buf = Vec::with_capacity(len); for _ in 0..len { @@ -61,13 +61,13 @@ impl Deserialize for String { } impl Serialize for bool { - fn serialize(&self, write: &mut impl io::Write) -> FendCoreResult<()> { + fn serialize(&self, write: &mut impl io::Write) -> FResult<()> { Ok(write.write_all(&[u8::from(*self)])?) } } impl Deserialize for bool { - fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + fn deserialize(read: &mut impl io::Read) -> FResult { let mut buf = [0; 1]; read.read_exact(&mut buf[..])?; match buf[0] { diff --git a/core/src/units.rs b/core/src/units.rs index 0265d915..c693d95a 100644 --- a/core/src/units.rs +++ b/core/src/units.rs @@ -3,7 +3,7 @@ use std::borrow::Cow; use crate::error::{FendError, Interrupt}; use crate::eval::evaluate_to_value; use crate::num::Number; -use crate::result::FendCoreResult; +use crate::result::FResult; use crate::value::Value; use crate::Attrs; @@ -35,7 +35,7 @@ fn expr_unit( attrs: Attrs, context: &mut crate::Context, int: &I, -) -> FendCoreResult { +) -> FResult { let (singular, plural, definition) = unit_def; let mut definition = definition.trim(); if definition == "$CURRENCY" { @@ -139,7 +139,7 @@ fn expr_unit( }) } -fn construct_prefixed_unit(a: UnitDef, b: UnitDef, int: &I) -> FendCoreResult { +fn construct_prefixed_unit(a: UnitDef, b: UnitDef, int: &I) -> FResult { let product = a.value.expect_num()?.mul(b.value.expect_num()?, int)?; assert_eq!(a.singular, a.plural); let unit = Number::create_unit_value_from_value( @@ -153,7 +153,7 @@ pub(crate) fn query_unit( attrs: Attrs, context: &mut crate::Context, int: &I, -) -> FendCoreResult { +) -> FResult { if ident.starts_with('\'') && ident.ends_with('\'') && ident.len() >= 3 { let ident = ident.split_at(1).1; let ident = ident.split_at(ident.len() - 1).0; @@ -170,7 +170,7 @@ pub(crate) fn query_unit_static( attrs: Attrs, context: &mut crate::Context, int: &I, -) -> FendCoreResult { +) -> FResult { match query_unit_case_sensitive(ident, true, attrs, context, int) { Err(FendError::IdentifierNotFound(_)) => (), Err(e) => return Err(e), @@ -187,7 +187,7 @@ fn query_unit_case_sensitive( attrs: Attrs, context: &mut crate::Context, int: &I, -) -> FendCoreResult { +) -> FResult { match query_unit_internal(ident, false, case_sensitive, true, context) { Err(FendError::IdentifierNotFound(_)) => (), Err(e) => return Err(e), @@ -238,7 +238,7 @@ fn query_unit_internal( case_sensitive: bool, whole_unit: bool, context: &mut crate::Context, -) -> FendCoreResult<(Cow<'static, str>, Cow<'static, str>, Cow<'static, str>)> { +) -> FResult<(Cow<'static, str>, Cow<'static, str>, Cow<'static, str>)> { if !short_prefixes { for (s, p, d) in &context.custom_units { let p = if p.is_empty() { s } else { p }; diff --git a/core/src/value.rs b/core/src/value.rs index ca261330..e5e56ac1 100644 --- a/core/src/value.rs +++ b/core/src/value.rs @@ -2,7 +2,7 @@ use crate::ast::Bop; use crate::date::{Date, DayOfWeek, Month}; use crate::error::{FendError, Interrupt}; 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::{ast::Expr, ident::Ident}; @@ -44,7 +44,7 @@ pub(crate) enum ApplyMulHandling { } impl Value { - 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::Num(n) => { 0u8.serialize(write)?; @@ -109,7 +109,7 @@ impl Value { Ok(()) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Ok(match u8::deserialize(read)? { 0 => Self::Num(Box::new(Number::deserialize(read)?)), 1 => Self::BuiltInFunction(BuiltInFunction::deserialize(read)?), @@ -165,7 +165,7 @@ impl Value { } } - fn as_bool(&self) -> FendCoreResult { + fn as_bool(&self) -> FResult { if let Self::Bool(b) = self { Ok(*b) } else { @@ -173,7 +173,7 @@ impl Value { } } - pub(crate) fn expect_num(self) -> FendCoreResult { + pub(crate) fn expect_num(self) -> FResult { match self { Self::Num(bigrat) => Ok(*bigrat), _ => Err(FendError::ExpectedANumber), @@ -186,10 +186,10 @@ impl Value { pub(crate) fn handle_num( self, - eval_fn: impl FnOnce(Number) -> FendCoreResult, + eval_fn: impl FnOnce(Number) -> FResult, lazy_fn: impl FnOnce(Box) -> Expr, scope: Option>, - ) -> FendCoreResult { + ) -> FResult { Ok(match self { Self::Num(n) => Self::Num(Box::new(eval_fn(*n)?)), Self::Fn(param, expr, scope) => Self::Fn(param, Box::new(lazy_fn(expr)), scope), @@ -201,11 +201,11 @@ impl Value { pub(crate) fn handle_two_nums) -> Expr, F2: FnOnce(Box) -> Expr>( self, rhs: Self, - eval_fn: impl FnOnce(Number, Number) -> FendCoreResult, + eval_fn: impl FnOnce(Number, Number) -> FResult, lazy_fn_lhs: impl FnOnce(Number) -> F1, lazy_fn_rhs: impl FnOnce(Number) -> F2, scope: Option>, - ) -> FendCoreResult { + ) -> FResult { Ok(match (self, rhs) { (Self::Num(a), Self::Num(b)) => Self::Num(Box::new(eval_fn(*a, *b)?)), (Self::BuiltInFunction(f), Self::Num(a)) => f.wrap_with_expr(lazy_fn_lhs(*a), scope), @@ -228,7 +228,7 @@ impl Value { attrs: Attrs, context: &mut crate::Context, int: &I, - ) -> FendCoreResult { + ) -> FResult { let stringified_self = self.format_to_plain_string(0, attrs, context, int)?; Ok(match self { Self::Num(n) => { @@ -275,7 +275,7 @@ impl Value { attrs: Attrs, context: &mut crate::Context, int: &I, - ) -> FendCoreResult { + ) -> FResult { let arg = crate::ast::evaluate(arg, scope.clone(), attrs, context, int)?; Ok(Self::Num(Box::new(match func { BuiltInFunction::Approximately => arg.expect_num()?.make_approximate(), @@ -318,7 +318,7 @@ impl Value { attrs: Attrs, ctx: &mut crate::Context, int: &I, - ) -> FendCoreResult { + ) -> FResult { let mut spans = vec![]; self.format(indent, &mut spans, attrs, ctx, int)?; let mut res = String::new(); @@ -335,7 +335,7 @@ impl Value { attrs: Attrs, ctx: &mut crate::Context, int: &I, - ) -> FendCoreResult<()> { + ) -> FResult<()> { match self { Self::Num(n) => { n.clone() @@ -436,7 +436,7 @@ impl Value { Ok(()) } - pub(crate) fn get_object_member(self, key: &Ident) -> FendCoreResult { + pub(crate) fn get_object_member(self, key: &Ident) -> FResult { match self { Self::Object(kv) => { for (k, v) in kv { diff --git a/core/src/value/built_in_function.rs b/core/src/value/built_in_function.rs index e8fe762d..96e2b216 100644 --- a/core/src/value/built_in_function.rs +++ b/core/src/value/built_in_function.rs @@ -1,4 +1,4 @@ -use crate::result::FendCoreResult; +use crate::result::FResult; use crate::value::Expr; use crate::value::Ident; use crate::value::Scope; @@ -54,7 +54,7 @@ impl BuiltInFunction { ) } - pub(crate) fn invert(self) -> FendCoreResult { + pub(crate) fn invert(self) -> FResult { Ok(match self { Self::Sin => Value::BuiltInFunction(Self::Asin), Self::Cos => Value::BuiltInFunction(Self::Acos), @@ -101,7 +101,7 @@ impl BuiltInFunction { } } - fn try_from_str(s: &str) -> FendCoreResult { + fn try_from_str(s: &str) -> FResult { Ok(match s { "approximately" => Self::Approximately, "abs" => Self::Abs, @@ -130,11 +130,11 @@ impl BuiltInFunction { }) } - pub(crate) fn serialize(self, write: &mut impl io::Write) -> FendCoreResult<()> { + pub(crate) fn serialize(self, write: &mut impl io::Write) -> FResult<()> { self.as_str().serialize(write) } - pub(crate) fn deserialize(read: &mut impl io::Read) -> FendCoreResult { + pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult { Self::try_from_str(String::deserialize(read)?.as_str()) } }