{"id":16687856,"url":"https://github.com/picohz/mediatype","last_synced_at":"2025-05-16T11:06:18.003Z","repository":{"id":57637691,"uuid":"459541120","full_name":"picoHz/mediatype","owner":"picoHz","description":"MIME Media-type parsing for Rust","archived":false,"fork":false,"pushed_at":"2025-03-03T09:33:16.000Z","size":298,"stargazers_count":52,"open_issues_count":1,"forks_count":8,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-16T11:06:11.401Z","etag":null,"topics":["media-type","media-types","mime","rust"],"latest_commit_sha":null,"homepage":"https://docs.rs/mediatype","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/picoHz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2022-02-15T11:01:22.000Z","updated_at":"2025-05-05T00:30:09.000Z","dependencies_parsed_at":"2025-03-06T03:11:28.500Z","dependency_job_id":"ab79a42f-ba59-47b3-972c-2992f7e26985","html_url":"https://github.com/picoHz/mediatype","commit_stats":{"total_commits":179,"total_committers":10,"mean_commits":17.9,"dds":0.4860335195530726,"last_synced_commit":"29921e91f7176784d4ed1fe42ca40f8a8f225941"},"previous_names":[],"tags_count":35,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/picoHz%2Fmediatype","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/picoHz%2Fmediatype/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/picoHz%2Fmediatype/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/picoHz%2Fmediatype/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/picoHz","download_url":"https://codeload.github.com/picoHz/mediatype/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254518383,"owners_count":22084374,"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":["media-type","media-types","mime","rust"],"created_at":"2024-10-12T15:25:24.746Z","updated_at":"2025-05-16T11:06:12.964Z","avatar_url":"https://github.com/picoHz.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n# MediaType\n\nMIME Media-type parsing for Rust\n\n[![Crates.io](https://img.shields.io/crates/v/mediatype.svg)](https://crates.io/crates/mediatype)\n[![dependency status](https://deps.rs/crate/mediatype/0.19.3/status.svg)](https://deps.rs/crate/mediatype/0.19.3)\n[![GitHub license](https://img.shields.io/github/license/picoHz/mediatype.svg)](https://github.com/picoHz/mediatype/blob/main/LICENSE)\n[![Rustdoc](https://img.shields.io/badge/doc-rustdoc-green.svg)](https://docs.rs/mediatype)\n![Rust](https://github.com/picoHz/mediatype/workflows/Rust/badge.svg)\n\n\u003c/div\u003e\n\n- [Parsing](#parsing)\n- [Construction](#construction)\n- [Parameters](#parameters)\n  - [Case Sensitivity](#case-sensitivity)\n  - [Duplicate Parameter Names](#duplicate-parameter-names)\n- [Owned Type](#owned-type)\n- [MediaTypeList](#mediatypelist)\n- [Serialize and Deserialize](serialize-and-deserialize)\n\n## Parsing\n\n`MediaType::parse` runs a zero-copy parsing: A `MediaType` borrows the input string instead of copying it. \n\nIf you need an owned type, use [`MediaTypeBuf`](#owned-type).\n\n```rust\nuse mediatype::{names::*, values::*, MediaType};\n\nlet madia_type = \"image/svg+xml; charset=UTF-8\";\nlet svg = MediaType::parse(madia_type).unwrap();\n\nassert_eq!(svg.ty, IMAGE);\nassert_eq!(svg.subty, SVG);\nassert_eq!(svg.suffix, Some(XML));\nassert_eq!(svg.get_param(CHARSET), Some(UTF_8));\n```\n\n## Construction\n\n`MediaType` is const-constructible. It can be defined as a constant. \n\nPredefind names and values are defined in [`names`](https://docs.rs/mediatype/latest/mediatype/names/index.html) and [`values`](https://docs.rs/mediatype/latest/mediatype/values/index.html) modules.\n\n```rust\nuse mediatype::{names::*, values::*, media_type, MediaType};\n\nconst TEXT_PLAIN: MediaType = MediaType::new(TEXT, PLAIN);\n\nconst IMAGE_SVG: MediaType = \n  MediaType::from_parts(TEXT, PLAIN, Some(XML), \u0026[(CHARSET, UTF_8)]);\n\nconst TEXT_MARKDOWN: MediaType = \n  media_type!(TEXT/MARKDOWN; CHARSET=UTF_8);\n```\n\n## Parameters\n\n### Case Sensitivity\n\nComparisons are case-insensitive except parameter values.\n\n```rust\nlet text_plain_lower = MediaType::parse(\"text/plain; charset=UTF-8\").unwrap();\nlet text_plain_upper = MediaType::parse(\"TEXT/PLAIN; CHARSET=UTF-8\").unwrap();\n\nassert_eq!(text_plain_lower, text_plain_upper);\nassert_eq!(text_plain_lower.ty(), \"Text\");\nassert_eq!(text_plain_upper.subty(), \"Plain\");\nassert!(text_plain_lower != \n  MediaType::parse(\"text/plain; charset=utf-8\").unwrap());\n```\n\n### Duplicate Parameter Names\n\nThe parser does not report duplicate parameter names as an error, but `MediaType` recognizes only the last value.\n\n```rust\nlet text_plain = MediaType::parse(\n  \"text/plain; charset=US-ASCII; charset=UTF-8\").unwrap();\n\nassert_eq!(\n    text_plain.to_string(),\n    \"text/plain; charset=US-ASCII; charset=UTF-8\"\n);\n\n// Return the last charset value.\nassert_eq!(text_plain.get_param(CHARSET), Some(UTF_8));\n\n// Compare the last charset value.\nassert_eq!(\n    text_plain,\n    MediaType::parse(\"text/plain; charset=UTF-8\").unwrap()\n);\n```\n\n## Owned Type\n \n[`MediaTypeBuf`](https://docs.rs/mediatype/latest/mediatype/struct.MediaTypeBuf.html) is an owned version of `MediaType`.\nIt is immutable but optimized for minimal stack and heap usage.\n\n```rust\nuse mediatype::{names::*, values::*, MediaType, MediaTypeBuf};\n\nlet text_plain: MediaTypeBuf = \"text/plain; charset=UTF-8\".parse().unwrap();\nassert_eq!(text_plain.get_param(CHARSET).unwrap(), UTF_8);\n\n// Convert to MediaType\nlet mut text_markdown: MediaType = text_plain.to_ref();\ntext_markdown.subty = MARKDOWN;\nassert_eq!(text_markdown.to_string(), \"text/markdown; charset=UTF-8\");\n```\n\n## MediaTypeList\n\n[`MediaTypeList`](https://docs.rs/mediatype/latest/mediatype/struct.MediaTypeList.html) parses a comma-separated list of `MediaType`s used in the HTTP `Accept` header. ([RFC 7231](https://www.rfc-editor.org/rfc/rfc7231#section-5.3.2))\n\n```rust\nuse mediatype::{MediaType, MediaTypeList};\n\nlet mut list = MediaTypeList::new(\n    \"text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8\",\n);\nassert_eq!(list.next(), Some(MediaType::parse(\"text/html\")));\nassert_eq!(list.next(), Some(MediaType::parse(\"application/xhtml+xml\")));\nassert_eq!(list.next(), Some(MediaType::parse(\"application/xml;q=0.9\")));\nassert_eq!(list.next(), Some(MediaType::parse(\"*/*;q=0.8\")));\nassert_eq!(list.next(), None);\n```\n\n## Serialize and Deserialize\n\nTo enable serialization and deserialization, specify `serde` feature in `Cargo.toml`.\n\n```toml\nmediatype = { version = \"...\", features = [\"serde\"] }\n```\n\n```rust\nlet json = r#\"\n    [\n        \"text/plain\",\n        \"image/svg+xml; charset=UTF-8\"\n    ]\n\"#;\n\nlet decoded: Vec\u003cMediaType\u003e = serde_json::from_str(json).unwrap();\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpicohz%2Fmediatype","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpicohz%2Fmediatype","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpicohz%2Fmediatype/lists"}