mirror of
https://github.com/AleoHQ/leo.git
synced 2024-12-20 08:01:42 +03:00
cleanup, serializers
This commit is contained in:
parent
c90b8de590
commit
7a29c8ba6d
@ -19,7 +19,7 @@ use crate::{Expression, Identifier, Type};
|
||||
|
||||
/// A single definition inside a section in a state or an input file.
|
||||
/// Structure of a definition would be: `<name>: <type_> = <value>;`
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Definition {
|
||||
pub type_: Type,
|
||||
pub name: Identifier,
|
||||
|
@ -15,9 +15,10 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use super::*;
|
||||
use leo_errors::AstError;
|
||||
|
||||
/// Input data which includes [`ProgramInput`] and [`ProgramState`].
|
||||
#[derive(Debug, Clone, Default)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub struct Input {
|
||||
pub program_input: ProgramInput,
|
||||
pub program_state: ProgramState,
|
||||
@ -25,7 +26,14 @@ pub struct Input {
|
||||
|
||||
/// A raw unprocessed input or state file data. Used for future conversion
|
||||
/// into [`ProgramInput`] or [`ProgramState`].
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct ParsedInputFile {
|
||||
pub sections: Vec<Section>,
|
||||
}
|
||||
|
||||
impl Input {
|
||||
/// Serializes the ast into a JSON string.
|
||||
pub fn to_json_string(&self) -> Result<String> {
|
||||
Ok(serde_json::to_string_pretty(&self).map_err(|e| AstError::failed_to_convert_ast_to_json_string(&e))?)
|
||||
}
|
||||
}
|
||||
|
@ -17,9 +17,10 @@
|
||||
use crate::{CharValue, Expression, GroupValue, IntegerType, Node, SpreadOrExpression, Type, ValueExpression};
|
||||
use leo_errors::{AstError, LeoError, ParserError, Result};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum InputValue {
|
||||
Address(String),
|
||||
Boolean(bool),
|
||||
|
@ -38,5 +38,7 @@ pub use section::*;
|
||||
use indexmap::IndexMap;
|
||||
use leo_errors::{LeoError, Result};
|
||||
use leo_span::{sym, Span, Symbol};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
||||
type Definitions = IndexMap<Parameter, InputValue>;
|
||||
|
@ -19,7 +19,7 @@ use crate::{Identifier, Type};
|
||||
|
||||
/// A set of properties for a single definition in an input file.
|
||||
/// Used as a key in [`ProgramInput`] and [`ProgramState`].
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize)]
|
||||
pub struct Parameter {
|
||||
pub variable: Identifier,
|
||||
pub type_: Type,
|
||||
@ -35,3 +35,20 @@ impl From<Definition> for Parameter {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Parameter {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}: {}", self.variable, self.type_)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parameter is a key, so for allowing its JSON representation,
|
||||
/// we need to make a string.
|
||||
impl Serialize for Parameter {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
serializer.serialize_str(&self.to_string())
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,7 @@
|
||||
use super::*;
|
||||
|
||||
/// Processed Program input.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub struct ProgramInput {
|
||||
main: Definitions,
|
||||
registers: Definitions,
|
||||
|
@ -17,7 +17,7 @@
|
||||
use super::*;
|
||||
|
||||
/// Processed Program state.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub struct ProgramState {
|
||||
state: Definitions,
|
||||
record: Definitions,
|
||||
|
@ -18,7 +18,7 @@ use super::*;
|
||||
|
||||
/// A single section in an input or a state file.
|
||||
/// Example of a section would be: `[main]`.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Section {
|
||||
pub name: Symbol,
|
||||
pub definitions: Vec<Definition>,
|
||||
|
@ -19,7 +19,7 @@ use leo_span::symbol::create_session_if_not_set_then;
|
||||
|
||||
use std::{env, fs, path::Path};
|
||||
|
||||
fn to_leo_tree(filepath: &Path) -> Result<String> {
|
||||
fn to_leo_tree(filepath: &Path) -> Result<String, String> {
|
||||
// Loads the Leo code as a string from the given file path.
|
||||
let program_filepath = filepath.to_path_buf();
|
||||
let program_string = fs::read_to_string(&program_filepath).expect("failed to open input file");
|
||||
@ -27,20 +27,22 @@ fn to_leo_tree(filepath: &Path) -> Result<String> {
|
||||
// Parses the Leo file constructing an ast which is then serialized.
|
||||
create_session_if_not_set_then(|_| {
|
||||
Handler::with(|handler| {
|
||||
let _ast = leo_parser::parse_program_input(
|
||||
let input = leo_parser::parse_program_input(
|
||||
&handler,
|
||||
program_string.clone(),
|
||||
filepath.to_str().unwrap(),
|
||||
program_string,
|
||||
filepath.to_str().unwrap(),
|
||||
)?;
|
||||
// Ok(Input::to_json_string(&ast).expect("serialization failed"))
|
||||
Ok("aa".to_string())
|
||||
});
|
||||
|
||||
let json = input.to_json_string()?;
|
||||
println!("{}", json);
|
||||
Ok(json)
|
||||
}).map_err(|e| e.to_string())
|
||||
})
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
fn main() -> Result<(), String> {
|
||||
// Parse the command-line arguments as strings.
|
||||
let cli_arguments = env::args().collect::<Vec<String>>();
|
||||
|
||||
|
@ -161,6 +161,9 @@ symbols! {
|
||||
record,
|
||||
state,
|
||||
state_leaf,
|
||||
|
||||
public,
|
||||
private,
|
||||
}
|
||||
|
||||
/// An interned string.
|
||||
|
Loading…
Reference in New Issue
Block a user