{"id":19381402,"url":"https://github.com/lukaskalbertodt/litrs","last_synced_at":"2025-04-09T19:19:19.064Z","repository":{"id":62442323,"uuid":"369333571","full_name":"LukasKalbertodt/litrs","owner":"LukasKalbertodt","description":"Parsing and inspecting Rust literals (particularly useful for proc macros)","archived":false,"fork":false,"pushed_at":"2023-10-18T18:59:52.000Z","size":191,"stargazers_count":43,"open_issues_count":6,"forks_count":5,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-09T19:19:13.272Z","etag":null,"topics":["literal","parser","proc-macro","rust-macro"],"latest_commit_sha":null,"homepage":"https://docs.rs/litrs","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LukasKalbertodt.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-20T20:44:07.000Z","updated_at":"2025-03-06T22:13:27.000Z","dependencies_parsed_at":"2024-06-19T14:58:52.368Z","dependency_job_id":"a9d246bb-bd45-4c8b-8722-7781f33ec9e0","html_url":"https://github.com/LukasKalbertodt/litrs","commit_stats":{"total_commits":133,"total_committers":2,"mean_commits":66.5,"dds":0.007518796992481258,"last_synced_commit":"5cecfbb3f86cbfb3d0c6ec5209f41bb92a76bc76"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LukasKalbertodt%2Flitrs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LukasKalbertodt%2Flitrs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LukasKalbertodt%2Flitrs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LukasKalbertodt%2Flitrs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LukasKalbertodt","download_url":"https://codeload.github.com/LukasKalbertodt/litrs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248094991,"owners_count":21046770,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["literal","parser","proc-macro","rust-macro"],"created_at":"2024-11-10T09:16:57.224Z","updated_at":"2025-04-09T19:19:19.043Z","avatar_url":"https://github.com/LukasKalbertodt.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `litrs`: parsing and inspecting Rust literals\n\n[\u003cimg alt=\"CI status of main\" src=\"https://img.shields.io/github/actions/workflow/status/LukasKalbertodt/litrs/ci.yml?branch=main\u0026label=CI\u0026logo=github\u0026logoColor=white\u0026style=for-the-badge\" height=\"23\"\u003e](https://github.com/LukasKalbertodt/litrs/actions/workflows/ci.yml)\n[\u003cimg alt=\"Crates.io Version\" src=\"https://img.shields.io/crates/v/litrs?logo=rust\u0026style=for-the-badge\" height=\"23\"\u003e](https://crates.io/crates/litrs)\n[\u003cimg alt=\"docs.rs\" src=\"https://img.shields.io/crates/v/litrs?color=blue\u0026label=docs\u0026style=for-the-badge\" height=\"23\"\u003e](https://docs.rs/litrs)\n\n`litrs` offers functionality to parse Rust literals, i.e. tokens in the Rust programming language that represent fixed values.\nFor example: `27`, `\"crab\"`, `bool`.\nThis is particularly useful for proc macros, but can also be used outside of a proc-macro context.\n\n**Why this library?**\nUnfortunately, the `proc_macro` API shipped with the compiler offers no easy way to inspect literals.\nThere are mainly two libraries for this purpose:\n[`syn`](https://github.com/dtolnay/syn) and [`literalext`](https://github.com/mystor/literalext).\nThe latter is deprecated.\nAnd `syn` is oftentimes overkill for the task at hand, especially when developing function-like proc-macros (e.g. `foo!(..)`).\nThis crate is a lightweight alternative.\nAlso, when it comes to literals, `litrs` offers a bit more flexibility and a few more features compared to `syn`.\n\nI'm interested in community feedback!\nIf you consider using this, please speak your mind [in this issue](https://github.com/LukasKalbertodt/litrs/issues/1).\n\n## Example\n\n### In proc macro\n\n```rust\nuse std::convert::TryFrom;\nuse proc_macro::TokenStream;\nuse litrs::Literal;\n\n#[proc_macro]\npub fn foo(input: TokenStream) -\u003e TokenStream {\n    // Please do proper error handling in your real code!\n    let first_token = input.into_iter().next().expect(\"no input\");\n\n    // `try_from` will return an error if the token is not a literal.\n    match Literal::try_from(first_token) {\n        // Convenient methods to produce decent errors via `compile_error!`.\n        Err(e) =\u003e return e.to_compile_error(),\n\n        // You can now inspect your literal!\n        Ok(Literal::Integer(i)) =\u003e {\n            println!(\"Got an integer specified in base {:?}\", i.base());\n\n            let value = i.value::\u003cu64\u003e().expect(\"integer literal too large\");\n            println!(\"Is your integer even? {}\", value % 2 == 0);\n        }\n        Ok(other) =\u003e {\n            println!(\"Got a non-integer literal\");\n        }\n    }\n\n    TokenStream::new() // dummy output\n}\n```\n\nIf you are expecting a specific kind of literal, you can also use this, which will return an error if the token is not a float literal.\n\n```rust\nFloatLit::try_from(first_token)\n```\n\n### Parsing from a `\u0026str`\n\nOutside of a proc macro context you might want to parse a string directly.\n\n```rust\nuse litrs::{FloatLit, Literal};\n\nlet lit = Literal::parse(\"'🦀'\").expect(\"failed to parse literal\");\nlet float_lit = FloatLit::parse(\"2.7e3\").expect(\"failed to parse as float literal\");\n```\n\nSee [**the documentation**](https://docs.rs/litrs) or the `examples/` directory for more examples and information.\n\n\n\u003cbr /\u003e\n\n---\n\n## License\n\nLicensed under either of \u003ca href=\"LICENSE-APACHE\"\u003eApache License, Version\n2.0\u003c/a\u003e or \u003ca href=\"LICENSE-MIT\"\u003eMIT license\u003c/a\u003e at your option.\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in this project by you, as defined in the Apache-2.0 license,\nshall be dual licensed as above, without any additional terms or conditions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukaskalbertodt%2Flitrs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flukaskalbertodt%2Flitrs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukaskalbertodt%2Flitrs/lists"}