├── .gitignore ├── LICENSE ├── README.md ├── build.zig ├── build.zig.zon ├── mime.zig └── test.zig /.gitignore: -------------------------------------------------------------------------------- 1 | /.zig-cache 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (Expat) 2 | 3 | Copyright (c) contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Zig Module for MIME Types 2 | 3 | ## Synopsis 4 | 5 | ```zig 6 | const std = @import("std"); 7 | const mime = @import("mime"); 8 | 9 | test "html smoke test" { 10 | const mime_type = mime.extension_map.get(".html").?; 11 | try std.testing.expectEqualStrings("text/html", @tagName(mime_type)); 12 | } 13 | 14 | test "bogus extension" { 15 | try std.testing.expect(mime.extension_map.get(".sillybogo") == null); 16 | } 17 | ``` 18 | -------------------------------------------------------------------------------- /build.zig: -------------------------------------------------------------------------------- 1 | const std = @import("std"); 2 | 3 | pub fn build(b: *std.Build) void { 4 | const target = b.standardTargetOptions(.{}); 5 | const optimize = b.standardOptimizeOption(.{}); 6 | 7 | const module = b.addModule("mime", .{ 8 | .root_source_file = b.path("mime.zig"), 9 | .target = target, 10 | .optimize = optimize, 11 | }); 12 | 13 | const unit_tests = b.addTest(.{ 14 | .root_source_file = b.path("test.zig"), 15 | .target = target, 16 | .optimize = optimize, 17 | }); 18 | unit_tests.root_module.addImport("mime", module); 19 | const run_unit_tests = b.addRunArtifact(unit_tests); 20 | 21 | const test_step = b.step("test", "Run unit tests"); 22 | test_step.dependOn(&run_unit_tests.step); 23 | } 24 | -------------------------------------------------------------------------------- /build.zig.zon: -------------------------------------------------------------------------------- 1 | .{ 2 | .name = .mime, 3 | .version = "3.0.0", 4 | .minimum_zig_version = "0.14.0-dev.3451+d8d2aa9af", 5 | .fingerprint = 0x42858050fb8b09cf, 6 | 7 | .paths = .{ 8 | "LICENSE", 9 | "README.md", 10 | "build.zig", 11 | "build.zig.zon", 12 | "mime.zig", 13 | "test.zig", 14 | }, 15 | } 16 | -------------------------------------------------------------------------------- /mime.zig: -------------------------------------------------------------------------------- 1 | /// The integer values backing these enum tags are not protected by the 2 | /// semantic version of this package but the backing integer type is. 3 | /// The tags are guaranteed to be sorted by name. 4 | pub const Type = enum(u16) { 5 | @"application/epub+zip", 6 | @"application/gzip", 7 | @"application/java-archive", 8 | @"application/javascript", 9 | @"application/json", 10 | @"application/ld+json", 11 | @"application/msword", 12 | @"application/octet-stream", 13 | @"application/ogg", 14 | @"application/pdf", 15 | @"application/rtf", 16 | @"application/vnd.amazon.ebook", 17 | @"application/vnd.apple.installer+xml", 18 | @"application/vnd.mozilla.xul+xml", 19 | @"application/vnd.ms-excel", 20 | @"application/vnd.ms-fontobject", 21 | @"application/vnd.ms-powerpoint", 22 | @"application/vnd.oasis.opendocument.presentation", 23 | @"application/vnd.oasis.opendocument.spreadsheet", 24 | @"application/vnd.oasis.opendocument.text", 25 | @"application/vnd.openxmlformats-officedocument.presentationml.presentation", 26 | @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 27 | @"application/vnd.openxmlformats-officedocument.wordprocessingml.document", 28 | @"application/vnd.rar", 29 | @"application/vnd.visio", 30 | @"application/wasm", 31 | @"application/x-7z-compressed", 32 | @"application/x-abiword", 33 | @"application/x-bzip", 34 | @"application/x-bzip2", 35 | @"application/x-cdf", 36 | @"application/x-csh", 37 | @"application/x-freearc", 38 | @"application/x-httpd-php", 39 | @"application/x-sh", 40 | @"application/x-shockwave-flash", 41 | @"application/x-tar", 42 | @"application/xhtml+xml", 43 | @"application/xml", 44 | @"application/zip", 45 | @"audio/aac", 46 | @"audio/midi", 47 | @"audio/mpeg", 48 | @"audio/ogg", 49 | @"audio/opus", 50 | @"audio/wav", 51 | @"audio/webm", 52 | @"font/otf", 53 | @"font/ttf", 54 | @"font/woff", 55 | @"font/woff2", 56 | @"image/bmp", 57 | @"image/gif", 58 | @"image/jpeg", 59 | @"image/png", 60 | @"image/svg+xml", 61 | @"image/tiff", 62 | @"image/vnd.microsoft.icon", 63 | @"image/webp", 64 | @"text/calendar", 65 | @"text/css", 66 | @"text/csv", 67 | @"text/html", 68 | @"text/plain", 69 | @"video/3gpp", 70 | @"video/3gpp2", 71 | @"video/mp2t", 72 | @"video/mp4", 73 | @"video/mpeg", 74 | @"video/ogg", 75 | @"video/quicktime", 76 | @"video/webm", 77 | @"video/x-msvideo", 78 | }; 79 | 80 | /// Maps file extension to mime type. 81 | pub const extension_map = std.StaticStringMap(Type).initComptime(.{ 82 | .{ ".aac", .@"audio/aac" }, 83 | .{ ".abw", .@"application/x-abiword" }, 84 | .{ ".arc", .@"application/x-freearc" }, 85 | .{ ".avi", .@"video/x-msvideo" }, 86 | .{ ".azw", .@"application/vnd.amazon.ebook" }, 87 | .{ ".bin", .@"application/octet-stream" }, 88 | .{ ".bmp", .@"image/bmp" }, 89 | .{ ".bz", .@"application/x-bzip" }, 90 | .{ ".bz2", .@"application/x-bzip2" }, 91 | .{ ".cda", .@"application/x-cdf" }, 92 | .{ ".csh", .@"application/x-csh" }, 93 | .{ ".css", .@"text/css" }, 94 | .{ ".csv", .@"text/csv" }, 95 | .{ ".doc", .@"application/msword" }, 96 | .{ ".docx", .@"application/vnd.openxmlformats-officedocument.wordprocessingml.document" }, 97 | .{ ".eot", .@"application/vnd.ms-fontobject" }, 98 | .{ ".epub", .@"application/epub+zip" }, 99 | .{ ".gz", .@"application/gzip" }, 100 | .{ ".gif", .@"image/gif" }, 101 | .{ ".htm", .@"text/html" }, 102 | .{ ".html", .@"text/html" }, 103 | .{ ".ico", .@"image/vnd.microsoft.icon" }, 104 | .{ ".ics", .@"text/calendar" }, 105 | .{ ".jar", .@"application/java-archive" }, 106 | .{ ".jpg", .@"image/jpeg" }, 107 | .{ ".jpeg", .@"image/jpeg" }, 108 | .{ ".js", .@"application/javascript" }, 109 | .{ ".json", .@"application/json" }, 110 | .{ ".jsonld", .@"application/ld+json" }, 111 | .{ ".mid", .@"audio/midi" }, 112 | .{ ".mjs", .@"application/javascript" }, 113 | .{ ".mov", .@"video/quicktime" }, 114 | .{ ".mp3", .@"audio/mpeg" }, 115 | .{ ".mp4", .@"video/mp4" }, 116 | .{ ".mpeg", .@"video/mpeg" }, 117 | .{ ".mpkg", .@"application/vnd.apple.installer+xml" }, 118 | .{ ".odp", .@"application/vnd.oasis.opendocument.presentation" }, 119 | .{ ".ods", .@"application/vnd.oasis.opendocument.spreadsheet" }, 120 | .{ ".odt", .@"application/vnd.oasis.opendocument.text" }, 121 | .{ ".oga", .@"audio/ogg" }, 122 | .{ ".ogv", .@"video/ogg" }, 123 | .{ ".ogx", .@"application/ogg" }, 124 | .{ ".opus", .@"audio/opus" }, 125 | .{ ".otf", .@"font/otf" }, 126 | .{ ".png", .@"image/png" }, 127 | .{ ".pdf", .@"application/pdf" }, 128 | .{ ".php", .@"application/x-httpd-php" }, 129 | .{ ".ppt", .@"application/vnd.ms-powerpoint" }, 130 | .{ ".pptx", .@"application/vnd.openxmlformats-officedocument.presentationml.presentation" }, 131 | .{ ".rar", .@"application/vnd.rar" }, 132 | .{ ".rtf", .@"application/rtf" }, 133 | .{ ".sh", .@"application/x-sh" }, 134 | .{ ".svg", .@"image/svg+xml" }, 135 | .{ ".swf", .@"application/x-shockwave-flash" }, 136 | .{ ".tar", .@"application/x-tar" }, 137 | .{ ".tiff", .@"image/tiff" }, 138 | .{ ".ts", .@"video/mp2t" }, 139 | .{ ".ttf", .@"font/ttf" }, 140 | .{ ".txt", .@"text/plain" }, 141 | .{ ".vsd", .@"application/vnd.visio" }, 142 | .{ ".wasm", .@"application/wasm" }, 143 | .{ ".wav", .@"audio/wav" }, 144 | .{ ".weba", .@"audio/webm" }, 145 | .{ ".webm", .@"video/webm" }, 146 | .{ ".webp", .@"image/webp" }, 147 | .{ ".woff", .@"font/woff" }, 148 | .{ ".woff2", .@"font/woff2" }, 149 | .{ ".xhtml", .@"application/xhtml+xml" }, 150 | .{ ".xls", .@"application/vnd.ms-excel" }, 151 | .{ ".xlsx", .@"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }, 152 | .{ ".xml", .@"application/xml" }, 153 | .{ ".xul", .@"application/vnd.mozilla.xul+xml" }, 154 | .{ ".zip", .@"application/zip" }, 155 | .{ ".3gp", .@"video/3gpp" }, 156 | .{ ".3g2", .@"video/3gpp2" }, 157 | .{ ".7z", .@"application/x-7z-compressed" }, 158 | }); 159 | 160 | const std = @import("std"); 161 | -------------------------------------------------------------------------------- /test.zig: -------------------------------------------------------------------------------- 1 | const std = @import("std"); 2 | const mime = @import("mime"); 3 | 4 | test "html smoke test" { 5 | const mime_type = mime.extension_map.get(".html").?; 6 | try std.testing.expectEqualStrings("text/html", @tagName(mime_type)); 7 | } 8 | 9 | test "bogus extension" { 10 | try std.testing.expect(mime.extension_map.get(".sillybogo") == null); 11 | } 12 | --------------------------------------------------------------------------------