Skip to content

Commit

Permalink
#93 kid() returns int
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Mar 3, 2023
1 parent 3db1888 commit 73e212e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl Sodg {
v = u32::from_str(num.as_str())?;
continue;
}
if let Some((to, loc)) = self.kid(v, k.as_str()) {
if let Some(to) = self.kid(v, k.as_str()) {
if !loc.starts_with('.') {
v = to;
continue;
Expand Down
14 changes: 7 additions & 7 deletions src/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Sodg {
let target = if let Some(t) = mapped.get(&to) {
self.bind(left, *t, a.as_str())?;
*t
} else if let Some((t, _)) = self.kid(left, &a) {
} else if let Some(t) = self.kid(left, &a) {
t
} else {
let id = self.next_id();
Expand All @@ -91,8 +91,8 @@ fn merges_two_graphs() -> Result<()> {
extra.bind(0, 1, "bar")?;
g.merge(&extra, 0, 0)?;
assert_eq!(3, g.vertices.len());
assert_eq!(1, g.kid(0, "foo").unwrap().0);
assert_eq!(2, g.kid(0, "bar").unwrap().0);
assert_eq!(1, g.kid(0, "foo").unwrap());
assert_eq!(2, g.kid(0, "bar").unwrap());
Ok(())
}

Expand All @@ -110,7 +110,7 @@ fn avoids_simple_duplicates() -> Result<()> {
extra.bind(1, 2, "bar")?;
g.merge(&extra, 0, 0)?;
assert_eq!(3, g.vertices.len());
assert_eq!(1, g.kid(0, "foo").unwrap().0);
assert_eq!(1, g.kid(0, "foo").unwrap());
Ok(())
}

Expand Down Expand Up @@ -186,8 +186,8 @@ fn understands_same_name_kids() -> Result<()> {
extra.bind(1, 2, "x")?;
g.merge(&extra, 0, 0)?;
assert_eq!(5, g.vertices.len());
assert_eq!(1, g.kid(0, "a").unwrap().0);
assert_eq!(2, g.kid(1, "x").unwrap().0);
assert_eq!(1, g.kid(0, "a").unwrap());
assert_eq!(2, g.kid(1, "x").unwrap());
Ok(())
}

Expand All @@ -204,7 +204,7 @@ fn merges_into_empty_graph() -> Result<()> {
extra.bind(3, 1, "c")?;
g.merge(&extra, 1, 1)?;
assert_eq!(3, g.vertices.len());
assert_eq!(2, g.kid(1, "a").unwrap().0);
assert_eq!(2, g.kid(1, "a").unwrap());
Ok(())
}

Expand Down
14 changes: 5 additions & 9 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ impl Sodg {
Ok(kids)
}

/// Find a kid of a vertex, by its edge name, and return the ID of the vertex
/// found and the locator of the edge.
/// Find a kid of a vertex, by its edge name, and return the ID of the vertex found.
///
/// For example:
///
Expand All @@ -195,16 +194,13 @@ impl Sodg {
/// g.add(0).unwrap();
/// g.add(42).unwrap();
/// g.bind(0, 42, "k").unwrap();
/// assert_eq!((42, "k".to_string()), g.kid(0, "k").unwrap());
/// assert_eq!(42, g.kid(0, "k").unwrap());
/// ```
///
/// If vertex `v1` is absent, `None` will be returned.
pub fn kid(&self, v: u32, a: &str) -> Option<(u32, String)> {
pub fn kid(&self, v: u32, a: &str) -> Option<u32> {
if let Some(vtx) = self.vertices.get(&v) {
vtx.edges
.iter()
.find(|e| e.a == a)
.map(|e| (e.to, e.a.clone()))
vtx.edges.iter().find(|e| e.a == a).map(|e| e.to)
} else {
None
}
Expand Down Expand Up @@ -264,7 +260,7 @@ fn overwrites_edge() -> Result<()> {
g.bind(1, 2, "foo")?;
g.add(3)?;
g.bind(1, 3, "foo")?;
assert_eq!(3, g.kid(1, "foo").unwrap().0);
assert_eq!(3, g.kid(1, "foo").unwrap());
Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Script {
/// );
/// let mut g = Sodg::empty();
/// let total = s.deploy_to(&mut g).unwrap();
/// assert_eq!(1, g.kid(0, "foo").unwrap().0);
/// assert_eq!(1, g.kid(0, "foo").unwrap());
/// ```
#[allow(clippy::should_implement_trait)]
pub fn from_str(s: &str) -> Script {
Expand Down Expand Up @@ -195,6 +195,6 @@ fn simple_command() -> Result<()> {
let total = s.deploy_to(&mut g)?;
assert_eq!(4, total);
assert_eq!("привет", g.data(1)?.to_utf8()?);
assert_eq!(1, g.kid(0, "foo").unwrap().0);
assert_eq!(1, g.kid(0, "foo").unwrap());
Ok(())
}

0 comments on commit 73e212e

Please sign in to comment.