{"id":15705334,"url":"https://github.com/brahmlower/default-aware","last_synced_at":"2025-03-30T15:43:30.020Z","repository":{"id":188422633,"uuid":"678701095","full_name":"brahmlower/default-aware","owner":"brahmlower","description":"A tiny struct for indicating if a value was generated via the Default trait or not.","archived":false,"fork":false,"pushed_at":"2023-08-21T01:45:39.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-05-02T06:10:05.235Z","etag":null,"topics":["rust"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/default_aware","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/brahmlower.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"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":"2023-08-15T06:55:50.000Z","updated_at":"2023-08-21T06:49:40.000Z","dependencies_parsed_at":"2024-10-09T13:46:41.627Z","dependency_job_id":null,"html_url":"https://github.com/brahmlower/default-aware","commit_stats":null,"previous_names":["brahmlower/default-aware"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brahmlower%2Fdefault-aware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brahmlower%2Fdefault-aware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brahmlower%2Fdefault-aware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brahmlower%2Fdefault-aware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brahmlower","download_url":"https://codeload.github.com/brahmlower/default-aware/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246342713,"owners_count":20761938,"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":["rust"],"created_at":"2024-10-03T20:15:27.944Z","updated_at":"2025-03-30T15:43:29.997Z","avatar_url":"https://github.com/brahmlower.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Default-Aware\n\nA tiny struct for indicating if a value was generated via the Default trait or not.\n\nInstallation:\n\n```toml\n[dependencies]\ndefault_aware = \"0.1.0\"\n```\n\n## Usage\n\nIf you're deserializing a struct and want to behavior based on a value being explicitly set or not, you can use the `DefaultAware` struct to wrap the type in question, and then inspect it after derserializing to see if the value was created by the `Default` trait, or if it came from the deserialized source:\n\n```rust\nuse default_aware::DefaultAware;\nuse serde::{self, Deserialize};\nuse serde_json;\n\n// This represents some value you might deserialize, which\n// implements the `Default` trait.\n#[derive(Deserialize, PartialEq, Debug)]\nstruct Port(u32);\n\nimpl Default for Port {\n    fn default() -\u003e Self {\n        Port(8000)\n    }\n}\n\n#[derive(Deserialize)]\npub struct MyConfig {\n    #[serde(default)]\n    http_port: DefaultAware\u003cPort\u003e,\n}\n\nfn main() {\n\n    // The first config sets the port number to the same as the\n    // default. If we weren't using the `DefaultAware` wrapper, the\n    // value recieved after deserializing would be\n    // indistinguishable from the default value. Therefore we could\n    // not know if the `http_port` field was actually provided in\n    // the document or not.\n    let config1_json: \u0026str = r#\"{ \"http_port\": 8000 }\"#;\n\n    // But since we are using the `DefaultAware` wrapper, we can\n    // easily tell that the value was not created via the `Default`\n    // implementation.\n    let config1: MyConfig = serde_json::from_str(config1_json)\n        .unwrap();\n    assert_eq!(false, config1.http_port.is_default());\n    assert_eq!(Port(8000), config1.http_port.unwrap());\n\n\n    // We can demonstrate the behavior when the default value is\n    // used by checking the output\n    // on a document that omits the `http_port` field.\n    const config2_json: \u0026str = r#\"{  }\"#;\n\n    // The field has the same wrapped value as before, but notice\n    // that we tell it was created via the Default implementation.\n\n    let config2: MyConfig = serde_json::from_str(config2_json)\n        .unwrap();\n    assert_eq!(Port(8000), *config2.http_port.as_ref());\n    assert_eq!(true, config2.http_port.is_default());\n\n    // The type is just an enun, so we can use all the typical enum\n    // patterns\n    match config2.http_port {\n        DefaultAware::Default(_) =\u003e\n            println!(\"value set by default\"),\n        DefaultAware::Declared(_) =\u003e\n            println!(\"value set in the config\"),\n    }\n\n    if let DefaultAware::Default(_) = config2.http_port {\n        println!(\"Port number not set! Running on port 8000 by \\\n            default. Silence this warning by setting the \\\n            `http_port` field in your config.\");\n    }\n}\n```\n\n\n## Features\n\nSerde functionality is guarded by the feature `serde` and is enabled by default. You can use this crate without serde by listing the dependency like so:\n\n```toml\n[dependencies]\ndefault_aware = { version = \"0.1.0\", default-features = false }\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrahmlower%2Fdefault-aware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrahmlower%2Fdefault-aware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrahmlower%2Fdefault-aware/lists"}