Skip to content

Commit

Permalink
var.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodesdev committed Jun 7, 2024
1 parent 2428377 commit 2769946
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 89 deletions.
3 changes: 2 additions & 1 deletion crates/syntest/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod syntest;
mod var;

pub use syntest::LocalVariable;
pub use syntest::Syntest;
pub use var::LocalVariable;
90 changes: 2 additions & 88 deletions crates/syntest/src/syntest.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::fmt::Display;
use std::fs;
use std::path::PathBuf;
use syn::{
parse_file, punctuated::Punctuated, token::PathSep, Expr, Item, ItemFn, Lit, Pat, PathSegment,
Stmt,
};

use crate::var::{LocalVariable, VarDetails};

pub struct Syntest {
pub file: syn::File,
}
Expand Down Expand Up @@ -363,93 +364,6 @@ impl From<&str> for Syntest {
}
}

#[derive(Debug, PartialEq)]
pub struct VarDetails {
pub name: String,
pub used: bool,
}

#[derive(Debug, PartialEq)]
pub enum LocalVariable {
Str {
name: String,
value: String,
used: bool,
},
Int {
name: String,
value: usize,
used: bool,
},
Float {
name: String,
value: f64,
used: bool,
},
Char {
name: String,
value: char,
used: bool,
},
Bool {
name: String,
value: bool,
used: bool,
},
Closure {
name: String,
used: bool,
},
Other {
name: String,
used: bool,
},
}

impl LocalVariable {
pub fn is_used(&self) -> bool {
match self {
LocalVariable::Str { used, .. } => *used,
LocalVariable::Int { used, .. } => *used,
LocalVariable::Float { used, .. } => *used,
LocalVariable::Char { used, .. } => *used,
LocalVariable::Bool { used, .. } => *used,
LocalVariable::Closure { used, .. } => *used,
LocalVariable::Other { used, .. } => *used,
}
}

pub fn name(&self) -> &str {
match self {
LocalVariable::Str { name, .. } => name,
LocalVariable::Int { name, .. } => name,
LocalVariable::Float { name, .. } => name,
LocalVariable::Char { name, .. } => name,
LocalVariable::Bool { name, .. } => name,
LocalVariable::Closure { name, .. } => name,
LocalVariable::Other { name, .. } => name,
}
}

pub fn value(&self) -> String {
match self {
LocalVariable::Str { value, .. } => value.clone(),
LocalVariable::Int { value, .. } => value.to_string(),
LocalVariable::Float { value, .. } => value.to_string(),
LocalVariable::Char { value, .. } => value.to_string(),
LocalVariable::Bool { value, .. } => value.to_string(),
LocalVariable::Closure { .. } => "closure".to_string(),
LocalVariable::Other { .. } => "other".to_string(),
}
}
}

impl Display for LocalVariable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
88 changes: 88 additions & 0 deletions crates/syntest/src/var.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use std::fmt::Display;

#[derive(Debug, PartialEq)]
pub struct VarDetails {
pub name: String,
pub used: bool,
}

#[derive(Debug, PartialEq)]
pub enum LocalVariable {
Str {
name: String,
value: String,
used: bool,
},
Int {
name: String,
value: usize,
used: bool,
},
Float {
name: String,
value: f64,
used: bool,
},
Char {
name: String,
value: char,
used: bool,
},
Bool {
name: String,
value: bool,
used: bool,
},
Closure {
name: String,
used: bool,
},
Other {
name: String,
used: bool,
},
}

impl LocalVariable {
pub fn is_used(&self) -> bool {
match self {
LocalVariable::Str { used, .. } => *used,
LocalVariable::Int { used, .. } => *used,
LocalVariable::Float { used, .. } => *used,
LocalVariable::Char { used, .. } => *used,
LocalVariable::Bool { used, .. } => *used,
LocalVariable::Closure { used, .. } => *used,
LocalVariable::Other { used, .. } => *used,
}
}

pub fn name(&self) -> &str {
match self {
LocalVariable::Str { name, .. } => name,
LocalVariable::Int { name, .. } => name,
LocalVariable::Float { name, .. } => name,
LocalVariable::Char { name, .. } => name,
LocalVariable::Bool { name, .. } => name,
LocalVariable::Closure { name, .. } => name,
LocalVariable::Other { name, .. } => name,
}
}

pub fn value(&self) -> String {
match self {
LocalVariable::Str { value, .. } => value.clone(),
LocalVariable::Int { value, .. } => value.to_string(),
LocalVariable::Float { value, .. } => value.to_string(),
LocalVariable::Char { value, .. } => value.to_string(),
LocalVariable::Bool { value, .. } => value.to_string(),
LocalVariable::Closure { .. } => "closure".to_string(),
LocalVariable::Other { .. } => "other".to_string(),
}
}
}

impl Display for LocalVariable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name())
}
}

0 comments on commit 2769946

Please sign in to comment.