|
1 | 1 | use crate::common_args;
|
2 | 2 | use crate::config::Config;
|
3 | 3 | use crate::edit_distance::{edit_distance, find_best_match_for_name};
|
4 |
| -use crate::generate::rust::{write_arglist_no_delimiters, write_type}; |
5 | 4 | use crate::util;
|
6 | 5 | use crate::util::{add_auth_header_opt, database_address, get_auth_header_only};
|
7 | 6 | use anyhow::{bail, Context, Error};
|
@@ -191,10 +190,10 @@ fn reducer_signature(schema_json: Value, reducer_name: &str) -> Option<String> {
|
191 | 190 | fn ctx(typespace: &Typespace, r: AlgebraicTypeRef) -> String {
|
192 | 191 | let ty = &typespace[r];
|
193 | 192 | let mut ty_str = String::new();
|
194 |
| - write_type(&|r| ctx(typespace, r), &mut ty_str, ty).unwrap(); |
| 193 | + write_type::write_type(&|r| ctx(typespace, r), &mut ty_str, ty).unwrap(); |
195 | 194 | ty_str
|
196 | 195 | }
|
197 |
| - write_arglist_no_delimiters(&|r| ctx(&typespace, r), &mut args, ¶ms, None).unwrap(); |
| 196 | + write_type::write_arglist_no_delimiters(&|r| ctx(&typespace, r), &mut args, ¶ms, None).unwrap(); |
198 | 197 | let args = args.trim().trim_end_matches(',').replace('\n', " ");
|
199 | 198 |
|
200 | 199 | // Print the full signature to `reducer_fmt`.
|
@@ -322,3 +321,136 @@ fn typespace(value: &serde_json::Value) -> Option<Typespace> {
|
322 | 321 | let types = value.as_object()?.get("typespace")?;
|
323 | 322 | deserialize_from(types).map(Typespace::new).ok()
|
324 | 323 | }
|
| 324 | + |
| 325 | +// this is an old version of code in generate::rust that got |
| 326 | +// refactored, but reducer_signature() was using it |
| 327 | +// TODO: port reducer_signature() to use AlgebraicTypeUse et al, somehow. |
| 328 | +mod write_type { |
| 329 | + use super::*; |
| 330 | + use convert_case::{Case, Casing}; |
| 331 | + use spacetimedb_lib::sats::ArrayType; |
| 332 | + use spacetimedb_lib::ProductType; |
| 333 | + use std::fmt; |
| 334 | + use std::ops::Deref; |
| 335 | + |
| 336 | + pub fn write_type<W: fmt::Write>( |
| 337 | + ctx: &impl Fn(AlgebraicTypeRef) -> String, |
| 338 | + out: &mut W, |
| 339 | + ty: &AlgebraicType, |
| 340 | + ) -> fmt::Result { |
| 341 | + match ty { |
| 342 | + p if p.is_identity() => write!(out, "Identity")?, |
| 343 | + p if p.is_address() => write!(out, "Address")?, |
| 344 | + p if p.is_schedule_at() => write!(out, "ScheduleAt")?, |
| 345 | + AlgebraicType::Sum(sum_type) => { |
| 346 | + if let Some(inner_ty) = sum_type.as_option() { |
| 347 | + write!(out, "Option<")?; |
| 348 | + write_type(ctx, out, inner_ty)?; |
| 349 | + write!(out, ">")?; |
| 350 | + } else { |
| 351 | + write!(out, "enum ")?; |
| 352 | + print_comma_sep_braced(out, &sum_type.variants, |out: &mut W, elem: &_| { |
| 353 | + if let Some(name) = &elem.name { |
| 354 | + write!(out, "{name}: ")?; |
| 355 | + } |
| 356 | + write_type(ctx, out, &elem.algebraic_type) |
| 357 | + })?; |
| 358 | + } |
| 359 | + } |
| 360 | + AlgebraicType::Product(ProductType { elements }) => { |
| 361 | + print_comma_sep_braced(out, elements, |out: &mut W, elem: &ProductTypeElement| { |
| 362 | + if let Some(name) = &elem.name { |
| 363 | + write!(out, "{name}: ")?; |
| 364 | + } |
| 365 | + write_type(ctx, out, &elem.algebraic_type) |
| 366 | + })?; |
| 367 | + } |
| 368 | + AlgebraicType::Bool => write!(out, "bool")?, |
| 369 | + AlgebraicType::I8 => write!(out, "i8")?, |
| 370 | + AlgebraicType::U8 => write!(out, "u8")?, |
| 371 | + AlgebraicType::I16 => write!(out, "i16")?, |
| 372 | + AlgebraicType::U16 => write!(out, "u16")?, |
| 373 | + AlgebraicType::I32 => write!(out, "i32")?, |
| 374 | + AlgebraicType::U32 => write!(out, "u32")?, |
| 375 | + AlgebraicType::I64 => write!(out, "i64")?, |
| 376 | + AlgebraicType::U64 => write!(out, "u64")?, |
| 377 | + AlgebraicType::I128 => write!(out, "i128")?, |
| 378 | + AlgebraicType::U128 => write!(out, "u128")?, |
| 379 | + AlgebraicType::I256 => write!(out, "i256")?, |
| 380 | + AlgebraicType::U256 => write!(out, "u256")?, |
| 381 | + AlgebraicType::F32 => write!(out, "f32")?, |
| 382 | + AlgebraicType::F64 => write!(out, "f64")?, |
| 383 | + AlgebraicType::String => write!(out, "String")?, |
| 384 | + AlgebraicType::Array(ArrayType { elem_ty }) => { |
| 385 | + write!(out, "Vec<")?; |
| 386 | + write_type(ctx, out, elem_ty)?; |
| 387 | + write!(out, ">")?; |
| 388 | + } |
| 389 | + AlgebraicType::Map(ty) => { |
| 390 | + write!(out, "Map<")?; |
| 391 | + write_type(ctx, out, &ty.key_ty)?; |
| 392 | + write!(out, ", ")?; |
| 393 | + write_type(ctx, out, &ty.ty)?; |
| 394 | + write!(out, ">")?; |
| 395 | + } |
| 396 | + AlgebraicType::Ref(r) => { |
| 397 | + write!(out, "{}", ctx(*r))?; |
| 398 | + } |
| 399 | + } |
| 400 | + Ok(()) |
| 401 | + } |
| 402 | + |
| 403 | + fn print_comma_sep_braced<W: fmt::Write, T>( |
| 404 | + out: &mut W, |
| 405 | + elems: &[T], |
| 406 | + on: impl Fn(&mut W, &T) -> fmt::Result, |
| 407 | + ) -> fmt::Result { |
| 408 | + write!(out, "{{")?; |
| 409 | + |
| 410 | + let mut iter = elems.iter(); |
| 411 | + |
| 412 | + // First factor. |
| 413 | + if let Some(elem) = iter.next() { |
| 414 | + write!(out, " ")?; |
| 415 | + on(out, elem)?; |
| 416 | + } |
| 417 | + // Other factors. |
| 418 | + for elem in iter { |
| 419 | + write!(out, ", ")?; |
| 420 | + on(out, elem)?; |
| 421 | + } |
| 422 | + |
| 423 | + if !elems.is_empty() { |
| 424 | + write!(out, " ")?; |
| 425 | + } |
| 426 | + |
| 427 | + write!(out, "}}")?; |
| 428 | + |
| 429 | + Ok(()) |
| 430 | + } |
| 431 | + |
| 432 | + pub fn write_arglist_no_delimiters( |
| 433 | + ctx: &impl Fn(AlgebraicTypeRef) -> String, |
| 434 | + out: &mut impl Write, |
| 435 | + elements: &[ProductTypeElement], |
| 436 | + |
| 437 | + // Written before each line. Useful for `pub`. |
| 438 | + prefix: Option<&str>, |
| 439 | + ) -> fmt::Result { |
| 440 | + for elt in elements { |
| 441 | + if let Some(prefix) = prefix { |
| 442 | + write!(out, "{prefix} ")?; |
| 443 | + } |
| 444 | + |
| 445 | + let Some(name) = &elt.name else { |
| 446 | + panic!("Product type element has no name: {elt:?}"); |
| 447 | + }; |
| 448 | + let name = name.deref().to_case(Case::Snake); |
| 449 | + |
| 450 | + write!(out, "{name}: ")?; |
| 451 | + write_type(ctx, out, &elt.algebraic_type)?; |
| 452 | + writeln!(out, ",")?; |
| 453 | + } |
| 454 | + Ok(()) |
| 455 | + } |
| 456 | +} |
0 commit comments