Skip to content

Commit

Permalink
Add trait
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Jan 21, 2024
1 parent 96aac44 commit 1f16d37
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/elements/subword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,23 @@

pub mod unquoted;

use crate::{Feeder, ShellCore};
use super::subword::unquoted::UnquotedSubword;
use std::fmt;
use std::fmt::Debug;

impl Debug for dyn Subword {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct(&self.get_text()).finish()
}
}

pub trait Subword {
fn get_text(&self) -> String;
fn eval(&mut self) -> Vec<Vec<String>>;
}

pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option<Box<dyn Subword>> {
if let Some(a) = UnquotedSubword::parse(feeder, core){ Some(Box::new(a)) }
else{ None }
}
6 changes: 6 additions & 0 deletions src/elements/subword/unquoted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
//SPDX-License-Identifier: BSD-3-Clause

use crate::{ShellCore, Feeder};
use crate::elements::subword::Subword;

#[derive(Debug)]
pub struct UnquotedSubword {
pub text: String,
}

impl Subword for UnquotedSubword {
fn get_text(&self) -> String { self.text.clone() }
fn eval(&mut self) -> Vec<Vec<String>> { vec![vec![self.text.clone()]] }
}

impl UnquotedSubword {
fn new() -> UnquotedSubword {
UnquotedSubword {
Expand Down
9 changes: 5 additions & 4 deletions src/elements/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
//SPDX-License-Identifier: BSD-3-Clause

use crate::{Feeder, ShellCore};
use crate::elements::subword::unquoted::UnquotedSubword;
use crate::elements::subword;
use crate::elements::subword::Subword;

#[derive(Debug)]
pub struct Word {
pub text: String,
pub subwords: Vec<UnquotedSubword>,
pub subwords: Vec<Box<dyn Subword>>,
}

impl Word {
Expand All @@ -24,8 +25,8 @@ impl Word {

pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option<Word> {
let mut ans = Word::new();
while let Some(sw) = UnquotedSubword::parse(feeder, core) {
ans.text += &sw.text.clone();
while let Some(sw) = subword::parse(feeder, core) {
ans.text += &sw.get_text();
ans.subwords.push(sw);
}

Expand Down

0 comments on commit 1f16d37

Please sign in to comment.