Line data Source code
1 : use anyhow::{anyhow, Context, Result}; 2 : use semver::Version; 3 : use std::{ 4 : fs::{read_to_string, write}, 5 : path::Path, 6 : }; 7 : use toml_edit::{DocumentMut, Item, Value}; 8 : 9 : #[allow(clippy::module_name_repetitions)] 10 2 : pub fn clippy_utils_version_from_rust_version(rust_version: &str) -> Result<String> { 11 2 : Version::parse(rust_version.strip_prefix("rust-").unwrap_or(rust_version)) 12 2 : .map(|version| Version::new(0, version.major, version.minor).to_string()) 13 2 : .map_err(Into::into) 14 2 : } 15 : 16 : #[allow(clippy::module_name_repetitions)] 17 13349 : pub fn clippy_utils_package_version(path: &Path) -> Result<String> { 18 13349 : let cargo_toml = path.join("clippy_utils/Cargo.toml"); 19 13349 : let contents = read_to_string(&cargo_toml).with_context(|| { 20 0 : format!( 21 0 : "`read_to_string` failed for `{}`", 22 0 : cargo_toml.to_string_lossy(), 23 0 : ) 24 13349 : })?; 25 13349 : let document = contents.parse::<DocumentMut>()?; 26 13349 : document 27 13349 : .as_table() 28 13349 : .get("package") 29 13349 : .and_then(Item::as_table) 30 13349 : .and_then(|table| table.get("version")) 31 13349 : .and_then(Item::as_str) 32 13349 : .map(ToOwned::to_owned) 33 13349 : .ok_or_else(|| anyhow!("Could not determine `clippy_utils` version")) 34 13349 : } 35 : 36 4 : pub fn set_clippy_utils_dependency_revision(path: &Path, rev: &str) -> Result<()> { 37 4 : let cargo_toml = path.join("Cargo.toml"); 38 4 : let contents = read_to_string(&cargo_toml).with_context(|| { 39 0 : format!( 40 0 : "`read_to_string` failed for `{}`", 41 0 : cargo_toml.to_string_lossy(), 42 0 : ) 43 4 : })?; 44 4 : let mut document = contents.parse::<DocumentMut>()?; 45 : // smoelius: First check `dependencies` for `clippy_utils`. 46 4 : let mut clippy_utils = document 47 4 : .as_table_mut() 48 4 : .get_mut("dependencies") 49 4 : .and_then(Item::as_table_mut) 50 4 : .and_then(|table| table.get_mut("clippy_utils")); 51 4 : // smoelius: It it's not found there, check `workspace.dependencies`. 52 4 : if clippy_utils.is_none() { 53 0 : clippy_utils = document 54 0 : .as_table_mut() 55 0 : .get_mut("workspace") 56 0 : .and_then(Item::as_table_mut) 57 0 : .and_then(|table| table.get_mut("dependencies")) 58 0 : .and_then(Item::as_table_mut) 59 0 : .and_then(|table| table.get_mut("clippy_utils")); 60 4 : }; 61 4 : clippy_utils 62 4 : .and_then(Item::as_inline_table_mut) 63 4 : .and_then(|table| table.get_mut("rev")) 64 4 : .map(|value| *value = Value::from(rev)) 65 4 : .ok_or_else(|| anyhow!("Could not set `clippy_utils` revision"))?; 66 4 : write(cargo_toml, document.to_string()).map_err(Into::into) 67 4 : } 68 : 69 13352 : pub fn toolchain_channel(path: &Path) -> Result<String> { 70 13352 : let rust_toolchain = path.join("rust-toolchain"); 71 13352 : let contents = read_to_string(&rust_toolchain).with_context(|| { 72 0 : format!( 73 0 : "`read_to_string` failed for `{}`", 74 0 : rust_toolchain.to_string_lossy(), 75 0 : ) 76 13352 : })?; 77 13352 : let document = contents.parse::<DocumentMut>()?; 78 13352 : document 79 13352 : .as_table() 80 13352 : .get("toolchain") 81 13352 : .and_then(Item::as_table) 82 13352 : .and_then(|table| table.get("channel")) 83 13352 : .and_then(Item::as_str) 84 13352 : .map(ToOwned::to_owned) 85 13352 : .ok_or_else(|| anyhow!("Could not determine Rust toolchain channel")) 86 13352 : } 87 : 88 4 : pub fn set_toolchain_channel(path: &Path, channel: &str) -> Result<()> { 89 4 : let rust_toolchain = path.join("rust-toolchain"); 90 4 : let contents = read_to_string(&rust_toolchain).with_context(|| { 91 0 : format!( 92 0 : "`read_to_string` failed for `{}`", 93 0 : rust_toolchain.to_string_lossy(), 94 0 : ) 95 4 : })?; 96 4 : let mut document = contents.parse::<DocumentMut>()?; 97 4 : document 98 4 : .as_table_mut() 99 4 : .get_mut("toolchain") 100 4 : .and_then(Item::as_table_mut) 101 4 : .and_then(|table| table.get_mut("channel")) 102 4 : .and_then(Item::as_value_mut) 103 4 : .map(|value| *value = Value::from(channel)) 104 4 : .ok_or_else(|| anyhow!("Could not set Rust toolchain channel"))?; 105 4 : write(rust_toolchain, document.to_string()).map_err(Into::into) 106 4 : }