{"id":13822413,"url":"https://github.com/rodrimati1992/konst","last_synced_at":"2025-05-15T20:07:12.623Z","repository":{"id":42676852,"uuid":"349795356","full_name":"rodrimati1992/konst","owner":"rodrimati1992","description":"Const equivalents of std functions, compile-time comparison, and parsing.","archived":false,"fork":false,"pushed_at":"2024-12-29T19:28:07.000Z","size":825,"stargazers_count":98,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-05-08T15:54:00.688Z","etag":null,"topics":["compile-time","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"zlib","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rodrimati1992.png","metadata":{"files":{"readme":"README.md","changelog":"Changelog.md","contributing":null,"funding":null,"license":"LICENSE-ZLIB.md","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-03-20T17:43:00.000Z","updated_at":"2025-05-08T02:17:30.000Z","dependencies_parsed_at":"2024-06-21T13:08:44.760Z","dependency_job_id":"8604fc6d-96a5-42b2-b319-badd2714f72f","html_url":"https://github.com/rodrimati1992/konst","commit_stats":{"total_commits":250,"total_committers":1,"mean_commits":250.0,"dds":0.0,"last_synced_commit":"5c0c45febbf03baba073d649397c032d3c88bf9d"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodrimati1992%2Fkonst","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodrimati1992%2Fkonst/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodrimati1992%2Fkonst/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodrimati1992%2Fkonst/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rodrimati1992","download_url":"https://codeload.github.com/rodrimati1992/konst/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254414499,"owners_count":22067272,"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":["compile-time","rust"],"created_at":"2024-08-04T08:01:59.393Z","updated_at":"2025-05-15T20:07:07.548Z","avatar_url":"https://github.com/rodrimati1992.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"[![Rust](https://github.com/rodrimati1992/konst/workflows/Rust/badge.svg)](https://github.com/rodrimati1992/konst/actions)\n[![crates-io](https://img.shields.io/crates/v/konst.svg)](https://crates.io/crates/konst)\n[![api-docs](https://docs.rs/konst/badge.svg)](https://docs.rs/konst/*)\n\nConst equivalents of std functions and const parsing.\n\n# Features\n\nThis crate provides:\n\n- Const fn equivalents of standard library functions and methods.\n\n- [`destructure`] macro to allow destructuring types in const without getting \"cannot drop in const\" errors.\n\n- Compile-time parsing through the [`Parser`] type, and [`parser_method`] macro.\n\n# Examples\n\n### Parsing an enum\n\nThis example demonstrates how you can parse a simple enum from an environment variable,\nat compile-time.\n\n```rust\nuse konst::{\n    eq_str,\n    option,\n    result::unwrap_ctx,\n};\n\n#[derive(Debug, PartialEq)]\nenum Direction {\n    Forward,\n    Backward,\n    Left,\n    Right,\n}\n\nimpl Direction {\n    const fn try_parse(input: \u0026str) -\u003e Result\u003cSelf, ParseDirectionError\u003e {\n        // As of Rust 1.65.0, string patterns don't work in const contexts\n        match () {\n            _ if eq_str(input, \"forward\") =\u003e Ok(Direction::Forward),\n            _ if eq_str(input, \"backward\") =\u003e Ok(Direction::Backward),\n            _ if eq_str(input, \"left\") =\u003e Ok(Direction::Left),\n            _ if eq_str(input, \"right\") =\u003e Ok(Direction::Right),\n            _ =\u003e Err(ParseDirectionError),\n        }\n    }\n}\n\nconst CHOICE: \u0026str = option::unwrap_or!(option_env!(\"chosen-direction\"), \"forward\");\n\nconst DIRECTION: Direction = unwrap_ctx!(Direction::try_parse(CHOICE));\n\nfn main() {\n    match DIRECTION {\n        Direction::Forward =\u003e assert_eq!(CHOICE, \"forward\"),\n        Direction::Backward =\u003e assert_eq!(CHOICE, \"backward\"),\n        Direction::Left =\u003e assert_eq!(CHOICE, \"left\"),\n        Direction::Right =\u003e assert_eq!(CHOICE, \"right\"),\n    }\n}\n\n#[derive(Debug, PartialEq)]\npub struct ParseDirectionError;\n\nuse std::fmt::{self, Display};\n\nimpl Display for ParseDirectionError {\n    fn fmt(\u0026self, f: \u0026mut fmt::Formatter\u003c'_\u003e) -\u003e fmt::Result {\n        f.write_str(\"Failed to parse a Direction\")\n    }\n}\n\nimpl ParseDirectionError {\n    const fn panic(\u0026self) -\u003e ! {\n        panic!(\"failed to parse a Direction\")\n    }\n}\n\n```\n\n### Parsing CSV\n\nThis example demonstrates how CSV can be parsed into integers.\n\nThis example requires the `\"parsing\"` and `\"iter\"` features\n(both are enabled by default).\n\n```rust\nuse konst::{\n    primitive::parse_u64,\n    result::unwrap_ctx,\n    iter, string,\n};\n\nconst CSV: \u0026str = \"3, 8, 13, 21, 34\";\n\nstatic PARSED: [u64; 5] = iter::collect_const!(u64 =\u003e\n    string::split(CSV, \",\"),\n        map(string::trim),\n        map(|s| unwrap_ctx!(parse_u64(s))),\n);\n\nassert_eq!(PARSED, [3, 8, 13, 21, 34]);\n\n```\n\n### Parsing a struct\n\nThis example demonstrates how a key-value pair format can be parsed into a struct.\n\nThis requires the `\"parsing\"` feature (enabled by default).\n\n```rust\nuse konst::{\n    parsing::{Parser, ParseValueResult},\n    eq_str,\n    for_range, parser_method, try_, unwrap_ctx,\n};\n\nconst PARSED: Struct = {\n    // You can also parse strings from environment variables, or from an `include_str!(....)`\n    let input = \"\\\n        colors = red, blue, green, blue\n        amount = 1000\n        repeating = circle\n        name = bob smith\n    \";\n    \n    unwrap_ctx!(parse_struct(Parser::new(input))).0\n};\n\nfn main(){\n    assert_eq!(\n        PARSED,\n        Struct{\n            name: \"bob smith\",\n            amount: 1000,\n            repeating: Shape::Circle,\n            colors: [Color::Red, Color::Blue, Color::Green, Color::Blue],\n        }\n    );\n}\n\n#[derive(Debug, Clone, PartialEq, Eq)]\npub struct Struct\u003c'a\u003e {\n    pub name: \u0026'a str,\n    pub amount: usize,\n    pub repeating: Shape,\n    pub colors: [Color; 4],\n}\n\n#[derive(Debug, Clone, PartialEq, Eq)]\npub enum Shape {\n    Circle,\n    Square,\n    Line,\n}\n\n#[derive(Debug, Copy, Clone, PartialEq, Eq)]\npub enum Color {\n    Red,\n    Blue,\n    Green,\n}\n\npub const fn parse_struct(mut parser: Parser\u003c'_\u003e) -\u003e ParseValueResult\u003c'_, Struct\u003c'_\u003e\u003e {\n    let mut name = \"\u003cnone\u003e\";\n    let mut amount = 0;\n    let mut repeating = Shape::Circle;\n    let mut colors = [Color::Red; 4];\n    \n    parser = parser.trim_end();\n    if !parser.is_empty() {\n        loop {\n            let mut prev_parser = parser.trim_start();\n\n            parser = try_!(parser.find_skip('='));\n\n            parser_method!{prev_parser, strip_prefix;\n                \"name\" =\u003e (name, parser) = try_!(parser.trim_start().split_keep('\\n')),\n                \"amount\" =\u003e (amount, parser) = try_!(parser.trim_start().parse_usize()),\n                \"repeating\" =\u003e (repeating, parser) = try_!(parse_shape(parser.trim_start())),\n                \"colors\" =\u003e (colors, parser) = try_!(parse_colors(parser.trim_start())),\n                _ =\u003e {\n                    let err = \u0026\"could not parse Struct field name\";\n                    return Err(prev_parser.into_other_error(err));\n                }\n            }\n\n            if parser.is_empty() {\n                break\n            }\n            parser = try_!(parser.strip_prefix(\"\\n\"));\n        }\n    }\n\n    Ok((Struct{name, amount, repeating, colors}, parser))\n}\n\npub const fn parse_shape(mut parser: Parser\u003c'_\u003e) -\u003e ParseValueResult\u003c'_, Shape\u003e {\n    let shape = parser_method!{parser, strip_prefix;\n        \"circle\" =\u003e Shape::Circle,\n        \"square\" =\u003e Shape::Square,\n        \"line\" =\u003e Shape::Line,\n        _ =\u003e return Err(parser.into_other_error(\u0026\"could not parse Shape\"))\n    };\n    Ok((shape, parser))\n}\n\npub const fn parse_colors\u003cconst LEN: usize\u003e(\n    mut parser: Parser\u003c'_\u003e,\n) -\u003e ParseValueResult\u003c'_, [Color; LEN]\u003e {\n    let mut colors = [Color::Red; LEN];\n\n    for_range!{i in 0..LEN =\u003e\n        (colors[i], parser) = try_!(parse_color(parser.trim_start()));\n        \n        match parser.strip_prefix(\",\") {\n            Ok(next) =\u003e parser = next,\n            Err(_) if i == LEN - 1 =\u003e {}\n            Err(e) =\u003e return Err(e),\n        }\n    }\n\n    Ok((colors, parser))\n}\n\npub const fn parse_color(mut parser: Parser\u003c'_\u003e) -\u003e ParseValueResult\u003c'_, Color\u003e {\n    let color = parser_method!{parser, strip_prefix;\n        \"red\" =\u003e Color::Red,\n        \"blue\" =\u003e Color::Blue,\n        \"green\" =\u003e Color::Green,\n        _ =\u003e return Err(parser.into_other_error(\u0026\"could not parse Color\"))\n    };\n    Ok((color, parser))\n}\n\n\n\n```\n\n# Cargo features\n\nThese are the features of these crates:\n\n- `\"iter\"`(enabled by default):\nEnables all iteration items, including macros/functions that take/return iterators,\n\n- `\"cmp\"`(enabled by default):\nEnables all comparison functions and macros,\nthe string equality and ordering comparison functions don't require this feature.\n\n- `\"parsing_proc\"`(enabled by default):\nEnables the `\"parsing\"` feature, compiles the `konst_proc_macros` dependency,\nand enables the [`parser_method`] macro.\nYou can use this feature instead of `\"parsing\"` if the slightly longer\ncompile times aren't a problem.\n\n- `\"parsing\"`(enabled by default):\nEnables the [`parsing`] module (for parsing from `\u0026str` and `\u0026[u8]`),\nthe `primitive::parse_*` functions, `try_rebind`, and `rebind_if_ok` macros.\n\n- `\"alloc\"`:\nEnables items that use types from the [`alloc`] crate, including `Vec` and `String`.\n\n### Rust release related\n\nNone of thse features are enabled by default.\n\n- `\"rust_latest_stable\"`: enables the latest `\"rust_1_*\"` feature.\nOnly recommendable if you can update the Rust compiler every stable release.\n\n- `\"rust_1_83\"`: \nEnables const functions that take mutable references,\n`array::{from_fn_, map_}` macros, and [`destructure`] macro.\n\n# No-std support\n\n`konst` is `#![no_std]`, it can be used anywhere Rust can be used.\n\n# Minimum Supported Rust Version\n\n`konst` requires Rust 1.65.0.\n\nFeatures that require newer versions of Rust, or the nightly compiler,\nneed to be explicitly enabled with crate features.\n\n\n[`alloc`]: https://doc.rust-lang.org/alloc/\n[`const_eq`]: https://docs.rs/konst/*/konst/macro.const_eq.html\n[`const_eq_for`]: https://docs.rs/konst/*/konst/macro.const_eq_for.html\n[`const_cmp`]: https://docs.rs/konst/*/konst/macro.const_cmp.html\n[`const_cmp_for`]: https://docs.rs/konst/*/konst/macro.const_cmp_for.html\n[`polymorphism`]: https://docs.rs/konst/*/konst/polymorphism/index.html\n[`parsing`]: https://docs.rs/konst/*/konst/parsing/index.html\n[`primitive`]: https://docs.rs/konst/*/konst/primitive/index.html\n[`parser_method`]: https://docs.rs/konst/*/konst/macro.parser_method.html\n[`Parser`]: https://docs.rs/konst/*/konst/parsing/struct.Parser.html\n[`Parser::parse_u128`]: https://docs.rs/konst/*/konst/parsing/struct.Parser.html#method.parse_u128\n[`destructure`]: https://docs.rs/konst/*/konst/macro.destructure.html","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frodrimati1992%2Fkonst","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frodrimati1992%2Fkonst","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frodrimati1992%2Fkonst/lists"}