Skip to content

Commit

Permalink
Bump curr and next (#298)
Browse files Browse the repository at this point in the history
* Bump curr and next

* Add missing soroban files
  • Loading branch information
sisuresh authored Sep 8, 2023
1 parent ec816c7 commit bda0d98
Show file tree
Hide file tree
Showing 10 changed files with 25,094 additions and 10,775 deletions.
34,732 changes: 24,008 additions & 10,724 deletions src/curr/generated.rs

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions src/curr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
mod generated;
pub use generated::*;

mod scval_conversions;
pub use scval_conversions::*;

mod scval_validations;
pub use scval_validations::*;

#[cfg(feature = "alloc")]
mod scmap;
#[cfg(feature = "alloc")]
pub use scmap::*;
75 changes: 75 additions & 0 deletions src/curr/scmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#![allow(clippy::missing_errors_doc)]

use super::{Error, ScMap, ScMapEntry, ScVal, Validate};
extern crate alloc;
use alloc::vec::Vec;

impl ScMap {
pub fn sorted_from_entries<I, E>(entries: I) -> Result<ScMap, Error>
where
E: TryInto<ScMapEntry>,
I: Iterator<Item = E>,
{
let mut v = entries
.map(TryInto::try_into)
.collect::<Result<Vec<_>, _>>()
.map_err(|_| Error::Invalid)?;
// TODO: Add tests that prove order consistency of ScVal with RawVal. https://github.com/stellar/rs-stellar-xdr/issues/117
v.sort_by(|a, b| a.key.cmp(&b.key));
let m = ScMap(v.try_into()?);
// `validate` will further check that there are no duplicates.
m.validate()?;
Ok(m)
}

pub fn sorted_from_pairs<K, V, I>(pairs: I) -> Result<ScMap, Error>
where
K: TryInto<ScVal>,
V: TryInto<ScVal>,
I: Iterator<Item = (K, V)>,
{
Self::sorted_from_entries(pairs)
}

pub fn sorted_from<I, E>(src: I) -> Result<ScMap, Error>
where
E: TryInto<ScMapEntry>,
I: IntoIterator<Item = E>,
{
Self::sorted_from_entries(src.into_iter())
}
}

#[cfg(test)]
mod test {
use super::*;
use alloc::{collections::BTreeMap, vec};

#[test]
fn scmap_from_map() -> Result<(), ()> {
let mut m: BTreeMap<u32, u32> = BTreeMap::new();
m.insert(1, 2);
m.insert(5, 6);
m.insert(3, 4);
let scm = ScMap::sorted_from(m)?;
assert_eq!(scm.0.first().unwrap().key, 1u32.into());
assert_eq!(scm.0.last().unwrap().key, 5u32.into());
Ok(())
}

#[test]
fn scmap_from_pairs() -> Result<(), ()> {
let pairs: Vec<(u32, u32)> = vec![(3, 4), (5, 6), (1, 2)];
let scm = ScMap::sorted_from(pairs)?;
assert_eq!(scm.0.first().unwrap().key, 1u32.into());
assert_eq!(scm.0.last().unwrap().key, 5u32.into());
Ok(())
}

#[test]
fn scmap_from_pairs_containing_duplicate_keys() {
let pairs: Vec<(u32, u32)> = vec![(3, 4), (3, 5), (5, 6), (1, 2)];
let scm = ScMap::sorted_from(pairs);
assert!(scm.is_err());
}
}
Loading

0 comments on commit bda0d98

Please sign in to comment.