Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

Move serialisation tests to docs

Ensure that JSON nesting is in the snapshot by introducing a new PackageInformation struct

+268 -7
+1 -1
compiler-cli/src/export.rs
··· 170 170 171 171 pub fn package_information(paths: &ProjectPaths, out: Utf8PathBuf) -> Result<()> { 172 172 let config = crate::config::root_config(paths)?; 173 - let out = gleam_core::docs::generate_json_package_information(out, &config); 173 + let out = gleam_core::docs::generate_json_package_information(out, config); 174 174 crate::fs::write_outputs_under(&[out], paths.root())?; 175 175 Ok(()) 176 176 }
+81 -6
compiler-core/src/docs.rs
··· 24 24 use askama::Template; 25 25 use ecow::EcoString; 26 26 use itertools::Itertools; 27 - use serde::Serialize; 28 - use serde_json::{json, to_string as serde_to_string}; 27 + use serde::{Deserialize, Serialize}; 28 + use serde_json::to_string as serde_to_string; 29 29 30 30 const MAX_COLUMNS: isize = 65; 31 31 ··· 33 33 pub enum DocContext { 34 34 HexPublish, 35 35 Build, 36 + } 37 + 38 + #[derive(PartialEq, Debug, Serialize, Deserialize)] 39 + pub struct PackageInformation { 40 + #[serde(rename = "gleam.toml")] 41 + package_config: PackageConfig, 36 42 } 37 43 38 44 pub fn generate_html<IO: FileSystemReader>( ··· 479 485 } 480 486 } 481 487 482 - pub fn generate_json_package_information(path: Utf8PathBuf, config: &PackageConfig) -> OutputFile { 483 - let gleam_toml = serde_json::to_string(&config).expect("JSON module interface serialisation"); 484 - 488 + pub fn generate_json_package_information(path: Utf8PathBuf, config: PackageConfig) -> OutputFile { 485 489 OutputFile { 486 490 path, 487 - content: Content::Text(json!({"gleam.toml": gleam_toml}).to_string()), 491 + content: Content::Text(package_information_as_json(config)), 488 492 } 489 493 } 490 494 495 + fn package_information_as_json(config: PackageConfig) -> String { 496 + let info = PackageInformation { 497 + package_config: config, 498 + }; 499 + serde_json::to_string_pretty(&info).expect("JSON module information serialisation") 500 + } 501 + 491 502 fn page_unnest(path: &str) -> String { 492 503 let unnest = path 493 504 .strip_prefix('/') ··· 889 900 // Erlang, 890 901 Gleam, 891 902 } 903 + 904 + #[test] 905 + fn package_config_to_json() { 906 + let input = r#" 907 + name = "my_project" 908 + version = "1.0.0" 909 + licences = ["Apache-2.0", "MIT"] 910 + description = "Pretty complex config" 911 + target = "erlang" 912 + repository = { type = "github", user = "example", repo = "my_dep" } 913 + links = [{ title = "Home page", href = "https://example.com" }] 914 + internal_modules = ["my_app/internal"] 915 + gleam = ">= 0.30.0" 916 + 917 + [dependencies] 918 + gleam_stdlib = ">= 0.18.0 and < 2.0.0" 919 + my_other_project = { path = "../my_other_project" } 920 + 921 + [dev-dependencies] 922 + gleeunit = ">= 1.0.0 and < 2.0.0" 923 + 924 + [documentation] 925 + pages = [{ title = "My Page", path = "my-page.html", source = "./path/to/my-page.md" }] 926 + 927 + [erlang] 928 + application_start_module = "my_app/application" 929 + extra_applications = ["inets", "ssl"] 930 + 931 + [javascript] 932 + typescript_declarations = true 933 + runtime = "node" 934 + 935 + [javascript.deno] 936 + allow_all = false 937 + allow_ffi = true 938 + allow_env = ["DATABASE_URL"] 939 + allow_net = ["example.com:443"] 940 + allow_read = ["./database.sqlite"] 941 + "#; 942 + 943 + let config = toml::from_str::<PackageConfig>(&input).unwrap(); 944 + let info = PackageInformation { 945 + package_config: config.clone(), 946 + }; 947 + let json = package_information_as_json(config); 948 + let output = format!("--- GLEAM.TOML\n{input}\n\n--- EXPORTED JSON\n\n{json}"); 949 + insta::assert_snapshot!(output); 950 + 951 + let roundtrip: PackageInformation = serde_json::from_str(&json).unwrap(); 952 + assert_eq!(info, roundtrip); 953 + } 954 + 955 + #[test] 956 + fn barebones_package_config_to_json() { 957 + let input = r#" 958 + name = "my_project" 959 + version = "1.0.0" 960 + "#; 961 + 962 + let config = toml::from_str::<PackageConfig>(&input).unwrap(); 963 + let json = package_information_as_json(config); 964 + let output = format!("--- GLEAM.TOML\n{input}\n\n--- EXPORTED JSON\n\n{json}"); 965 + insta::assert_snapshot!(output); 966 + }
+9
compiler-core/src/requirement.rs
··· 14 14 #[serde(untagged, remote = "Self")] 15 15 pub enum Requirement { 16 16 Hex { 17 + #[serde(deserialize_with = "deserialise_range")] 17 18 version: Range, 18 19 }, 19 20 Path { ··· 84 85 } 85 86 86 87 // Deserialization 88 + 89 + fn deserialise_range<'de, D>(deserializer: D) -> Result<Range, D::Error> 90 + where 91 + D: Deserializer<'de>, 92 + { 93 + let version = String::deserialize(deserializer)?; 94 + Ok(Range::new(version)) 95 + } 87 96 88 97 #[derive(Debug, Copy, Clone)] 89 98 pub struct Void;
+53
compiler-core/src/snapshots/gleam_core__docs__barebones_package_config_to_json.snap
··· 1 + --- 2 + source: compiler-core/src/docs.rs 3 + expression: output 4 + --- 5 + --- GLEAM.TOML 6 + 7 + name = "my_project" 8 + version = "1.0.0" 9 + 10 + 11 + --- EXPORTED JSON 12 + 13 + { 14 + "gleam.toml": { 15 + "name": "my_project", 16 + "version": "1.0.0", 17 + "gleam": null, 18 + "licences": [], 19 + "description": "", 20 + "documentation": { 21 + "pages": [] 22 + }, 23 + "dependencies": {}, 24 + "dev-dependencies": {}, 25 + "repository": { 26 + "type": "none" 27 + }, 28 + "links": [], 29 + "erlang": { 30 + "application_start_module": null, 31 + "extra_applications": [] 32 + }, 33 + "javascript": { 34 + "typescript_declarations": false, 35 + "runtime": "nodejs", 36 + "deno": { 37 + "allow_env": [], 38 + "allow_sys": false, 39 + "allow_hrtime": false, 40 + "allow_net": [], 41 + "allow_ffi": false, 42 + "allow_read": [], 43 + "allow_run": [], 44 + "allow_write": [], 45 + "allow_all": false, 46 + "unstable": false, 47 + "location": null 48 + } 49 + }, 50 + "target": "erlang", 51 + "internal_modules": null 52 + } 53 + }
+124
compiler-core/src/snapshots/gleam_core__docs__package_config_to_json.snap
··· 1 + --- 2 + source: compiler-core/src/docs.rs 3 + expression: output 4 + --- 5 + --- GLEAM.TOML 6 + 7 + name = "my_project" 8 + version = "1.0.0" 9 + licences = ["Apache-2.0", "MIT"] 10 + description = "Pretty complex config" 11 + target = "erlang" 12 + repository = { type = "github", user = "example", repo = "my_dep" } 13 + links = [{ title = "Home page", href = "https://example.com" }] 14 + internal_modules = ["my_app/internal"] 15 + gleam = ">= 0.30.0" 16 + 17 + [dependencies] 18 + gleam_stdlib = ">= 0.18.0 and < 2.0.0" 19 + my_other_project = { path = "../my_other_project" } 20 + 21 + [dev-dependencies] 22 + gleeunit = ">= 1.0.0 and < 2.0.0" 23 + 24 + [documentation] 25 + pages = [{ title = "My Page", path = "my-page.html", source = "./path/to/my-page.md" }] 26 + 27 + [erlang] 28 + application_start_module = "my_app/application" 29 + extra_applications = ["inets", "ssl"] 30 + 31 + [javascript] 32 + typescript_declarations = true 33 + runtime = "node" 34 + 35 + [javascript.deno] 36 + allow_all = false 37 + allow_ffi = true 38 + allow_env = ["DATABASE_URL"] 39 + allow_net = ["example.com:443"] 40 + allow_read = ["./database.sqlite"] 41 + 42 + 43 + --- EXPORTED JSON 44 + 45 + { 46 + "gleam.toml": { 47 + "name": "my_project", 48 + "version": "1.0.0", 49 + "gleam": ">= 0.30.0", 50 + "licences": [ 51 + "Apache-2.0", 52 + "MIT" 53 + ], 54 + "description": "Pretty complex config", 55 + "documentation": { 56 + "pages": [ 57 + { 58 + "title": "My Page", 59 + "path": "my-page.html", 60 + "source": "./path/to/my-page.md" 61 + } 62 + ] 63 + }, 64 + "dependencies": { 65 + "gleam_stdlib": { 66 + "version": ">= 0.18.0 and < 2.0.0" 67 + }, 68 + "my_other_project": { 69 + "path": "../my_other_project" 70 + } 71 + }, 72 + "dev-dependencies": { 73 + "gleeunit": { 74 + "version": ">= 1.0.0 and < 2.0.0" 75 + } 76 + }, 77 + "repository": { 78 + "type": "github", 79 + "user": "example", 80 + "repo": "my_dep", 81 + "path": null 82 + }, 83 + "links": [ 84 + { 85 + "title": "Home page", 86 + "href": "https://example.com/" 87 + } 88 + ], 89 + "erlang": { 90 + "application_start_module": "my_app/application", 91 + "extra_applications": [ 92 + "inets", 93 + "ssl" 94 + ] 95 + }, 96 + "javascript": { 97 + "typescript_declarations": true, 98 + "runtime": "nodejs", 99 + "deno": { 100 + "allow_env": [ 101 + "DATABASE_URL" 102 + ], 103 + "allow_sys": false, 104 + "allow_hrtime": false, 105 + "allow_net": [ 106 + "example.com:443" 107 + ], 108 + "allow_ffi": true, 109 + "allow_read": [ 110 + "./database.sqlite" 111 + ], 112 + "allow_run": [], 113 + "allow_write": [], 114 + "allow_all": false, 115 + "unstable": false, 116 + "location": null 117 + } 118 + }, 119 + "target": "erlang", 120 + "internal_modules": [ 121 + "my_app/internal" 122 + ] 123 + } 124 + }