Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump curr and next #298

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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