Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-12-06 13:52:57 -06:00
commit 528ccf1523
782 files changed
+145156

No files matched your search

+357
View File
@@ -0,0 +1,357 @@
#! @file result.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the properties and methods of `result`, and `ok` and `err`, which
#! make one holding a value or an error.
#!
#! `result`, which holds either the value an operation gave or the error that
#! stopped it, is among the types the syntax makes, which the compiler declares
#! itself. It must be handled where it is made, since letting it go would lose
#! the error it may hold: by `match`, by `is`, or by `else`, as
#! `val f := open(path) else (e) { return e; };`.
#!
#! The methods reading what it holds, as `unwrap`, move it out of what can be
#! written, as `move` does, and copy it out of what is only read, which a value
#! that closes refuses. Those making another `result` of it, as `map`, move it
#! in, as an iterator's adapters do: `as_ref` lends them what one that is only
#! read holds. Each makes what it gives by naming the field, so a `result` whose
#! value and error are of one type is told apart.
module core;
#! Makes a result holding a value.
#! @param value The value to hold.
#! @returns The result, holding the value.
pub def result<T, E>::ok(value: T): result<T, E> {
return result::<T, E> { value = move(&mut value) };
}
#! Makes a result holding an error.
#! @param error The error to hold.
#! @returns The result, holding the error.
pub def result<T, E>::err(error: E): result<T, E> {
return result::<T, E> { error = move(&mut error) };
}
#! Whether it holds the value, where the work succeeded.
#! @param this The result.
#! @returns Whether it does.
pub val result<T, E>::is_ok: bool { get => this is value; }
#! Whether it holds the error, where the work failed.
#! @param this The result.
#! @returns Whether it does.
pub val result<T, E>::is_err: bool { get => this is error; }
#! Whether it holds a value a test passes.
#! @tparam F The test.
#! @param this The result.
#! @param test The test, given a reference to the value.
#! @returns Whether it holds one that passes.
pub def result<T, E>::is_ok_and<F>(&this, test: F): bool
where F: invokable<def(&T): bool>
{
return this is value(&held) && test(held);
}
#! Whether it holds an error a test passes.
#! @tparam F The test.
#! @param this The result.
#! @param test The test, given a reference to the error.
#! @returns Whether it holds one that passes.
pub def result<T, E>::is_err_and<F>(&this, test: F): bool
where F: invokable<def(&E): bool>
{
return this is error(&held) && test(held);
}
#! Gives the value it holds, moved out as `move` moves it. It stops the program
#! where it holds an error.
#! @param this The result.
#! @returns The value.
pub def result<T, E>::unwrap(&mut this): T {
var held := move(this);
return move(got) if held is value(&mut got);
fault("A `result` holding an error was unwrapped.");
}
#! Gives a copy of the value it holds, which a value that closes refuses. It
#! stops the program where it holds an error.
#! @param this The result.
#! @returns The value.
pub def result<T, E>::unwrap(&this): T {
if const T is closeable {
copied::<T>();
} else {
return got if
this is value(got);
}
fault("A `result` holding an error was unwrapped.");
}
#! Gives the value it holds, moved out as `unwrap` moves it. It stops the
#! program where it holds an error, saying why.
#! @param this The result.
#! @param why What went wrong, where it holds an error.
#! @returns The value.
pub def result<T, E>::expect(&mut this, why: strview): T {
var held := move(this);
return move(got) if
held is value(&mut got);
fault(why);
}
#! Gives a copy of the value it holds, which a value that closes refuses. It
#! stops the program where it holds an error, saying why.
#! @param this The result.
#! @param why What went wrong, where it holds an error.
#! @returns The value.
pub def result<T, E>::expect(&this, why: strview): T {
if const T is closeable {
copied::<T>();
} else {
return got if
this is value(got);
}
fault(why);
}
#! Gives the error it holds, moved out as `move` moves it. It stops the program
#! where it holds a value.
#! @param this The result.
#! @returns The error.
pub def result<T, E>::unwrap_err(&mut this): E {
var held := move(this);
return move(got) if
held is error(&mut got);
fault("A `result` holding a value was unwrapped for its error.");
}
#! Gives a copy of the error it holds, which an error that closes refuses. It
#! stops the program where it holds a value.
#! @param this The result.
#! @returns The error.
pub def result<T, E>::unwrap_err(&this): E {
if const E is closeable {
copied::<E>();
} else {
return got if
this is error(got);
}
fault("A `result` holding a value was unwrapped for its error.");
}
#! Gives the error it holds, moved out as `unwrap_err` moves it. It stops the
#! program where it holds a value, saying why.
#! @param this The result.
#! @param why What went wrong, where it holds a value.
#! @returns The error.
pub def result<T, E>::expect_err(&mut this, why: strview): E {
var held := move(this);
return move(got) if
held is error(&mut got);
fault(why);
}
#! Gives a copy of the error it holds, which an error that closes refuses. It
#! stops the program where it holds a value, saying why.
#! @param this The result.
#! @param why What went wrong, where it holds a value.
#! @returns The error.
pub def result<T, E>::expect_err(&this, why: strview): E {
if const E is closeable {
copied::<E>();
} else {
return got if
this is error(got);
}
fault(why);
}
#! Gives the value it holds, moved out as `unwrap` moves it, or a fallback
#! where it holds an error.
#! @param this The result.
#! @param fallback The value given where it holds an error.
#! @returns The value, or the fallback.
pub def result<T, E>::unwrap_or(&mut this, fallback: T): T {
var held := move(this);
return move(got) if
held is value(&mut got);
return move(&mut fallback);
}
#! Gives a copy of the value it holds, which a value that closes refuses, or a
#! fallback where it holds an error.
#! @param this The result.
#! @param fallback The value given where it holds an error.
#! @returns The value, or the fallback.
pub def result<T, E>::unwrap_or(&this, fallback: T): T {
if const T is closeable {
copied::<T>();
} else {
return got if
this is value(got);
}
return move(&mut fallback);
}
#! Gives the value it holds, moved out as `unwrap` moves it, or what a function
#! makes of the error, which it is called on only then.
#! @tparam F The function.
#! @param this The result.
#! @param make The function, given the error, moved out.
#! @returns The value, or what the function made.
pub def result<T, E>::unwrap_or_else<F>(&mut this, make: F): T where F: invokable<def(E): T> {
var held := move(this);
return match held {
value(&mut got) => move(got),
error(&mut bad) => make(move(bad))
};
}
#! Lends what it holds: a reference to the value or to the error.
#! @param this The result.
#! @returns The result of the references.
pub def result<T, E>::as_ref(&this): result<&T, &E> {
return match this {
value(&got) => result::<&T, &E> { value = got },
error(&bad) => result::<&T, &E> { error = bad }
};
}
#! Lends what it holds to be written: a reference to the value or to the
#! error.
#! @param this The result.
#! @returns The result of the references.
pub def result<T, E>::as_mut(&mut this): result<&mut T, &mut E> {
return match this {
value(&mut got) => result::<&mut T, &mut E> { value = got },
error(&mut bad) => result::<&mut T, &mut E> { error = bad }
};
}
#! Makes a value of the value it holds, which a function is given, moved in,
#! keeping the error it holds otherwise.
#! @tparam U What the function makes.
#! @tparam F The function.
#! @param this The result, moved in as `unwrap` moves it.
#! @param f The function, called on the value where it holds one.
#! @returns The result of what the function made, or of the error.
pub def result<T, E>::map<U, F>(&mut this, f: F): result<U, E>
where F: invokable<def(T): U>
{
var held := move(this);
return match held {
value(&mut got) => result::<U, E> { value = f(move(got)) },
error(&mut bad) => result::<U, E> { error = move(bad) }
};
}
#! Makes another error of the error it holds, which a function is given, moved
#! in, keeping the value it holds otherwise.
#! @tparam D What the function makes.
#! @tparam F The function.
#! @param this The result, moved in as `unwrap` moves it.
#! @param f The function, called on the error where it holds one.
#! @returns The result of the value, or of what the function made.
pub def result<T, E>::map_err<D, F>(&mut this, f: F): result<T, D>
where F: invokable<def(E): D>
{
var held := move(this);
return match held {
value(&mut got) => result::<T, D> { value = move(got) },
error(&mut bad) => result::<T, D> { error = f(move(bad)) }
};
}
#! Gives what a function makes of the value it holds, which may fail too: the
#! next step of work that may fail, keeping the error it holds otherwise.
#! @tparam U What the function may make.
#! @tparam F The function.
#! @param this The result, moved in as `unwrap` moves it.
#! @param f The function, called on the value where it holds one.
#! @returns What the function gave, or the result of the error.
pub def result<T, E>::and_then<U, F>(&mut this, f: F): result<U, E>
where F: invokable<def(T): result<U, E>>
{
var held := move(this);
return match held {
value(&mut got) => f(move(got)),
error(&mut bad) => result::<U, E> { error = move(bad) }
};
}
#! Gives what a function makes of the error it holds, which may recover from
#! it, keeping the value it holds otherwise.
#! @tparam D The error of what the function makes.
#! @tparam F The function.
#! @param this The result, moved in as `unwrap` moves it.
#! @param f The function, called on the error where it holds one.
#! @returns The result of the value, or what the function gave.
pub def result<T, E>::or_else<D, F>(&mut this, f: F): result<T, D>
where F: invokable<def(E): result<T, D>>
{
var held := move(this);
return match held {
value(&mut got) => result::<T, D> { value = move(got) },
error(&mut bad) => f(move(bad))
};
}
#! Gives the value it holds, as a `maybe`, losing the error.
#! @param this The result, moved in as `unwrap` moves it.
#! @returns Maybe the value; otherwise, `nothing` where it holds an error.
pub def result<T, E>::to_maybe(&mut this): maybe<T> {
var held := move(this);
return maybe::<T> { some = move(got) } if held is value(&mut got);
return nothing {};
}
#! Gives the error it holds, as a `maybe`, losing the value.
#! @param this The result, moved in as `unwrap` moves it.
#! @returns Maybe the error; otherwise, `nothing` where it holds a value.
pub def result<T, E>::err_to_maybe(&mut this): maybe<E> {
var held := move(this);
return maybe::<E> { some = move(got) } if held is error(&mut got);
return nothing {};
}
#! Whether it holds what another does: equal values, or equal errors, as `==`
#! compares them.
#! @param this The result.
#! @param other The other.
#! @returns Whether they are equal.
#! @remarks A `result` whose value or error is of a type that is not
#! `equatable` is an error where it is compared.
pub def result<T, E>::equals(&this, other: &result<T, E>): bool {
return match this {
value(&mine) => other is value(&theirs) && equals::<T>(mine, theirs),
error(&mine) => other is error(&theirs) && equals::<E>(mine, theirs)
};
}
#! Hashes what it holds, so a value hashes apart from an error.
#! @param this The result.
#! @returns The hash.
#! @remarks A `result` whose value or error is of a type that is not `hashable`
#! is an error where it is hashed.
pub def result<T, E>::hash(&this): u64 {
return match this {
value(&got) => mix(1, hash::<T>(got)),
error(&bad) => mix(2, hash::<E>(bad))
};
}
#! Builds a copy of a result, holding a copy of its value or its error, as
#! `copy` makes it.
#! @param other The result to copy.
#! @returns The copy.
pub def result<T, E>::copy_of(other: &result<T, E>): result<T, E> {
return match other {
value(&got) => result::<T, E> { value = copy::<T>(got) },
error(&bad) => result::<T, E> { error = copy::<E>(bad) }
};
}