Refactor FunctionInput; removes unecessary code

This commit is contained in:
Pranav Gaddamadugu 2022-08-04 17:48:57 -07:00
parent ac4cd71800
commit 112cc64290
3 changed files with 6 additions and 95 deletions

View File

@ -41,7 +41,7 @@ impl fmt::Display for ParamMode {
/// A function parameter. /// A function parameter.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FunctionInputVariable { pub struct FunctionInput {
/// The name the parameter is accessible as in the function's body. /// The name the parameter is accessible as in the function's body.
pub identifier: Identifier, pub identifier: Identifier,
/// The mode of the function parameter. /// The mode of the function parameter.
@ -52,7 +52,7 @@ pub struct FunctionInputVariable {
pub span: Span, pub span: Span,
} }
impl FunctionInputVariable { impl FunctionInput {
pub fn new(identifier: Identifier, mode: ParamMode, type_: Type, span: Span) -> Self { pub fn new(identifier: Identifier, mode: ParamMode, type_: Type, span: Span) -> Self {
Self { Self {
identifier, identifier,
@ -67,7 +67,7 @@ impl FunctionInputVariable {
} }
} }
impl FunctionInputVariable { impl FunctionInput {
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result { fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} ", self.mode)?; write!(f, "{} ", self.mode)?;
write!(f, "{}: ", self.identifier)?; write!(f, "{}: ", self.identifier)?;
@ -75,16 +75,16 @@ impl FunctionInputVariable {
} }
} }
impl fmt::Display for FunctionInputVariable { impl fmt::Display for FunctionInput {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.format(f) self.format(f)
} }
} }
impl fmt::Debug for FunctionInputVariable { impl fmt::Debug for FunctionInput {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.format(f) self.format(f)
} }
} }
crate::simple_node_impl!(FunctionInputVariable); crate::simple_node_impl!(FunctionInput);

View File

@ -1,86 +0,0 @@
// Copyright (C) 2019-2022 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 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 crate::{FunctionInputVariable, Node};
use leo_span::Span;
use serde::{Deserialize, Serialize};
use std::fmt;
/// Enumerates the possible inputs to a function.
#[derive(Clone, Serialize, Deserialize)]
pub enum FunctionInput {
/// A normal function parameter.
Variable(FunctionInputVariable),
}
impl FunctionInput {
///
/// Returns Option with FunctionInputVariable if the input is a variable.
/// Returns None otherwise.
///
pub fn get_variable(&self) -> &FunctionInputVariable {
match self {
Self::Variable(var) => var,
}
}
/// Formats the parameter to `f`.
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
FunctionInput::Variable(function_input) => write!(f, "{}", function_input),
}
}
}
impl fmt::Display for FunctionInput {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.format(f)
}
}
impl fmt::Debug for FunctionInput {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.format(f)
}
}
impl PartialEq for FunctionInput {
/// Returns true if `self == other`. Does not compare spans.
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(FunctionInput::Variable(left), FunctionInput::Variable(right)) => left.eq(right),
}
}
}
impl Eq for FunctionInput {}
impl Node for FunctionInput {
fn span(&self) -> Span {
use FunctionInput::*;
match self {
Variable(variable) => variable.span,
}
}
fn set_span(&mut self, span: Span) {
use FunctionInput::*;
match self {
Variable(variable) => variable.span = span,
}
}
}

View File

@ -20,9 +20,6 @@ pub use annotation::*;
pub mod function_input; pub mod function_input;
pub use function_input::*; pub use function_input::*;
pub mod input_variable;
pub use input_variable::*;
use crate::{Block, Identifier, Node, Type}; use crate::{Block, Identifier, Node, Type};
use leo_span::{sym, Span, Symbol}; use leo_span::{sym, Span, Symbol};