mirror of
https://github.com/AleoHQ/leo.git
synced 2024-08-17 10:50:29 +03:00
fmt fixes, realized they have to be in that format for outputs
This commit is contained in:
parent
d6c3aec431
commit
0bd580e3d1
5
.gitignore
vendored
5
.gitignore
vendored
@ -8,3 +8,8 @@
|
||||
**/.crates.toml
|
||||
**/.crates2.json
|
||||
**/bin/
|
||||
|
||||
# Emacs gitignore files
|
||||
*~
|
||||
\#*\#
|
||||
.\#*
|
||||
|
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -2800,9 +2800,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tracing-subscriber"
|
||||
version = "0.2.18"
|
||||
version = "0.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa5553bf0883ba7c9cbe493b085c29926bd41b66afc31ff72cf17ff4fb60dcd5"
|
||||
checksum = "ab69019741fca4d98be3c62d2b75254528b5432233fd8a4d2739fec20278de48"
|
||||
dependencies = [
|
||||
"ansi_term 0.12.1",
|
||||
"chrono",
|
||||
|
@ -145,7 +145,7 @@ version = "0.5"
|
||||
version = "0.1"
|
||||
|
||||
[dependencies.tracing-subscriber]
|
||||
version = "0.2"
|
||||
version = "0.2.19"
|
||||
features = [ "fmt" ]
|
||||
|
||||
[dependencies.zip]
|
||||
|
@ -21,6 +21,9 @@ use snarkvm_r1cs::SynthesisError;
|
||||
pub enum GroupError {
|
||||
#[error("{}", _0)]
|
||||
Error(#[from] FormattedError),
|
||||
|
||||
#[error("Failed to represent group as log string.")]
|
||||
LogError,
|
||||
}
|
||||
|
||||
impl LeoError for GroupError {}
|
||||
@ -30,6 +33,10 @@ impl GroupError {
|
||||
GroupError::Error(FormattedError::new_from_span(message, span))
|
||||
}
|
||||
|
||||
pub fn failed_to_create_log_string() -> Self {
|
||||
GroupError::LogError
|
||||
}
|
||||
|
||||
pub fn negate_operation(error: SynthesisError, span: &Span) -> Self {
|
||||
let message = format!("group negation failed due to the synthesis error `{:?}`", error,);
|
||||
|
||||
|
@ -14,7 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use leo_ast::{FormattedError, IntegerType, LeoError, Span};
|
||||
use leo_ast::{FormattedError, LeoError, Span};
|
||||
|
||||
use snarkvm_gadgets::errors::{SignedIntegerError, UnsignedIntegerError};
|
||||
use snarkvm_r1cs::SynthesisError;
|
||||
@ -68,7 +68,7 @@ impl IntegerError {
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn integer_type_mismatch(expected: &IntegerType, received: IntegerType, span: &Span) -> Self {
|
||||
pub fn integer_type_mismatch(expected: String, received: String, span: &Span) -> Self {
|
||||
let message = format!("expected data type `{}`, found `{}`", expected, received);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
|
@ -18,7 +18,7 @@
|
||||
|
||||
use crate::{
|
||||
address::Address,
|
||||
errors::FunctionError,
|
||||
errors::{FunctionError, IntegerError},
|
||||
program::ConstrainedProgram,
|
||||
value::{
|
||||
boolean::input::bool_from_input,
|
||||
@ -101,9 +101,19 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
|
||||
Ok(ConstrainedValue::Field(FieldType::constant(cs, value, span)?))
|
||||
}
|
||||
(Type::Group, InputValue::Group(value)) => Ok(ConstrainedValue::Group(G::constant(&value.into(), span)?)),
|
||||
(Type::Integer(integer_type), InputValue::Integer(_, value)) => Ok(ConstrainedValue::Integer(
|
||||
Integer::new(&ConstInt::parse(integer_type, &value, span)?),
|
||||
)),
|
||||
(Type::Integer(integer_type), InputValue::Integer(input_type, value)) => {
|
||||
let parsed = ConstInt::parse(integer_type, &value, span)?;
|
||||
let parsed_type = parsed.get_int_type();
|
||||
let input_type = input_type.into();
|
||||
if std::mem::discriminant(&parsed_type) != std::mem::discriminant(&input_type) {
|
||||
return Err(FunctionError::from(IntegerError::integer_type_mismatch(
|
||||
input_type.to_string(),
|
||||
parsed_type.to_string(),
|
||||
span,
|
||||
)));
|
||||
}
|
||||
Ok(ConstrainedValue::Integer(Integer::new(&parsed)))
|
||||
}
|
||||
(Type::Array(type_, arr_len), InputValue::Array(values)) => {
|
||||
if *arr_len != values.len() {
|
||||
return Err(FunctionError::invalid_input_array_dimensions(
|
||||
|
@ -155,6 +155,8 @@ impl Output {
|
||||
|
||||
format!("({})", values)
|
||||
}
|
||||
ConstrainedValue::Field(field) => format!("{:?}", field),
|
||||
ConstrainedValue::Group(group) => format!("{:?}", group),
|
||||
_ => value.to_string(),
|
||||
};
|
||||
|
||||
|
@ -45,6 +45,8 @@ pub trait GroupType<F: Field>:
|
||||
+ ToBitsBEGadget<F>
|
||||
+ ToBytesGadget<F>
|
||||
{
|
||||
fn log_string(&self) -> Result<String, GroupError>;
|
||||
|
||||
fn constant(value: &GroupValue, span: &Span) -> Result<Self, GroupError>;
|
||||
|
||||
fn to_allocated<CS: ConstraintSystem<F>>(&self, cs: CS, span: &Span) -> Result<Self, GroupError>;
|
||||
|
@ -52,6 +52,16 @@ pub enum EdwardsGroupType {
|
||||
}
|
||||
|
||||
impl GroupType<Fq> for EdwardsGroupType {
|
||||
fn log_string(&self) -> Result<String, GroupError> {
|
||||
match self {
|
||||
EdwardsGroupType::Constant(constant) => Ok(format!("({}, {})group", constant.x, constant.y)),
|
||||
EdwardsGroupType::Allocated(allocated) => match (allocated.x.get_value(), allocated.y.get_value()) {
|
||||
(Some(x), Some(y)) => Ok(format!("({}, {})group", x, y)),
|
||||
_ => Err(GroupError::failed_to_create_log_string()),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn constant(group: &GroupValue, span: &Span) -> Result<Self, GroupError> {
|
||||
let value = Self::edwards_affine_from_value(group, span)?;
|
||||
|
||||
|
@ -160,7 +160,11 @@ impl Integer {
|
||||
if let InputValue::Integer(type_, number) = input {
|
||||
let asg_type = IntegerType::from(type_);
|
||||
if std::mem::discriminant(&asg_type) != std::mem::discriminant(integer_type) {
|
||||
return Err(IntegerError::integer_type_mismatch(integer_type, asg_type, span));
|
||||
return Err(IntegerError::integer_type_mismatch(
|
||||
integer_type.to_string(),
|
||||
asg_type.to_string(),
|
||||
span,
|
||||
));
|
||||
}
|
||||
Some(number)
|
||||
} else {
|
||||
|
@ -96,8 +96,14 @@ impl<'a, F: PrimeField, G: GroupType<F>> fmt::Display for ConstrainedValue<'a, F
|
||||
.unwrap_or_else(|| "[allocated]".to_string())
|
||||
),
|
||||
ConstrainedValue::Char(ref value) => write!(f, "{}", value),
|
||||
ConstrainedValue::Field(ref value) => write!(f, "{:?}", value),
|
||||
ConstrainedValue::Group(ref value) => write!(f, "{:?}", value),
|
||||
ConstrainedValue::Field(ref value) => {
|
||||
if let Some(value) = value.get_value() {
|
||||
write!(f, "{}", value)
|
||||
} else {
|
||||
Err(std::fmt::Error)
|
||||
}
|
||||
}
|
||||
ConstrainedValue::Group(ref value) => write!(f, "{}", value.log_string().unwrap_or_else(|e| e.to_string())),
|
||||
ConstrainedValue::Integer(ref value) => write!(f, "{}", value),
|
||||
|
||||
// Data type wrappers
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
use std::{fmt, sync::Once};
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use colored::Colorize;
|
||||
use tracing::{event::Event, subscriber::Subscriber};
|
||||
use tracing_subscriber::{
|
||||
@ -181,17 +182,23 @@ where
|
||||
|
||||
let mut message = "".to_string();
|
||||
|
||||
let scope = context.scope();
|
||||
for span in scope {
|
||||
message += span.metadata().name();
|
||||
match context.lookup_current() {
|
||||
Some(span_ref) => {
|
||||
let scope = span_ref.scope();
|
||||
|
||||
let ext = span.extensions();
|
||||
let fields = &ext
|
||||
.get::<FormattedFields<N>>()
|
||||
.expect("Unable to find FormattedFields in extensions; this is a bug");
|
||||
if !fields.is_empty() {
|
||||
message += &format!("{{{}}}", fields);
|
||||
for span in scope {
|
||||
message += span.metadata().name();
|
||||
|
||||
let ext = span.extensions();
|
||||
let fields = &ext
|
||||
.get::<FormattedFields<N>>()
|
||||
.expect("Unable to find FormattedFields in extensions; this is a bug");
|
||||
if !fields.is_empty() {
|
||||
message += &format!("{{{}}}", fields);
|
||||
}
|
||||
}
|
||||
}
|
||||
None => return Err(std::fmt::Error),
|
||||
}
|
||||
|
||||
write!(writer, "{:>10} ", colored_string(meta.level(), &message)).expect("Error writing event");
|
||||
@ -203,10 +210,16 @@ where
|
||||
}
|
||||
|
||||
/// Initialize logger with custom format and verbosity.
|
||||
pub fn init_logger(_app_name: &'static str, verbosity: usize) {
|
||||
pub fn init_logger(_app_name: &'static str, verbosity: usize) -> Result<()> {
|
||||
// This line enables Windows 10 ANSI coloring API.
|
||||
#[cfg(target_family = "windows")]
|
||||
ansi_term::enable_ansi_support();
|
||||
match ansi_term::enable_ansi_support() {
|
||||
Ok(_) => {}
|
||||
Err(_) => {
|
||||
println!("YOTE");
|
||||
return Err(anyhow!("Error: Failedto enable ansi_support"));
|
||||
}
|
||||
};
|
||||
|
||||
let subscriber = FmtSubscriber::builder()
|
||||
// all spans/events with a level higher than TRACE (e.g, debug, info, warn, etc.)
|
||||
@ -226,4 +239,5 @@ pub fn init_logger(_app_name: &'static str, verbosity: usize) {
|
||||
START.call_once(|| {
|
||||
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
12
leo/main.rs
12
leo/main.rs
@ -38,7 +38,7 @@ use commands::{
|
||||
Watch,
|
||||
};
|
||||
|
||||
use anyhow::Error;
|
||||
use anyhow::Result;
|
||||
use std::{path::PathBuf, process::exit};
|
||||
use structopt::{clap::AppSettings, StructOpt};
|
||||
|
||||
@ -185,13 +185,13 @@ fn main() {
|
||||
}
|
||||
|
||||
/// Run command with custom build arguments.
|
||||
fn run_with_args(opt: Opt) -> Result<(), Error> {
|
||||
fn run_with_args(opt: Opt) -> Result<()> {
|
||||
if !opt.quiet {
|
||||
// Init logger with optional debug flag.
|
||||
logger::init_logger("leo", match opt.debug {
|
||||
false => 1,
|
||||
true => 2,
|
||||
});
|
||||
})?;
|
||||
}
|
||||
|
||||
// Get custom root folder and create context for it.
|
||||
@ -225,7 +225,7 @@ fn run_with_args(opt: Opt) -> Result<(), Error> {
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_error<T>(res: Result<T, Error>) -> T {
|
||||
fn handle_error<T>(res: Result<T>) -> T {
|
||||
match res {
|
||||
Ok(t) => t,
|
||||
Err(err) => {
|
||||
@ -239,13 +239,13 @@ fn handle_error<T>(res: Result<T, Error>) -> T {
|
||||
mod cli_tests {
|
||||
use crate::{run_with_args, Opt};
|
||||
|
||||
use anyhow::Error;
|
||||
use anyhow::Result;
|
||||
use std::path::PathBuf;
|
||||
use structopt::StructOpt;
|
||||
use test_dir::{DirBuilder, FileType, TestDir};
|
||||
|
||||
// Runs Command from cmd-like argument "leo run --arg1 --arg2".
|
||||
fn run_cmd(args: &str, path: &Option<PathBuf>) -> Result<(), Error> {
|
||||
fn run_cmd(args: &str, path: &Option<PathBuf>) -> Result<()> {
|
||||
let args = args.split(' ').collect::<Vec<&str>>();
|
||||
let mut opts = Opt::from_iter_safe(args)?;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user