Skip to content

Commit 1916f8e

Browse files
committed
Step 0.2: Add context and errors
1 parent 7126b7b commit 1916f8e

4 files changed

Lines changed: 181 additions & 13 deletions

File tree

.codex/rules/default.rules

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
prefix_rule(pattern = ["git", "status"], decision = "allow")
2+
prefix_rule(pattern = ["git", "add"], decision = "allow")
3+
prefix_rule(pattern = ["git", "diff"], decision = "allow")
4+
prefix_rule(pattern = ["git", "commit"], decision = "allow")
5+
prefix_rule(pattern = ["git", "restore"], decision = "allow")
6+
prefix_rule(pattern = ["git", "checkout"], decision = "allow")
7+
8+
prefix_rule(pattern = ["jj"], decision = "allow")
9+
10+
prefix_rule(pattern = ["cargo", "add"], decision = "allow")
11+
prefix_rule(pattern = ["cargo", "fmt"], decision = "allow")
12+
prefix_rule(pattern = ["cargo", "test"], decision = "allow")
13+
prefix_rule(pattern = ["cargo", "check"], decision = "allow")
14+
prefix_rule(pattern = ["cargo", "clippy"], decision = "allow")
15+
prefix_rule(pattern = ["cargo", "build"], decision = "allow")
16+
prefix_rule(pattern = ["cargo", "run"], decision = "allow")

zb_core/src/context.rs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
use std::path::PathBuf;
2+
3+
#[derive(Clone, Debug, PartialEq, Eq)]
4+
pub struct Paths {
5+
pub root: PathBuf,
6+
pub store: PathBuf,
7+
pub cellar: PathBuf,
8+
pub cache: PathBuf,
9+
pub db: PathBuf,
10+
pub locks: PathBuf,
11+
}
12+
13+
impl Paths {
14+
pub fn from_root(root: PathBuf) -> Self {
15+
let store = root.join("store");
16+
let cellar = root.join("cellar");
17+
let cache = root.join("cache");
18+
let db = root.join("db").join("zb.sqlite3");
19+
let locks = root.join("locks");
20+
21+
Self {
22+
root,
23+
store,
24+
cellar,
25+
cache,
26+
db,
27+
locks,
28+
}
29+
}
30+
}
31+
32+
#[derive(Clone, Debug, PartialEq, Eq)]
33+
pub struct ConcurrencyLimits {
34+
pub download: usize,
35+
pub unpack: usize,
36+
pub materialize: usize,
37+
}
38+
39+
impl Default for ConcurrencyLimits {
40+
fn default() -> Self {
41+
Self {
42+
download: 16,
43+
unpack: 4,
44+
materialize: 4,
45+
}
46+
}
47+
}
48+
49+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
50+
pub enum LogLevel {
51+
Info,
52+
Warn,
53+
Error,
54+
}
55+
56+
#[derive(Clone, Debug, PartialEq, Eq)]
57+
pub struct LoggerHandle {
58+
pub level: LogLevel,
59+
}
60+
61+
impl Default for LoggerHandle {
62+
fn default() -> Self {
63+
Self {
64+
level: LogLevel::Info,
65+
}
66+
}
67+
}
68+
69+
#[derive(Clone, Debug, PartialEq, Eq)]
70+
pub struct Context {
71+
pub paths: Paths,
72+
pub concurrency: ConcurrencyLimits,
73+
pub logger: LoggerHandle,
74+
}
75+
76+
impl Context {
77+
pub fn from_defaults() -> Self {
78+
Self {
79+
paths: Paths::from_root(PathBuf::from("/opt/zerobrew")),
80+
concurrency: ConcurrencyLimits::default(),
81+
logger: LoggerHandle::default(),
82+
}
83+
}
84+
}
85+
86+
#[cfg(test)]
87+
mod tests {
88+
use super::*;
89+
90+
#[test]
91+
fn from_defaults_sets_expected_paths() {
92+
let context = Context::from_defaults();
93+
94+
assert_eq!(context.paths.root, PathBuf::from("/opt/zerobrew"));
95+
assert_eq!(
96+
context.paths.store,
97+
PathBuf::from("/opt/zerobrew").join("store")
98+
);
99+
assert_eq!(
100+
context.paths.cellar,
101+
PathBuf::from("/opt/zerobrew").join("cellar")
102+
);
103+
assert_eq!(
104+
context.paths.cache,
105+
PathBuf::from("/opt/zerobrew").join("cache")
106+
);
107+
assert_eq!(
108+
context.paths.db,
109+
PathBuf::from("/opt/zerobrew").join("db").join("zb.sqlite3")
110+
);
111+
assert_eq!(
112+
context.paths.locks,
113+
PathBuf::from("/opt/zerobrew").join("locks")
114+
);
115+
}
116+
}

zb_core/src/errors.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use std::fmt;
2+
use std::path::PathBuf;
3+
4+
#[derive(Clone, Debug, PartialEq, Eq)]
5+
pub enum Error {
6+
UnsupportedBottle { name: String },
7+
ChecksumMismatch { expected: String, actual: String },
8+
LinkConflict { path: PathBuf },
9+
StoreCorruption { message: String },
10+
NetworkFailure { message: String },
11+
}
12+
13+
impl fmt::Display for Error {
14+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15+
match self {
16+
Error::UnsupportedBottle { name } => {
17+
write!(f, "unsupported bottle for formula '{name}'")
18+
}
19+
Error::ChecksumMismatch { expected, actual } => {
20+
write!(f, "checksum mismatch (expected {expected}, got {actual})")
21+
}
22+
Error::LinkConflict { path } => {
23+
write!(f, "link conflict at '{}'", path.to_string_lossy())
24+
}
25+
Error::StoreCorruption { message } => write!(f, "store corruption: {message}"),
26+
Error::NetworkFailure { message } => write!(f, "network failure: {message}"),
27+
}
28+
}
29+
}
30+
31+
impl std::error::Error for Error {}
32+
33+
#[cfg(test)]
34+
mod tests {
35+
use super::*;
36+
37+
#[test]
38+
fn unsupported_bottle_display_includes_name() {
39+
let err = Error::UnsupportedBottle {
40+
name: "libheif".to_string(),
41+
};
42+
43+
assert!(err.to_string().contains("libheif"));
44+
}
45+
}

zb_core/src/lib.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
pub fn add(left: u64, right: u64) -> u64 {
2-
left + right
3-
}
1+
pub mod context;
2+
pub mod errors;
43

5-
#[cfg(test)]
6-
mod tests {
7-
use super::*;
8-
9-
#[test]
10-
fn it_works() {
11-
let result = add(2, 2);
12-
assert_eq!(result, 4);
13-
}
14-
}
4+
pub use context::{ConcurrencyLimits, Context, LogLevel, LoggerHandle, Paths};
5+
pub use errors::Error;

0 commit comments

Comments
 (0)