Skip to content
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
2 changes: 1 addition & 1 deletion src/exp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ impl Exp {
let args = input_args(&ctx, &func.args)?;
// Ideally, we should store the args in helper and call editor.readline_with_initial to display
// the full command in the editor. The tricky part is to know where to insert the args in text.
eprintln!("Generated arguments: {}", args);
eprintln!("Generated arguments: {args}");
eprintln!("Do you want to send this message? [y/N]");
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;
Expand Down
2 changes: 1 addition & 1 deletion src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ fn match_type(line: &str, helper: &MyHelper) -> Option<(usize, Vec<Pair>)> {
for (var, value) in helper.env.0.iter() {
let ty = value.value_ty();
if candid::types::subtype::subtype(&mut gamma, env, &ty, expect_ty).is_ok() {
let value = format!("{:?}", value);
let value = format!("{value:?}");
// TODO use floor_char_boundary when available.
let value = &value[..20.min(value.len())];
res.push(Pair {
Expand Down
4 changes: 2 additions & 2 deletions src/offline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub fn send(helper: &MyHelper, msg: &IngressWithStatus) -> Result<IDLArgs> {
IDLArgs::from_bytes(&bytes)?
};
println!("Sending {} call as {}:", message.call_type, sender);
println!(" call \"{}\".{}{};", canister_id, method_name, args);
println!(" call \"{canister_id}\".{method_name}{args};");
println!("Do you want to send this message? [y/N]");
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;
Expand Down Expand Up @@ -188,6 +188,6 @@ async fn send_internal(
} else {
IDLArgs::from_bytes(&response)?
};
println!("{}", res);
println!("{res}");
Ok(res)
}
51 changes: 47 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ pub fn stringify(v: &IDLValue) -> anyhow::Result<Cow<str>> {

fn num_cast_helper(v: IDLValue, truncate_float: bool) -> Result<String> {
Ok(match v {
IDLValue::Number(n) => n,
IDLValue::Number(n) => n.replace('_', ""),
IDLValue::Int64(n) => n.to_string(),
IDLValue::Int32(n) => n.to_string(),
IDLValue::Int16(n) => n.to_string(),
IDLValue::Int8(n) => n.to_string(),
IDLValue::Int(n) => n.to_string(),
IDLValue::Int(n) => n.to_string().replace('_', ""),
IDLValue::Nat64(n) => n.to_string(),
IDLValue::Nat32(n) => n.to_string(),
IDLValue::Nat16(n) => n.to_string(),
IDLValue::Nat8(n) => n.to_string(),
IDLValue::Nat(n) => n.to_string(),
IDLValue::Nat(n) => n.to_string().replace('_', ""),
IDLValue::Float32(f) => if truncate_float { f.trunc() } else { f }.to_string(),
IDLValue::Float64(f) => if truncate_float { f.trunc() } else { f }.to_string(),
_ => return Err(anyhow!("{v} is not a number")),
Expand Down Expand Up @@ -278,7 +278,7 @@ pub async fn fetch_state_path(agent: &Agent, mut path: StatePath) -> anyhow::Res
})?
};
path.effective_id = Some(id);
eprintln!("Using {} as effective canister/subnet id. To change it, put the effective id as the first argument.", id);
eprintln!("Using {id} as effective canister/subnet id. To change it, put the effective id as the first argument.");
}
fetch_state_path_(agent, path).await
}
Expand Down Expand Up @@ -503,3 +503,46 @@ pub fn parse_state_path(paths: &[IDLValue]) -> anyhow::Result<StatePath> {
result,
})
}

#[test]
fn test_cast_type_big_num() {
use candid::{Int, Nat};

// cast to Nat64
assert!(
matches!(cast_type(IDLValue::Number("1_000_000".to_string()), &TypeInner::Nat64.into()),
Ok(v) if v == IDLValue::Nat64(1_000_000u64))
);
assert!(
matches!(cast_type(IDLValue::Nat(Nat::from(1_000_000u64)), &TypeInner::Nat64.into()),
Ok(v) if v == IDLValue::Nat64(1_000_000u64))
);
assert!(
matches!(cast_type(IDLValue::Int(Int::from(1_000_000i64)), &TypeInner::Nat64.into()),
Ok(v) if v == IDLValue::Nat64(1_000_000u64))
);

fn pow<T: std::ops::MulAssign + From<u32> + Clone>(base: T, n: i32) -> T {
let mut num = T::from(1u32);
for _ in 0..n {
num *= base.clone();
}
num
}

// cast to Float64
for n in [0i32, 1, 10, 55] {
assert!(
matches!(cast_type(IDLValue::Number(pow(Nat::from(10u64), n).to_string()), &TypeInner::Float64.into()),
Ok(v) if v == IDLValue::Float64(10f64.powi(n)))
);
assert!(
matches!(cast_type(IDLValue::Nat(pow(Nat::from(10u64), n)), &TypeInner::Float64.into()),
Ok(v) if v == IDLValue::Float64(10f64.powi(n)))
);
assert!(
matches!(cast_type(IDLValue::Int(pow(Int::from(-10i64), n)), &TypeInner::Float64.into()),
Ok(v) if v == IDLValue::Float64((-10f64).powi(n)))
);
}
}
Loading