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

Configure Feed

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

gleam / compiler-cli / src / format.rs
3.1 kB 121 lines
1// SPDX-License-Identifier: Apache-2.0 2// SPDX-FileCopyrightText: 2020 The Gleam contributors 3 4use gleam_core::{ 5 error::{Error, FileIoAction, FileKind, Result, StandardIoAction, Unformatted}, 6 io::Content, 7 io::OutputFile, 8}; 9use std::{io::Read, str::FromStr}; 10 11use camino::{Utf8Path, Utf8PathBuf}; 12 13pub fn run(stdin: bool, check: bool, files: Vec<String>) -> Result<()> { 14 if stdin { 15 process_stdin(check) 16 } else { 17 process_files(check, files) 18 } 19} 20 21fn process_stdin(check: bool) -> Result<()> { 22 let src = read_stdin()?.into(); 23 let mut out = String::new(); 24 gleam_format::pretty(&mut out, &src, Utf8Path::new("<stdin>"))?; 25 26 if !check { 27 print!("{out}"); 28 return Ok(()); 29 } 30 31 if src != out { 32 return Err(Error::Format { 33 problem_files: vec![Unformatted { 34 source: Utf8PathBuf::from("<standard input>"), 35 destination: Utf8PathBuf::from("<standard output>"), 36 input: src, 37 output: out, 38 }], 39 }); 40 } 41 42 Ok(()) 43} 44 45fn process_files(check: bool, files: Vec<String>) -> Result<()> { 46 if check { 47 check_files(files) 48 } else { 49 format_files(files) 50 } 51} 52 53fn check_files(files: Vec<String>) -> Result<()> { 54 let problem_files = unformatted_files(files)?; 55 56 if problem_files.is_empty() { 57 Ok(()) 58 } else { 59 Err(Error::Format { problem_files }) 60 } 61} 62 63fn format_files(files: Vec<String>) -> Result<()> { 64 for file in unformatted_files(files)? { 65 crate::fs::write_output(&OutputFile { 66 path: file.destination, 67 content: Content::Text(file.output), 68 })?; 69 } 70 Ok(()) 71} 72 73pub fn unformatted_files(files: Vec<String>) -> Result<Vec<Unformatted>> { 74 let mut problem_files = Vec::with_capacity(files.len()); 75 76 for file_path in files { 77 let path = Utf8PathBuf::from_str(&file_path).map_err(|e| Error::FileIo { 78 action: FileIoAction::Open, 79 kind: FileKind::File, 80 path: Utf8PathBuf::from(file_path), 81 err: Some(e.to_string()), 82 })?; 83 84 if path.is_dir() { 85 for path in crate::fs::gleam_files(&path) { 86 format_file(&mut problem_files, path)?; 87 } 88 } else { 89 format_file(&mut problem_files, path)?; 90 } 91 } 92 93 Ok(problem_files) 94} 95 96fn format_file(problem_files: &mut Vec<Unformatted>, path: Utf8PathBuf) -> Result<()> { 97 let src = crate::fs::read(&path)?.into(); 98 let mut output = String::new(); 99 gleam_format::pretty(&mut output, &src, &path)?; 100 101 if src != output { 102 problem_files.push(Unformatted { 103 source: path.clone(), 104 destination: path, 105 input: src, 106 output, 107 }); 108 } 109 Ok(()) 110} 111 112pub fn read_stdin() -> Result<String> { 113 let mut src = String::new(); 114 let _ = std::io::stdin() 115 .read_to_string(&mut src) 116 .map_err(|e| Error::StandardIo { 117 action: StandardIoAction::Read, 118 err: Some(e.kind()), 119 })?; 120 Ok(src) 121}