| Crates.io | syn-locator |
| lib.rs | syn-locator |
| version | 0.1.4 |
| created_at | 2025-04-06 07:23:46.214746+00 |
| updated_at | 2026-01-25 04:06:41.44382+00 |
| description | A simple source code locator for syn crate |
| homepage | |
| repository | https://github.com/ecoricemon/syn-locator |
| max_upload_size | |
| id | 1622684 |
| size | 196,738 |
syn-locator helps you to find source code locations of syn nodes.
If you read a rust file then parse the read string through the syn::parse_str,
you will lose span information. This crate finds source code locations of syntax
tree nodes by simple string comparison.
use syn_locator::*;
// Assumes that we read this code from a file.
let file_path = "/path/to/file.rs";
let code = "
struct Foo {
a: i32,
}
";
let syn = syn::parse_str::<syn::File>(code).unwrap();
// Finds location of the syntax tree.
let syn = std::pin::Pin::new(&syn);
syn.locate_as_entry(file_path, code);
// Picks a syntax tree node.
let item_struct = match &syn.items[0] {
syn::Item::Struct(item_struct) => item_struct,
_ => unreachable!()
};
let field_ty = &item_struct.fields.iter().next().unwrap().ty;
// Let's find out 'i32' from the syntax tree node.
assert_eq!(field_ty.location_message(), "/path/to/file.rs:3: i32");
assert!(matches!(field_ty.location(), Location {
start: 29, // Byte offset
end: 32,
..
}));