dag/nameset/
legacy.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8use super::super::NameDag;
9use super::IdStaticSet;
10use super::NameSet;
11use crate::IdSet;
12
13/// A legacy token that enables conversion between IdSet (id-based)
14/// and NameSet (hash-based). It should not be used for new Rust code.
15#[derive(Copy, Clone)]
16pub struct LegacyCodeNeedIdAccess;
17
18// This is ideally not provided.  However revision numbers in revset still have
19// large use-cases in Python and for now we provide this way to convert IdStaticSet
20// to IdSet using "revision" numbers.
21impl<'a> From<(LegacyCodeNeedIdAccess, &'a IdStaticSet)> for IdSet {
22    fn from(value: (LegacyCodeNeedIdAccess, &'a IdStaticSet)) -> IdSet {
23        let set = value.1;
24        set.spans.clone()
25    }
26}
27
28impl<'a> From<(LegacyCodeNeedIdAccess, IdSet, &'a NameDag)> for NameSet {
29    fn from(value: (LegacyCodeNeedIdAccess, IdSet, &'a NameDag)) -> NameSet {
30        NameSet::from_spans_dag(value.1, value.2).unwrap()
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use nonblocking::non_blocking_result as r;
37
38    use super::super::id_static::tests::with_dag;
39    use super::*;
40    use crate::DagAlgorithm;
41    use crate::Result;
42
43    #[test]
44    fn test_legacy_convert() -> Result<()> {
45        use LegacyCodeNeedIdAccess as L;
46        with_dag(|dag| -> Result<()> {
47            let set1 = r(dag.ancestors("G".into()))?;
48            let spans: IdSet = (
49                L,
50                set1.as_any().downcast_ref::<IdStaticSet>().unwrap().clone(),
51            )
52                .into();
53            let set2: NameSet = (L, spans.clone(), dag).into();
54            assert_eq!(format!("{:?}", &set1), "<spans [E:G+4:6, A:B+0:1]>");
55            assert_eq!(format!("{:?}", &set2), "<spans [E:G+4:6, A:B+0:1]>");
56            assert_eq!(format!("{:?}", &spans), "0 1 4 5 6");
57            Ok(())
58        })
59    }
60}