Skip to content

jayvdb/desynt

Repository files navigation

desynt

A Rust library for handling syn objects with raw identifier prefixes and path normalization.

Crates.io Documentation License

Overview

desynt provides utilities for working with syn objects, particularly focusing on:

  • Stripping raw identifier prefixes (r#) from Ident, Path, and PathSegment objects
  • Path resolution and normalization via PathResolver for canonical type mapping
  • Built-in type support for Rust primitives, prelude types, and common std types
  • Multiple storage backends - HashMap (dynamic) or phf::Map (static)

This library is especially useful when working with proc macros or code analysis tools where you need to normalize type references regardless of how they're written in the source code.

Features

Raw Identifier Stripping

Use the StripRaw trait to remove r# prefixes from syn objects:

use syn::Ident;
use desynt::StripRaw;

let ident: Ident = syn::parse_str("r#type").unwrap();
let stripped = ident.strip_raw();
assert_eq!(stripped.to_string(), "type");

Path Resolution

Use PathResolver to normalize type paths and resolve them to canonical forms. This is particularly useful in proc macros where types can be referenced in multiple ways:

use desynt::{TypeGroups, DynamicPathResolver};
use std::collections::HashMap;
use syn::Path;

let mut mappings = HashMap::new();
mappings.insert("my_crate::types::UserId".to_string(), "UserId".to_string());

let resolver = DynamicPathResolver::from_map(mappings, TypeGroups::ALL);

// Resolves custom types
let path: Path = syn::parse_str("my_crate::types::UserId").unwrap();
assert_eq!(resolver.resolve(&path), Some("UserId"));

// Also handles type groups with various path forms
let path: Path = syn::parse_str("std::option::Option").unwrap();
assert_eq!(resolver.resolve(&path), Some("Option"));

let path: Path = syn::parse_str("Option<String>").unwrap();
assert_eq!(resolver.resolve(&path), Some("Option"));

Type Group Categories

The TypeGroups struct lets you control which standard types are automatically resolved:

  • Primitives: Language primitives (i8, u32, f64, bool, char, str, etc.)
  • Prelude: Types in the Rust prelude (String, Vec, Option, Result, Box)
  • Common std: Frequently used std types (HashMap, HashSet)

License

This project is licensed under either of

at your option.

About

Convert syn paths & idents to normalised names, removing r# raw prefix

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages