| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
use std::fmt::Write as _; |
| 26 |
use std::path::{Path, PathBuf}; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#[derive(Debug)] |
| 33 |
pub enum EmbedError { |
| 34 |
|
| 35 |
Unreadable { |
| 36 |
dir: PathBuf, |
| 37 |
source: std::io::Error, |
| 38 |
}, |
| 39 |
|
| 40 |
BadVersion { file: String }, |
| 41 |
|
| 42 |
Duplicate { version: i64 }, |
| 43 |
|
| 44 |
NoOutDir, |
| 45 |
|
| 46 |
Unwritable { |
| 47 |
dest: PathBuf, |
| 48 |
source: std::io::Error, |
| 49 |
}, |
| 50 |
} |
| 51 |
|
| 52 |
impl std::fmt::Display for EmbedError { |
| 53 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 54 |
match self { |
| 55 |
Self::Unreadable { dir, source } => { |
| 56 |
write!( |
| 57 |
f, |
| 58 |
"migrations directory {} is unreadable: {source}", |
| 59 |
dir.display() |
| 60 |
) |
| 61 |
} |
| 62 |
Self::BadVersion { file } => { |
| 63 |
write!(f, "migration {file}: expected an integer version prefix") |
| 64 |
} |
| 65 |
Self::Duplicate { version } => write!(f, "two migrations claim version {version}"), |
| 66 |
Self::NoOutDir => write!(f, "OUT_DIR is unset; this belongs in a build script"), |
| 67 |
Self::Unwritable { dest, source } => { |
| 68 |
write!(f, "could not write {}: {source}", dest.display()) |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
impl std::error::Error for EmbedError {} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
pub fn from_dir(dir: impl AsRef<Path>) -> Result<(), EmbedError> { |
| 88 |
let manifest = |
| 89 |
std::env::var("CARGO_MANIFEST_DIR").map_or_else(|_| PathBuf::from("."), PathBuf::from); |
| 90 |
let dir = manifest.join(dir.as_ref()); |
| 91 |
let dir = dir.canonicalize().unwrap_or(dir); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
println!("cargo:rerun-if-changed={}", dir.display()); |
| 96 |
|
| 97 |
let mut entries: Vec<(i64, String, PathBuf)> = Vec::new(); |
| 98 |
let listing = std::fs::read_dir(&dir).map_err(|source| EmbedError::Unreadable { |
| 99 |
dir: dir.clone(), |
| 100 |
source, |
| 101 |
})?; |
| 102 |
|
| 103 |
for entry in listing { |
| 104 |
let path = entry |
| 105 |
.map_err(|source| EmbedError::Unreadable { |
| 106 |
dir: dir.clone(), |
| 107 |
source, |
| 108 |
})? |
| 109 |
.path(); |
| 110 |
let Some(name) = path.file_name().and_then(|n| n.to_str()) else { |
| 111 |
continue; |
| 112 |
}; |
| 113 |
let Some((version, rest)) = name.split_once('_') else { |
| 114 |
continue; |
| 115 |
}; |
| 116 |
if !Path::new(rest) |
| 117 |
.extension() |
| 118 |
.is_some_and(|ext| ext.eq_ignore_ascii_case("sql")) |
| 119 |
{ |
| 120 |
continue; |
| 121 |
} |
| 122 |
let version = version.parse::<i64>().map_err(|_| EmbedError::BadVersion { |
| 123 |
file: name.to_owned(), |
| 124 |
})?; |
| 125 |
|
| 126 |
println!("cargo:rerun-if-changed={}", path.display()); |
| 127 |
entries.push(( |
| 128 |
version, |
| 129 |
rest.trim_end_matches(".sql").replace('_', " "), |
| 130 |
path, |
| 131 |
)); |
| 132 |
} |
| 133 |
|
| 134 |
entries.sort_by_key(|(version, _, _)| *version); |
| 135 |
if let Some(pair) = entries.windows(2).find(|w| w[0].0 == w[1].0) { |
| 136 |
return Err(EmbedError::Duplicate { version: pair[0].0 }); |
| 137 |
} |
| 138 |
|
| 139 |
let mut out = String::from( |
| 140 |
"// @generated by quasi_store::embed::from_dir. Do not edit.\n\ |
| 141 |
static MIGRATIONS: &[::quasi_store::Migration] = &[\n", |
| 142 |
); |
| 143 |
for (version, description, path) in &entries { |
| 144 |
|
| 145 |
|
| 146 |
let _ = writeln!( |
| 147 |
out, |
| 148 |
" ::quasi_store::Migration {{ version: {version}, description: {description:?}, sql: include_str!({:?}) }},", |
| 149 |
path.display().to_string() |
| 150 |
); |
| 151 |
} |
| 152 |
out.push_str("];\n"); |
| 153 |
|
| 154 |
let dest = Path::new(&std::env::var("OUT_DIR").map_err(|_| EmbedError::NoOutDir)?) |
| 155 |
.join("quasi_migrations.rs"); |
| 156 |
std::fs::write(&dest, out).map_err(|source| EmbedError::Unwritable { dest, source }) |
| 157 |
} |
| 158 |
|