Skip to content

Commit

Permalink
#79 care about dot-starting locs on finding
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jan 16, 2023
1 parent 8e97e9d commit 6c38e58
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,25 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::{DeadRelay, LambdaRelay, Relay, Sodg};
use crate::{ConstRelay, DeadRelay, LambdaRelay, Relay, Sodg};
use anyhow::{anyhow, Context, Result};
use log::trace;
use std::collections::VecDeque;
use std::str::FromStr;

impl Relay for ConstRelay {
fn re(&self, _v: u32, _a: &str) -> Result<String> {
Ok(self.s.clone())
}
}

impl ConstRelay {
/// Make a new [`ConstRelay`], with a string inside.
pub fn new(s: &str) -> Self {
ConstRelay { s: s.to_string() }
}
}

impl Relay for DeadRelay {
fn re(&self, v: u32, a: &str) -> Result<String> {
Err(anyhow!("Can't find ν{v}.{a}"))
Expand Down Expand Up @@ -164,9 +177,11 @@ impl Sodg {
v = u32::from_str(num.as_str())?;
continue;
}
if let Some((to, _loc)) = self.kid(v, k.as_str()) {
v = to;
continue;
if let Some((to, loc)) = self.kid(v, k.as_str()) {
if !loc.starts_with('.') {
v = to;
continue;
}
};
trace!("#find(ν{v1}, {loc}): {indent}calling relay(ν{v}, {k})...");
let fault = match relay.re(v, &k) {
Expand Down Expand Up @@ -230,6 +245,20 @@ fn finds_with_closure() -> Result<()> {
Ok(())
}

#[test]
fn finds_with_locator() -> Result<()> {
let mut g = Sodg::empty();
g.add(1)?;
g.add(2)?;
g.bind(1, 2, "a/.foo")?;
g.add(3)?;
g.bind(1, 3, "xyz")?;
g.add(4)?;
g.bind(3, 4, "x")?;
assert_eq!(4, g.find(1, "a.x", &mut ConstRelay::new("xyz"))?);
Ok(())
}

#[test]
fn finds_root() -> Result<()> {
let mut g = Sodg::empty();
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ pub struct LambdaRelay {
lambda: fn(u32, &str) -> Result<String>,
}

/// A [`Relay`] that always returns the same `String`.
pub struct ConstRelay {
s: String,
}

#[cfg(test)]
use simple_logger::SimpleLogger;

Expand Down

0 comments on commit 6c38e58

Please sign in to comment.