1use serde::{Deserialize, Serialize};
10
11pub const MAX_PINS: usize = 1;
14
15pub const REFUSE_FULL: &str = "One pane is pinned to a window at a time: unpin the pinned pane first.";
17pub const REFUSE_ALONE: &str = "The only pane of a tab cannot be pinned: it would cover every other tab.";
19pub const REFUSE_MAXIMIZED: &str = "A maximized pane cannot be pinned: restore it first.";
21
22#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
24pub struct PinRecord {
25 #[serde(rename = "blockid")]
27 pub block_id: String,
28 pub order: u32,
30}
31
32pub fn refuse_pin(pins: &[PinRecord], block_id: &str) -> Option<&'static str> {
34 if pins.iter().any(|p| p.block_id == block_id) {
35 return Some("that pane is already pinned");
36 }
37 if pins.len() >= MAX_PINS {
38 return Some(REFUSE_FULL);
39 }
40 None
41}
42
43pub fn next_order(pins: &[PinRecord]) -> u32 {
45 pins.iter().map(|p| p.order + 1).max().unwrap_or(0)
46}
47
48#[cfg(test)]
49mod tests {
50 use super::*;
51
52 fn pin(id: &str, order: u32) -> PinRecord {
53 PinRecord { block_id: id.into(), order }
54 }
55
56 #[test]
57 fn one_pin_at_most_and_a_pane_is_pinned_once() {
58 assert!(refuse_pin(&[], "a").is_none());
59 assert_eq!(refuse_pin(&[pin("a", 0)], "b"), Some(REFUSE_FULL));
60 assert_eq!(refuse_pin(&[pin("a", 0)], "a"), Some("that pane is already pinned"));
61 }
62
63 #[test]
64 fn orders_count_up() {
65 assert_eq!(next_order(&[]), 0);
66 assert_eq!(next_order(&[pin("a", 3)]), 4);
67 }
68
69 #[test]
70 fn a_pin_stored_with_an_edge_and_a_size_still_reads() {
71 let old = r#"{"blockid":"a","edge":"left","size":0.3,"order":2}"#;
73 assert_eq!(serde_json::from_str::<PinRecord>(old).unwrap(), pin("a", 2));
74 }
75}