Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

apply review suggestions to etf module

author
Giacomo Cavalieri
committer
Louis Pilfold
date (Jul 14, 2026, 12:57 PM +0100) commit 6d37e748 parent 1512b64e change-id lsttzyzz
+40 -40
+40 -40
erlang-term-format/src/lib.rs
··· 8 8 #[macro_use] 9 9 extern crate pretty_assertions; 10 10 11 - /// A data structure used to encode values into the Erlang Term Format: 12 - /// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html. 13 - /// 14 - #[derive(Debug)] 15 - pub struct Etf { 16 - bytes: Vec<u8>, 17 - } 18 - 19 - impl Default for Etf { 20 - fn default() -> Self { 21 - Self::new() 22 - } 23 - } 24 - 25 11 #[must_use] 26 12 #[derive(Debug)] 27 13 pub struct List { ··· 48 34 } 49 35 } 50 36 51 - impl Etf { 37 + /// A data structure used to encode values into the Erlang Term Format: 38 + /// https://www.erlang.org/doc/apps/erts/erl_ext_dist.html. 39 + /// 40 + #[derive(Debug)] 41 + pub struct Builder { 42 + bytes: Vec<u8>, 43 + } 44 + 45 + impl Default for Builder { 46 + fn default() -> Self { 47 + Self::new() 48 + } 49 + } 50 + 51 + impl Builder { 52 52 pub fn new() -> Self { 53 53 Self { bytes: vec![131] } 54 54 } ··· 104 104 list.consume(); 105 105 } 106 106 107 - /// Pushes the most compact etf representation of the given atom. 107 + /// Pushes the most compact representation of the given atom. 108 108 pub fn atom(&mut self, atom: &str) { 109 109 if atom.len() <= 255 { 110 110 self.small_atom_utf8(atom); ··· 113 113 } 114 114 } 115 115 116 - /// Pushes the most compact etf representation of the given bigint number. 116 + /// Pushes the most compact representation of the given big int. 117 117 pub fn bigint(&mut self, value: BigInt) { 118 118 if let Some(value) = value.to_u8() { 119 119 self.small_integer(value); ··· 124 124 } 125 125 } 126 126 127 - /// Pushes the most compact etf representation of the given usize number. 127 + /// Pushes the most compact representation of the given usize int. 128 128 /// 129 129 pub fn usize(&mut self, value: usize) { 130 130 if let Some(value) = value.to_u8() { ··· 221 221 222 222 use num_bigint::BigInt; 223 223 224 - use crate::Etf; 224 + use crate::Builder; 225 225 226 226 #[test] 227 227 fn small_atom() { 228 - let mut etf = Etf::new(); 228 + let mut etf = Builder::new(); 229 229 etf.atom("atom"); 230 230 assert_eq!(etf.into_vec(), [131, 119, 4, 97, 116, 111, 109]) 231 231 } 232 232 233 233 #[test] 234 234 fn small_atom_utf8() { 235 - let mut etf = Etf::new(); 235 + let mut etf = Builder::new(); 236 236 etf.atom("ksiąskę"); 237 237 assert_eq!( 238 238 etf.into_vec(), ··· 242 242 243 243 #[test] 244 244 fn atom() { 245 - let mut etf = Etf::new(); 245 + let mut etf = Builder::new(); 246 246 etf.atom(&"ą".repeat(128)); 247 247 assert_eq!( 248 248 etf.into_vec(), ··· 270 270 271 271 #[test] 272 272 fn small_integer() { 273 - let mut etf = Etf::new(); 273 + let mut etf = Builder::new(); 274 274 etf.small_integer(1); 275 275 assert_eq!(etf.into_vec(), [131, 97, 1]); 276 276 } 277 277 278 278 #[test] 279 279 fn integer() { 280 - let mut etf = Etf::new(); 280 + let mut etf = Builder::new(); 281 281 etf.integer(-2); 282 282 assert_eq!(etf.into_vec(), [131, 98, 255, 255, 255, 254]); 283 283 } 284 284 285 285 #[test] 286 286 fn integer_2() { 287 - let mut etf = Etf::new(); 287 + let mut etf = Builder::new(); 288 288 etf.integer(1048576); 289 289 assert_eq!(etf.into_vec(), [131, 98, 0, 16, 0, 0]); 290 290 } 291 291 292 292 #[test] 293 293 fn float() { 294 - let mut etf = Etf::new(); 294 + let mut etf = Builder::new(); 295 295 etf.new_float(1.2); 296 296 assert_eq!(etf.into_vec(), [131, 70, 63, 243, 51, 51, 51, 51, 51, 51]) 297 297 } 298 298 299 299 #[test] 300 300 fn small_big() { 301 - let mut etf = Etf::new(); 301 + let mut etf = Builder::new(); 302 302 303 303 etf.small_big(BigInt::from(123123123123123123 as i64)); 304 304 assert_eq!( ··· 309 309 310 310 #[test] 311 311 fn negative_small_big() { 312 - let mut etf = Etf::new(); 312 + let mut etf = Builder::new(); 313 313 etf.small_big(BigInt::from(-123123123123123123 as i64)); 314 314 assert_eq!( 315 315 etf.into_vec(), ··· 319 319 320 320 #[test] 321 321 fn empty_list() { 322 - let mut etf = Etf::new(); 322 + let mut etf = Builder::new(); 323 323 etf.empty_list(); 324 324 assert_eq!(etf.into_vec(), [131, 106]); 325 325 } 326 326 327 327 #[test] 328 328 fn proper_list_with_a_single_item() { 329 - let mut etf = Etf::new(); 329 + let mut etf = Builder::new(); 330 330 let list = etf.start_list(); 331 331 etf.small_integer(1); 332 332 etf.end_list(list, 1); ··· 335 335 336 336 #[test] 337 337 fn proper_list() { 338 - let mut etf = Etf::new(); 338 + let mut etf = Builder::new(); 339 339 let list = etf.start_list(); 340 340 etf.small_integer(1); 341 341 etf.small_tuple(0); ··· 349 349 350 350 #[test] 351 351 fn empty_binary() { 352 - let mut etf = Etf::new(); 352 + let mut etf = Builder::new(); 353 353 etf.binary(0, vec![]); 354 354 assert_eq!(etf.into_vec(), [131, 109, 0, 0, 0, 0]) 355 355 } 356 356 #[test] 357 357 fn binary() { 358 - let mut etf = Etf::new(); 358 + let mut etf = Builder::new(); 359 359 etf.binary(3, vec![1, 2, 3]); 360 360 assert_eq!(etf.into_vec(), [131, 109, 0, 0, 0, 3, 1, 2, 3]) 361 361 } 362 362 363 363 #[test] 364 364 fn small_empty_tuple() { 365 - let mut etf = Etf::new(); 365 + let mut etf = Builder::new(); 366 366 etf.small_tuple(0); 367 367 assert_eq!(etf.into_vec(), [131, 104, 0]) 368 368 } 369 369 370 370 #[test] 371 371 fn small_single_item_tuple() { 372 - let mut etf = Etf::new(); 372 + let mut etf = Builder::new(); 373 373 etf.small_tuple(1); 374 374 etf.small_integer(1); 375 375 assert_eq!(etf.into_vec(), [131, 104, 1, 97, 1]) ··· 377 377 378 378 #[test] 379 379 fn small_tuple() { 380 - let mut etf = Etf::new(); 380 + let mut etf = Builder::new(); 381 381 etf.small_tuple(3); 382 382 etf.new_float(1.1); 383 383 etf.integer(-1); ··· 394 394 395 395 #[test] 396 396 fn small_nested_tuple() { 397 - let mut etf = Etf::new(); 397 + let mut etf = Builder::new(); 398 398 etf.small_tuple(2); 399 399 400 400 etf.small_tuple(2); ··· 415 415 416 416 #[test] 417 417 fn large_tuple() { 418 - let mut etf = Etf::new(); 418 + let mut etf = Builder::new(); 419 419 etf.large_tuple(500); 420 420 for _ in 1..=500 { 421 421 etf.small_integer(1); ··· 472 472 473 473 #[test] 474 474 fn large_big() { 475 - let mut etf = Etf::new(); 475 + let mut etf = Builder::new(); 476 476 etf.large_big(BigInt::from_str(&"1".repeat(1500)).unwrap()); 477 477 assert_eq!( 478 478 etf.into_vec(), ··· 518 518 519 519 #[test] 520 520 fn negative_large_big() { 521 - let mut etf = Etf::new(); 521 + let mut etf = Builder::new(); 522 522 etf.large_big(BigInt::from_str(&"1".repeat(1500)).unwrap().neg()); 523 523 assert_eq!( 524 524 etf.into_vec(),